skip to Main Content

I can’t see how to format this graphene query for shopify. I need to replicate this curl query with graphene in Django:

    curl -X POST 
"https://<shop>.myshopify.com/api/graphql" 
-H "Content-Type: application/graphql" 
-H "X-Shopify-Storefront-Access-Token: <storefront-access-token>" 
-d '
{
  shop {
    collections(first: 5) {
      edges {
        node {
          id
          handle
        }
      }
      pageInfo {
        hasNextPage
      }
    }
  }
}
'

So far I have:

access_token = 'some_token'
    headers = (
        { "Content-Type": "application/graphql" },
        { "X-Shopify-Storefront-Access-Token": access_token},
    )
    schema = graphene.Schema(query=Query)
    print(schema)
    result = schema.execute('{
        catsinuniform {
            collections(first: 5) {
              edges {
                node {
                  id
                  handle
                }
              }
              pageInfo {
                hasNextPage
              }
            }
        }'')
    print(result.data['catsinuniform'])

This syntax is wrong for graphene but I don’t get how it should look? Once I have the data in the right format I can then do a requests post to get the informaiton I want from the shopify storefrontapi

2

Answers


  1. Graphene is an implemention of the GraphQL spec for Python, meant for creating and executing your own GraphQL schema. It is not a GraphQL client for making requests to existing GraphQL servers. You can make calls to the Shopify API using any regular HTTP library, like requests, or you can use something like gql. A simple example:

    import requests
    
    access_token = <YOUR TOKEN>
    headers = {
        "Content-Type": "application/graphql",
        "X-Shopify-Storefront-Access-Token": access_token
    }
    
    query = """
    {
      shop {
        collections(first: 5) {
          edges {
            node {
              id
              handle
            }
          }
          pageInfo {
            hasNextPage
          }
        }
      }
    }
    """
    
    request = requests.post('https://<YOUR SHOP>.myshopify.com/api/graphql', json={'query': query}, headers=headers)
    result = request.json()
    
    Login or Signup to reply.
  2. As of version 5.1.0 of the Shopify Python API, support for querying the Shopify Admin API with Graphql is included:

    client = shopify.GraphQL()
    query = '''
        {
            shop {
                name
                id
            }
        }
    '''
    result = client.execute(query)
    

    Shopify Python API Graphql Docs

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search