1、参考:https://www.shuijingwanwq.com/2022/03/15/6129/ 。在 Altair GraphQL Client 中请求 Shopify’s GraphQL Admin API。最终决定再尝试一下使用 Postman。

2、参考:https://learning.postman.com/docs/sending-requests/supported-api-frameworks/graphql/ 。Querying with GraphQL

3、在请求体中发送 GraphQL 查询。在 Postman 中打开一个新的请求选项卡,并在地址栏中输入您的 GraphQL 端点 URL。从请求方法下拉列表中选择 POST。在 Body 选项卡下,选择 GraphQL body 类型。在查询编辑器中输入您的 GraphQL 查询。需要设置 Headers,Content-Type: application/graphql,X-Shopify-Access-Token: {access_token}、如图1

图1

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

4、使用 GraphQL 变量。在 GraphQL 变量编辑器中输入您的 GraphQL 变量。在查询编辑器中,在 GraphQL 查询中声明 GraphQL 变量。如图2

图2

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

5、要启用自动完成,您需要添加 GraphQL 架构。在 Postman 中创建或导入 GraphQL 架构。因此,需要通过 Altair GraphQL Client 先获取到 GraphQL 架构。查看网络请求,点击重载文档,复制响应参数。如图3

图3

6、粘贴至文件 schema.graphql。其大小为 2 MB 左右。如图4

图4

7、在左侧边栏中选择 API,然后选择 + 以创建新 API。输入 API 的名称和版本。从 Schema type 下拉列表中选择 GraphQL。从 Schema Format 下拉列表中选择 GraphQL SDL。选择 Import。Import local schema files。如图5

图5

8、启用 GraphQL 查询自动完成。从下拉列表中选择您的架构:Shopify’s GraphQL Admin API。Postman 现在将根据新 GraphQL 模式中的数据建议自动完成选项。但是提示错误:incorrert schema. Autocomplete options related to the schema will not work.。如图6

图6

9、参考网址:https://www.npmjs.com/package/graphql-introspection-json-to-sdl ,将格式从 GraphQL 内省 JSON 转换为 GraphQL SDL。

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


    目录: 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 : 无法加载文件 C:\Users\Lenovo\AppData\Roaming\np
m\graphql-introspection-json-to-sdl.ps1,因为在此系统上禁止运行脚本。有关详 细信息,
请参阅 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies
。
所在位置 行:1 字符: 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

执行策略更改
执行策略可帮助你防止执行不信任的脚本。更改执行策略可能会产生安全风险,如
https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies
帮助主题所述。是否要更改执行策略?
[Y] 是(Y)  [A] 全是(A)  [N] 否(N)  [L] 全否(L)  [S] 暂停(S)  [?] 帮助
(默认值为“N”):Y
PS E:\wwwroot\shopify-graphql> graphql-introspection-json-to-sdl schema.json > schema.graphql
PS E:\wwwroot\shopify-graphql> ls


    目录: 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、再次导入 Schema 文件,不再提示错误。自动完成可用。但是,在 Postman 中内省架构需要每次手动导入,不支持基于请求自动更新。如图7

图7

 

 

永夜