2

我正在尝试向 shopify 的管理 api 发出 graphql 请求。我正在使用官方的 shopify-api-node 库。

我的查询如下

      const response = await shopify.graphql({
      `order(id:"gid://shopify/Order/3138432598213") {
        currentCartDiscountAmountSet {
          shopMoney {
            amount
            currencyCode
          }
        }
      }`
    });

回应是

在此处输入图像描述

据我了解,这应该返回一个带有数据字段和扩展字段的对象(我对查询成本感兴趣)。

我得到的响应只是包含订单。

这是因为我正在查询店面 API 吗?我很困惑,因为我想查询 admin api。根据 shopify 文档,storefront API 下的订单没有 currentCartDiscountAmountSet 字段,而 admin API 下的订单有此字段。我使用 currentCartDiscountAmountSet 字段获得了正确的顺序,因此我假设我正在查询管理 api。但话又说回来,我没有得到数据和扩展?

我的问题是:

  1. 使用 shopify-api-node 时,如何指定是要查询 admin 还是 storefront API?

  2. 为什么我的回复中没有数据和扩展?实际上是因为我正在查询店面api吗?如果是这样,那么为什么我能够成功获得 currentCartDiscountAmountSet?

4

1 回答 1

4

First and foremost shopify-api-node support only GraphQL Admin API, it doesn't support Storefront API.

Second the Storefront API doesn't have access to the order object directly. You can get a customer orders with the storefront api but for that you need username and password to create the token for that user in order to request his orders or to say it simply you won't be doing that with nodejs or to be more precise it's too much work when you can just use the Admin API. (the API docs is really confusing on this one)

So this answer your first question, you can't specify if you are targeting the storefront or admin api, you are stuck with the admin api with this package.

As for your second question I assume that you are passing the wrong ID or something else. Do you pass it properly with id: "gid://shopify/Order/YOUR_ORDER_ID" or you are passing it just with id: "YOUR_ORDER_ID", since the second is wrong. There may be another issue but without knowing the full extend of the flow you will need to debug this on your side.

Here is an image with a sample request that returns what you want. So the request is OK, there is something else that is wrong with your current flow.

enter image description here

于 2020-12-27T13:36:09.727 回答