Shopify graphQL查询

时间:2018-11-01 19:27:58

标签: django graphql shopify-app

我看不到如何为shopify格式化该石墨烯查询。我需要在Django中使用graphene复制这个curl查询:

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

到目前为止,我有:

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'])

此语法对于石墨烯是错误的,但我不知道它的外观如何?一旦我获得了正确格式的数据,我就可以进行请求发布,以便从shopify storefrontapi获得我想要的信息

2 个答案:

答案 0 :(得分:2)

Graphene是用于Python的GraphQL规范的实现,用于创建和执行自己的GraphQL模式。它不是用于向现有GraphQL服务器发出请求的GraphQL客户端,不是。您可以使用任何常规HTTP库(例如requests来调用Shopify API,也可以使用gql之类的东西。一个简单的例子:

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()

答案 1 :(得分:1)

从Shopify Python API 5.1.0版本开始,支持使用Graphql查询Shopify Admin API:

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

Shopify Python API Graphql Docs