代码之家  ›  专栏  ›  技术社区  ›  Davtho1983

Shopify graphQL查询

  •  0
  • Davtho1983  · 技术社区  · 7 年前

    我不知道如何为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'])
    

    这种语法对石墨烯来说是错误的,但我不明白它应该是什么样子?一旦我得到了正确格式的数据,我就可以发布请求,从ShopifyStoreFrontAPI获取我想要的信息

    1 回复  |  直到 7 年前
        1
  •  3
  •   Daniel Rearden    7 年前

    Graphene是Python的GraphQL规范的实现,用于创建和执行自己的GraphQL模式。它是 不 requests ,或者你可以使用 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()
    
        2
  •  1
  •   dave4jr    6 年前

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

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

    Shopify Python API Graphql Docs