Why does FaunaDB output differ from Graphqli? - graphql

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 } } }

Related

GraphQL parameters for an exists filter in API Platform

I have setup an exists filter for an entity:
#[ApiFilter(ExistsFilter::class, properties: ['firstName', 'lastName'])]
I can then run the query just fine:
query accounts {
accounts(exists: {firstName: true}) {
edges {
node {
id
}
}
}
}
But I would like to parameterize the exists filter. The closest I have got is this:
Query
query getAccounts($exists: [AccountFilter_exists]) {
accounts(exists: $exists) {
edges {
node {
id
}
}
}
}
Parameters
{
"exists": {"firstName": true}
}
But I get this error message:
{
"errors": [
{
"message": "Variable \"$exists\" got invalid value {\"firstName\":true}; Expected type AccountFilter_exists to be an object at value.firstName.",
"extensions": {
"category": "graphql"
},
"locations": [
{
"line": 1,
"column": 19
}
]
}
]
}
Does anyone know where I am going wrong?
Thanks for your help.
You actually need to provide the exists filters in this format:
{
"exists": [{"firstName": true}]
}
https://api-platform.com/docs/v2.7/core/graphql/#syntax-for-filters-with-a-list-of-key--value-arguments
Hope this helps.

How to filter on entityBundle using graphql query in drupal

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.

GraphQL - How to get field types from the retrieved schema?

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.

Shopify GraphQL Checkout Create Mutation

I cannot create a checkout with Shopify's Graphql API
I am literally copying the example from this page in Shopify's Checkout Guide and pasting it into Shopify's GraphiQL App installed on the store where I am trying to create the checkout.
This is my mutation, where the only thing I changed was the variantId so it matches one on my store:
mutation {
checkoutCreate(input: {
lineItems: [{ variantId: "gid://shopify/ProductVariant/46037988422", quantity: 1 }]
}) {
checkout {
id
webUrl
lineItems(first: 5) {
edges {
node {
title
quantity
}
}
}
}
}
}
This is the response I'm getting from Shopify:
{
"errors": [
{
"message": "Field 'checkoutCreate' doesn't exist on type 'Mutation'",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"mutation",
"checkoutCreate"
],
"extensions": {
"code": "undefinedField",
"typeName": "Mutation",
"fieldName": "checkoutCreate"
}
}
The weird thing is that obviously checkoutCreate IS a mutation, according to Shopify. See the link to the page here
Then I noticed, that the mutation on that page is different. So I'm trying that version, without a variable like this:
mutation checkoutCreate(input: {
lineItems: [{ variantId: "gid://shopify/ProductVariant/46037988422", quantity: 1 }]
}) {
checkout {
id
}
checkoutUserErrors {
code
field
message
}
}
And now the error I'm getting back is:
{
"errors": [
{
"message": "Parse error on \"input\" (INPUT) at [1, 25]",
"locations": [
{
"line": 1,
"column": 25
}
]
}
]
}
Finally I tried this version with a variable and it also failed:
mutation checkoutCreate($input: CheckoutCreateInput!) {
checkoutCreate(input: $input) {
checkout {
id
}
checkoutUserErrors {
code
field
message
}
}
}
{
"input": {
lineItems: [{ variantId: "gid://shopify/ProductVariant/46037988422", quantity: 1 }]
}
}
The error here was:
{
"errors": [
{
"message": "Parse error on \"input\" (STRING) at [15, 3]",
"locations": [
{
"line": 15,
"column": 3
}
]
}
]
}
On top of all this, Shopify has interactive docs in their GraphiQL App.. and it does NOT list checkoutCreate as an available mutation. See this screenshot: https://nimb.ws/af4iHx
I believe your input is parsed as JSON, so try putting quotes even around the nested properties of your mutation variables when testing.
{
"input": {
"lineItems": [{ "variantId": "gid://shopify/ProductVariant/46037988422",
"quantity": 1 }]
}
}
The mutations that complete checkouts are only available for sales channels. These apps must be public. So it might not work if you are creating a private app.
https://shopify.dev/tutorials/create-a-checkout-with-storefront-api
https://shopify.dev/tutorials/authenticate-a-public-app-with-oauth#turn-an-app-into-a-sales-channel

prismic graphql querying single user

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"})

Resources