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

Request Shopify’s GraphQL Admin API in Postman

Import the Schema file again, and the error is no longer prompted. Autocomplete is available. However, introspecting the schema in Postman requires manual import every time, and it does not support automatic updating based on requests.

1. Reference: Requesting Shopify’s GraphQL Admin API in Altair GraphQL Client. I finally decided to try using Postman again.

2. Reference: https://learning.postman.com/docs/sending-requests/supported-api-frameworks/graphql/ . Querying with GraphQL

3. Send a GraphQL query in the request body. Open a new request tab in Postman and enter your GraphQL endpoint URL in the address bar. Select POST from the request method dropdown list. Under the Body tab, select the GraphQL body type. Enter your GraphQL query in the query editor. You need to set the Headers: Content-Type: application/graphql, X-Shopify-Access-Token: {access_token}, as shown in Figure 1.

Send a GraphQL query in the request body. Open a new request tab in Postman and enter your GraphQL endpoint URL in the address bar. Select POST from the request method dropdown list. Under the Body tab, select the GraphQL body type. Enter your GraphQL query in the query editor. You need to set the Headers: Content-Type: application/graphql, X-Shopify-Access-Token: {access_token}
Figure 1
GraphQL

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

4. Use GraphQL variables. Enter your GraphQL variables in the GraphQL variables editor. In the query editor, declare the GraphQL variables within the GraphQL query, as shown in Figure 2.

Use GraphQL variables. Enter your GraphQL variables in the GraphQL variables editor. In the query editor, declare the GraphQL variables within the GraphQL query
Figure 2
GraphQL

query ($first: Int!){
  products(first: $first) {
    edges {
      node {
        id
        title
      }
    }
  }
}

JSON

{
    "first": 3
}

5. To enable autocomplete, you need to add a GraphQL schema. Create or import a GraphQL schema in Postman. Therefore, you first need to obtain the GraphQL schema via the Altair GraphQL Client. Inspect the network requests, click reload document, and copy the response parameters, as shown in Figure 3.

To enable autocomplete, you need to add a GraphQL schema. Create or import a GraphQL schema in Postman. Therefore, you first need to obtain the GraphQL schema via the Altair GraphQL Client. Inspect the network requests, click reload document, and copy the response parameters
Figure 3

6. Paste it into the file schema.graphql. Its size is about 2 MB, as shown in Figure 4.

Paste it into the file schema.graphql. Its size is about 2 MB
Figure 4

7. Select API in the left sidebar, then select + to create a new API. Enter a name and version for the API. Select GraphQL from the Schema type dropdown list. Select GraphQL SDL from the Schema Format dropdown list. Select Import. Import local schema files, as shown in Figure 5.

Select API in the left sidebar, then select + to create a new API. Enter a name and version for the API. Select GraphQL from the Schema type dropdown list. Select GraphQL SDL from the Schema Format dropdown list. Select Import. Import local schema files
Figure 5

8. Enable GraphQL query autocomplete. Select your schema from the dropdown list: Shopify’s GraphQL Admin API. Postman will now suggest autocomplete options based on the data in the new GraphQL schema. However, an error is prompted: incorrert schema. Autocomplete options related to the schema will not work., as shown in Figure 6.

Enable GraphQL query autocomplete. Select your schema from the dropdown list: Shopify's GraphQL Admin API. Postman will now suggest autocomplete options based on the data in the new GraphQL schema. However, an error is prompted: incorrert schema. Autocomplete options related to the schema will not work.
Figure 6

9. Reference URL: https://www.npmjs.com/package/graphql-introspection-json-to-sdl , convert the format from GraphQL introspection JSON to GraphQL SDL.

Plaintext
PS E:\wwwroot\shopify-graphql> npm install graphql-introspection-json-to-sdl

added 7 packages, and audited 8 packages in 15s

found 0 vulnerabilities
PS E:\wwwroot\shopify-graphql> ls


    Directory: E:\wwwroot\shopify-graphql


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         2022/2/21     14:08                node_modules
-a----         2022/2/21     14:08           4632 package-lock.json
-a----         2022/2/21     14:08             78 package.json
-a----         2022/2/21     13:59        6115414 schema.json


PS E:\wwwroot\shopify-graphql> graphql-introspection-json-to-sdl schema.json > schema.graphql
graphql-introspection-json-to-sdl : Unable to load file C:\Users\Lenovo\AppData\Roaming\np
m\graphql-introspection-json-to-sdl.ps1, because running scripts is disabled on this system. For more information,
see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170
.
Location: Line:1 Character: 1
+ graphql-introspection-json-to-sdl schema.json > schema.graphql
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [],PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess
PS E:\wwwroot\shopify-graphql> get-executionpolicy
Restricted
PS E:\wwwroot\shopify-graphql> set-executionpolicy remotesigned

Execution Policy Change
Execution policies help you prevent execution of untrusted scripts. Changing the execution policy might pose a security risk, as
described in the about_Execution_Policies help topic at
https:/go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the execution policy?
[Y] Yes(Y)  [A] Yes to All(A)  [N] No(N)  [L] No to All(L)  [S] Suspend(S)  [?] Help
(default is "N"):Y
PS E:\wwwroot\shopify-graphql> graphql-introspection-json-to-sdl schema.json > schema.graphql
PS E:\wwwroot\shopify-graphql> ls


    Directory: E:\wwwroot\shopify-graphql


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         2022/2/21     14:08                node_modules
-a----         2022/2/21     14:08           4632 package-lock.json
-a----         2022/2/21     14:08             78 package.json
-a----         2022/2/21     16:10        2015866 schema.graphql
-a----         2022/2/21     16:01        2128569 schema.json


PS E:\wwwroot\shopify-graphql>

10. Import the Schema file again, and the error is no longer prompted. Autocomplete is available. However, introspecting the schema in Postman requires manual import every time, and it does not support automatic updating based on requests, as shown in Figure 7.

Import the Schema file again, and the error is no longer prompted. Autocomplete is available. However, introspecting the schema in Postman requires manual import every time, and it does not support automatic updating based on requests.
Figure 7

 

 

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

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

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

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

关于我  ·  GitHub  ·  邮件联系