Ad

How To Configure JSON For Graphql Query?

- 1 answer

I am trying to send queries to a Shopify site using the Storefront API provided. The API in question requires I use GraphQL, which is just JSON POSTed to a url endpoint. Given a query like this:

query {
  shop{
    products(first: 2) {
      edges {
        node {
          variants(first: 2) {
            edges {
              node {
                id
              }
            }
          }
        }
      }
    }
  }
}

How do I form the proper JSON such that the site responds appropriately?

Ad

Answer

The POST payload format is defined by the API developer. But typically it is like this:

{"query": "query { ... }"}

and oftentimes it includes a token, like this:

{"query": "query { ... }", "token" : "..."}

You are better off using a library, such as the graphql-batch library offered by Shopify.

Ad
source: stackoverflow.com
Ad