没有不值得去解决的问题,也没有不值得去学习的技术!

Request Shopify’s GraphQL Admin API With Altair GraphQL Client

Reference: https://shopify.dev/api/admin/getting-started#use-curl . If you are using an HTTP client such as Postman or Insomnia, you must set Content-Type to application/json instead of application/graphql. Reset the Headers to Content-Type: application/json. The response is still 404. After switching to a live store, the response is 200

1. Reference: https://shopify.dev/api/admin-graphql#authentication . All GraphQL Admin API queries require a valid Shopify access token.

2. Open the admin dashboard – Apps – Develop apps – Create a custom app. See Figure 1.

Open the admin dashboard - Apps - Develop apps - Create a custom app
Figure 1

3. Enter the app name, select the app developer, and create the app. See Figure 2.

Enter the app name, select the app developer, and create the app
Figure 2

4. Configure the Admin API scopes. See Figure 3.

Configure the Admin API scopes
Figure 3

5. App development – Shopify access token – Configuration – Admin API integration – Select access scopes – Save. See Figure 4.

App development - Shopify access token - Configuration - Admin API integration - Select access scopes - Save
Figure 4

6. API credentials: You do not have any access tokens yet. To obtain an Admin API access token, install the app. Install the app. See Figure 5.

API credentials: You do not have any access tokens yet. To obtain an Admin API access token, install the app. Install the app
Figure 5

7. API credentials – Admin API access token. To protect your data, your Admin API token can only be shown once. Copy your Admin API access token and save it in a secure location. See Figure 6.

API credentials - Admin API access token. To protect your data, your Admin API token can only be shown once. Copy your Admin API access token and save it in a secure location
Figure 6

8. Reference: https://shopify.dev/api/admin-graphql#endpoints . Execute GraphQL queries by sending a POST HTTP request to the endpoint. Retrieve the IDs and titles of the three most recently added products. In Altair GraphQL Client, open a new window and enter the URL: https://xxx.myshopify.com/admin/api/2022-01/graphql.json . A login dialog box appears, as shown in Figure 7.

Reference: https://shopify.dev/api/admin-graphql#endpoints . Execute GraphQL queries by sending a POST HTTP request to the endpoint. Retrieve the IDs and titles of the three most recently added products. In Altair GraphQL Client, open a new window and enter the URL: https://xxx.myshopify.com/admin/api/2022-01/graphql.json . A login dialog box appears
Figure 7

9. Set the Headers: Content-Type: application/graphql, X-Shopify-Access-Token: {access_token}. See Figure 8.

Set the Headers: Content-Type: application/graphql, X-Shopify-Access-Token: {access_token}
Figure 8

10. After setting the query, the response is 404. Inspecting the network requests reveals a 301 redirect to /admin/auth/login first, followed by a 302 redirect to /admin. See Figure 9.

After setting the query, the response is 404. Inspecting the network requests reveals a 301 redirect to /admin/auth/login first, followed by a 302 redirect to /admin
Figure 9
GraphQL

  {
    products(first: 3) {
      edges {
        node {
          id
          title
        }
      }
    }
  }

11. However, when executing the query in Git Bash, the response is 200. See Figure 10.

However, when executing the query in Git Bash, the response is 200
Figure 10
Plaintext
$ curl -X POST \
>   https://xxx.myshopify.com/admin/api/2022-01/graphql.json \
>   -H 'Content-Type: application/graphql' \
>   -H 'X-Shopify-Access-Token: shpat_' \
>   -d '
>   {
>     products(first: 3) {
>       edges {
>         node {
>           id
>           title
>         }
>       }
>     }
>   }
>   '
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   621    0   503  100   118    493    115  0:00:01  0:00:01 --:--:--   610{"data":{"products":{"edges":[{"node":{"id":"gid:\/\/shopify\/Product\/7560274542827","title":"满送优惠测试"}},{"node":{"id":"gid:\/\/shopify\/Product\/7560295743723","title":"商品A"}},{"node":{"id":"gid:\/\/shopify\/Product\/7560318976235","title":"Apple iPhone 12 (A2404) 128GB 白色 支持移动联通电信5G 双卡双待手机"}}]}},"extensions":{"cost":{"requestedQueryCost":5,"actualQueryCost":5,"throttleStatus":{"maximumAvailable":1000.0,"currentlyAvailable":995,"restoreRate":50.0}}}}



12. Reference: https://shopify.dev/api/admin/getting-started#use-curl . If you are using an HTTP client such as Postman or Insomnia, you must set Content-Type to application/json instead of application/graphql. Reset the Headers to Content-Type: application/json. The response is still 404. After switching to a live store, the response is 200. See Figure 11.

Reference: https://shopify.dev/api/admin/getting-started#use-curl . If you are using an HTTP client such as Postman or Insomnia, you must set Content-Type to application/json instead of application/graphql. Reset the Headers to Content-Type: application/json. The response is still 404. After switching to a live store, the response is 200
Figure 11
GraphQL

{
  products(first: 3) {
    edges {
      node {
        id
        title
      }
    }
  }
}

JSON

{
  "data": {
    "products": {
      "edges": [
        {
          "node": {
            "id": "gid://shopify/Product/6944715342030",
            "title": "commercial kitchen quality"
          }
        },
        {
          "node": {
            "id": "gid://shopify/Product/6944715636942",
            "title": "Easter Photographing Dress-up Acessories"
          }
        },
        {
          "node": {
            "id": "gid://shopify/Product/6944715702478",
            "title": "Perfect Pet Hair Remover"
          }
        }
      ]
    }
  },
  "extensions": {
    "cost": {
      "requestedQueryCost": 5,
      "actualQueryCost": 5,
      "throttleStatus": {
        "maximumAvailable": 1000,
        "currentlyAvailable": 995,
        "restoreRate": 50
      }
    }
  }
}

