2
votes

I am trying to make a graphql request to shopify's admin api. I am using the official shopify-api-node library.

My query is as follows

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

And the response is

enter image description here

From my understanding, this should return an object with a data field and an extensions field (I'm interested in query cost).

The response I get is simply contains order.

Is this because I'm querying storefront API? I'm confused because I want to query admin api. According to shopify docs, order under storefront API does not have the field currentCartDiscountAmountSet, whereas order under admin API does have this field. I am getting the correct order with the field currentCartDiscountAmountSet, and so I presume that I'm querying admin api. But then again, I'm not getting data and extensions?

My questions are:

  1. When using shopify-api-node, how to I specify if I want to query admin or storefront API?

  2. Why am I not getting data and extensions in my response? Is it actually because I'm querying storefront api? If so, then why am I successfully able to get currentCartDiscountAmountSet?

1

1 Answers

4
votes

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