In GraphQL, is it possible to have the output JSON in a different structure than the input data/JSON?
Ex: Below is the Data/input JSON on which query will be executed
{
"BatchId": 1897173,
"ApprovalCode": "12528809",
"ResponseCode": 0,
"ResponseMessage": "PENDING"
}
and the expected output after query is executed to be
{
"Batch": {
"id": 1897173
},
"ApprovalCode": "12528809",
"Response": {
"ResponseCode": 0,
"ResponseMessage": "PENDING"
}
}
Related
I have created a simple user.gql file
type Query {
users: [user]
userById(id:ID!):user
}
type user {
id: ID!
chat_data: String
}
My data is
[
{
"id": "0815960b-9725-48d5-b326-7718c4749cf5",
"chat_data": ""
}
]
When I run this on my local server and use the query
{users{id}}
I see the expected output
{
"data": {
"users": [
{
"id": "0815960b-9725-48d5-b326-7718c4749cf5"
}
]
}
}
I have created a user collection on FaunaDB with the data
{
"ref": Ref(Collection("user"), "324407037973758152"),
"ts": 1645691670220000,
"data": {
"id": "0815960b-9725-48d5-b326-7718c4749cf5",
"chat_data": ""
}
}
and uploaded my user.gql, but when I run the GraphQl query
{users{id}}
I get the error
{
"data": null,
"errors": [
{
"message": "Cannot query field 'id' on type 'userPage'. (line 3, column 5):\n id\n ^",
"locations": [
{
"line": 3,
"column": 5
}
]
}
]
}
What am I doing wrong?
This is very unintuitive, but Fauna seems to be returning a paginated result. Read more about it here.
The best thing would be to GraphiQL to have a look at the schema of the Fauna GraphQL endpoint. Autocomplete should also work when you look for fields to query. The error basically says that you can't query the id directly. Try this:
{ users { data { id } } }
I am using Elasticsearch7 and create a pipeline based on https://www.elastic.co/guide/en/elasticsearch/reference/current/ingest.html.
The pipeline is to decode base64 like below:
{
"version": 1,
"processors": [
{
"script": {
"source": "ctx.decoded = ctx.data.decodeBase64();"
}
},
{
"json": {
"field": "decoded",
"add_to_root": true
}
},
{
"remove": {
"field": "decoded"
}
}
]
}
I receive error when I push data to the index:
Response for request ff6dd42a-5694-4c17-b103-c37bae72af58 is not recognized as valid JSON or has unexpected fields. Raw response received: 400 {\"error\":{\"root_cause\":[{\"type\":\"script_exception\",\"reason\":\"runtime error\",\"script_stack\":[\"ctx.decoded = ctx.data.decodeBase64();\",\" ^---- HERE\"],\"script\":\"ctx.decoded = ctx.data.decodeBase64();\",\"lang\":\"painless\",\"position\":{\"offset\":22,\"start\":0,\"end\":38}}],\"type\":\"script_exception\",\"reason\":\"runtime error\",\"script_stack\":[\"ctx.decoded = ctx.data.decodeBase64();\",\" ^---- HERE\"],\"script\":\"ctx.decoded = ctx.data.decodeBase64();\",\"lang\":\"painless\",\"position\":{\"offset\":22,\"start\":0,\"end\":38},\"caused_by\":{\"type\":\"null_pointer_exception\",\"reason\":\"Cannot invoke \\\"Object.getClass()\\\" because \\\"callArgs[0]\\\" is null\"}},\"status\":400}
The reason is that the json is not a single one. It is a multiple json data:
{
"data": "..."
}
{
"data": "..."
}
They are not valid json array so how can I let Elasticsearch piepline accepts this format?
I am using the GraphQl api for Drupal 8. Unfortunately I don't know either technology particularly well.
I have the following query
query {
nodeQuery (offset: 0, limit: 23) {
entities {
entityLabel
entityBundle
entityId
}
count
}
}
which returns stuff that looks like this
{
"data": {
"nodeQuery": {
"entities": [
{
"entityLabel": "Frontpage",
"entityBundle": "section_page",
"entityId": "20"
},
....
Some of the entities returned are not section_page entities however, so I would like to do a filter that allows me to filter them out.
I have done the following
query {
nodeQuery (offset: 0, limit: 23,filter: {conditions: {field: "entityBundle", value: "section_page", operator: EQUAL}}) {
entities {
entityLabel
entityBundle
entityId
}
count
}
}
which doesn't work and I wouldn't really expect it to that much because obviously entityBundle is not a child of the node, so I should somehow filter on entityBundle inside entities. Haven't figured out how to do it.
The error I get when I run that query is
{
"errors": [
{
"message": "Internal server error",
"category": "internal",
"locations": [
{
"line": 32,
"column": 5
}
],
"path": [
"nodeQuery",
"entities"
]
},
{
"message": "Internal server error",
"category": "internal",
"locations": [
{
"line": 37,
"column": 5
}
],
"path": [
"nodeQuery",
"count"
]
}
],
"data": {
"nodeQuery": {
"entities": null,
"count": null
}
}
}
Ok, figured it out, surprisingly
query {
nodeQuery (offset: 0, limit: 23, filter: {conditions: [
{operator: EQUAL, field: "type", value: ["section_page"]}]}) {
entities {
entityLabel
entityBundle
entityId
}
count
}
}
since it seems that the type of the field was also the same as the entityBundle value.
Knowing the schema (fetched via getIntrospectionQuery), how could I get the type of a particular field?
For example, say I run this query:
query {
User {
name
lastUpdated
friends {
name
}
}
}
and get this result:
{
"data": {
"User": [
{
"name": "alice",
"lastUpdated": "2018-02-03T17:22:49+00:00",
"friends": []
},
{
"name": "bob",
"lastUpdated": "2017-09-01T17:08:49+00:00",
"friends": [
{
"name": "eve"
}
]
}
]
}
}
I'd like to know the types of the fields and construct something like this:
{
"name": "String",
"lastUpdated": "timestamptz",
"friends": "[Friend]"
}
How could I do that without extra requests to the server?
After retrieving the schema, you can build it into a JSON object (if your graphql framework does not do it already for you).
Using a JSON parser, you can retrieve the the types of each field.
I will not enter into the detail, as it would depend on the technology your are using.
I'm trying to figure out how to query a single user from graphql schema by id. I'm using the graphiql tool and I'm able to get all Users.
{
allPrismicUsers {
edges {
node {
id
data {
name
}
}
}
}
}
Outputs :
{
"data": {
"allPrismicUsers”: {
"edges": [
{
"node": {
"id": "Prismic__User__WzKFwywAABmiZk_4",
"data": {
"name": “John Doe”
}
}
},
{
"node": {
"id": "Prismic__User__WzKDZywAABmiZkYp",
"data": {
"name": “Jane Doe“
}
}
},
{
"node": {
"id": "Prismic__User__WzKGDiwAAJSiZlFL",
"data": {
"name": “Cat Doe”
}
}
}
]
}
}
}
I also have prismicUser() on the schema
query {
prismicUser {
id
data {
name
}
}
}
Output:
{
"data": {
"prismicUser": {
"id": "Prismic__User__WzKGDiwAAJSiZlFL",
"data": {
"name": "Cat Doe"
}
}
}
}
I'm trying to query a user based on a specific id but not sure if I'm querying the wrong way.
I tried this.
query {
prismicLocation(id: "Prismic__User__WzKDZywAABmiZkYp") {
data {
name
}
}
}
I get an error
{ "errors": [
{
"message": "Argument \"id\" has invalid value \"Prismic__User__WzKDZywAABmiZkYp\".\nExpected
\"prismicUserIdQueryString_2\", found not an object.",
"users": [
{
"line": 25,
"column": 23
}
]
} ] }
How can I call a specific user based on their id ?
According to the Gatsby GraphQL reference, Gatsby allows you to filter results by any field in GraphQL (using operators such as eq, ne, etc.)
The gatsby-source-prismic plugin provides a field called prismicId (i.e. W1syKSIAAAzdN1Jg).
Here is an example query:
{
prismicUser(prismicId:{eq:"WzKDZywAABmiZkYp"}) {
data {
name
}
}
}
But you can also query by id:
prismicUser(id:{eq:"Prismic__User__WzKDZywAABmiZkYp"})