13. When setting the Headers to Content-Type: application/graphql, an error occurs: Parse error on \”query\” (STRING) at [1, 2]. See Figure 12.

When setting the Headers to Content-Type: application/graphql, an error occurs: Parse error on \"query\" (STRING) at [1, 2]
Figure 12
JSON

{
  "errors": [
    {
      "message": "Parse error on \"query\" (STRING) at [1, 2]",
      "locations": [
        {
          "line": 1,
          "column": 2
        }
      ]
    }
  ]
}

14. In Postman, neither of the two stores redirects. The trial store responds 200, and the live store responds 200. The trial store is accessible, and the live store is accessible. This matches expectations. See Figure 12.

In Postman, neither of the two stores redirects. The trial store responds 200, and the live store responds 200. The trial store is accessible, and the live store is accessible. This matches expectations
Figure 12

Response from the trial store

JSON

{
    "data": {
        "products": {
            "edges": [
                {
                    "node": {
                        "id": "gid://shopify/Product/7560274542827",
                        "title": "满送优惠测试"
                    }
                },
                {
                    "node": {
                        "id": "gid://shopify/Product/7560295743723",
                        "title": "商品A"
                    }
                },
                {
                    "node": {
                        "id": "gid://shopify/Product/7560318976235",
                        "title": "Apple iPhone 12 (A2404) 128GB 白色 支持移动联通电信5G 双卡双待手机"
                    }
                }
            ]
        }
    },
    "extensions": {
        "cost": {
            "requestedQueryCost": 5,
            "actualQueryCost": 5,
            "throttleStatus": {
                "maximumAvailable": 1000.0,
                "currentlyAvailable": 995,
                "restoreRate": 50.0
            }
        }
    }
}

Response from the live store

JSON

{
    "data": {
        "products": {
            "edges": [
                {
                    "cursor": "eyJsYXN0X2lkIjo2OTQ0NzE1MzQyMDMwLCJsYXN0X3ZhbHVlIjoiNjk0NDcxNTM0MjAzMCJ9",
                    "node": {
                        "id": "gid://shopify/Product/6944715342030",
                        "title": "commercial kitchen quality"
                    }
                }
            ],
            "pageInfo": {
                "hasNextPage": true,
                "hasPreviousPage": false
            }
        }
    },
    "extensions": {
        "cost": {
            "requestedQueryCost": 3,
            "actualQueryCost": 3,
            "throttleStatus": {
                "maximumAvailable": 1000.0,
                "currentlyAvailable": 997,
                "restoreRate": 50.0
            }
        }
    }
}

15. In Git Bash, the trial store is accessible, and the live store is accessible. This matches expectations.
Response from the trial store

Plaintext
$ curl -X POST   https://xxx.myshopify.com/admin/api/2022-01/graphql.json   -H 'Content-Type: application/graphql'   -H 'X-Shopify-Access-Token: shpat_'   -d '
  {
    products(first: 3) {
      edges {
        node {
          id
          title
        }
      }
    }
  }
  '
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   621    0   503  100   118    479    112  0:00:01  0:00:01 --:--:--   593{"data":{"products":{"edges":[{"node":{"id":"gid:\/\/shopify\/Product\/7560274542827","title":"满送优惠测试"}},{"node":{"id":"gid:\/\/shopify\/Product\/7560295743723","title":"商品A"}},{"node":{"id":"gid:\/\/shopify\/Product\/7560318976235","title":"Apple iPhone 12 (A2404) 128GB 白色 支持移动联通电信5G 双卡双待手机"}}]}},"extensions":{"cost":{"requestedQueryCost":5,"actualQueryCost":5,"throttleStatus":{"maximumAvailable":1000.0,"currentlyAvailable":995,"restoreRate":50.0}}}}



Response from the live store

Plaintext

$ curl -X POST \
>   https://fuyan666.myshopify.com/admin/api/2022-01/graphql.json \
>   -H 'Content-Type: application/graphql' \
>   -H 'X-Shopify-Access-Token: shpat_' \
>   -d '
>   {
>     products(first: 3) {
>       edges {
>         node {
>           id
>           title
>         }
>       }
>     }
>   }
>   '
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   604    0   486  100   118    538    130 --:--:-- --:--:-- --:--:--   670{"data":{"products":{"edges":[{"node":{"id":"gid:\/\/shopify\/Product\/6944715342030","title":"commercial kitchen quality"}},{"node":{"id":"gid:\/\/shopify\/Product\/6944715636942","title":"Easter Photographing Dress-up Acessories"}},{"node":{"id":"gid:\/\/shopify\/Product\/6944715702478","title":"Perfect Pet Hair Remover"}}]}},"extensions":{"cost":{"requestedQueryCost":5,"actualQueryCost":5,"throttleStatus":{"maximumAvailable":1000.0,"currentlyAvailable":995,"restoreRate":50.0}}}}



16. In Postman and Git Bash, both the trial store and the live store are accessible. However, in Altair GraphQL Client, the trial store is inaccessible, and only the live store is accessible. I initially suspect this is related to manually entering the account credentials to log in when calling the trial store URL. Manual login should not be required.

 

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系