【问题标题】:How to convert a GraphQL file to a Postman Collection?如何将 GraphQL 文件转换为 Postman 集合?
【发布时间】:2021-01-30 21:54:01
【问题描述】:

我想将 GraphQL 文件转换为 Postman 集合。我尝试使用 JavaScript 库来做到这一点 (https://www.npmjs.com/package/graphql-to-postman)。

但我收到以下错误:

未处理的拒绝 (TypeError):无法设置属性 未定义的'includeDeprecatedFields'

function convert() {
    var postmanJson = fileReader.result,
    fileDownload = require('js-file-download');
    const graphQlToPostman = require('graphql-to-postman');

    const collection = graphQlToPostman.convert(postmanJson);

    fileDownload(
        JSON.stringify(collection),
        'postman collection',
    );
}

这是我使用库的函数。

【问题讨论】:

标签: graphql postman graphql-js converters postman-collection-runner


【解决方案1】:

要将 graphql 转换为 postman 集合,首先您需要 graphql 模式。可以通过在 graphql 服务器端点上运行自省查询来下载 Graphql 模式。这是怎么做的。

  1. 安装 graphql-cli

    npm i -g apollo
    
  2. 从 graphql 服务器下载架构

    apollo schema:download --endpoint=http://localhost:4000/graphql schema.json
    

将架构转换为 graphql 集合

const fs = require('fs')
const converter = require('graphql-to-postman')

const schema = fs.readFileSync('./schema.json')
converter.convert({
    type: 'string',
    data: schema.toString(),
}, {}, async function (error, result) {
    if (error) {
        log.error('Conversion failed')
    } else {
        const outputData = result.output[0].data
        fs.writeFileSync('output-collection.json', JSON.stringify(outputData))
        console.log('Conversion success');
    }
})

【讨论】:

    【解决方案2】:

    我构建了一个易于使用的命令行工具,可让您从 GraphQL 端点自动生成 Postman 集合。 (https://www.npmjs.com/package/graphql-testkit)

    它还提供开箱即用的支持来控制最大深度并为集合中的所有请求添加标头。

    只需执行此操作,在替换端点后,根据您的要求添加或删除标头即可自动生成支持变量的 Postman 集合。

    graphql-testkit \ 
    --endpoint=https://api/spacex.land/graphql\ 
    --header="Authorization:123,x-ws-system-id=10" \
    --maxDepth=4
    

    【讨论】:

      猜你喜欢
      • 2023-02-19
      • 1970-01-01
      • 2017-12-24
      • 2021-02-16
      • 1970-01-01
      • 2018-01-26
      • 2021-11-28
      • 1970-01-01
      • 2013-12-01
      相关资源
      最近更新 更多