【问题标题】:How can I run OpenAPI -> GraphQL conversion?如何运行 OpenAPI -> GraphQL 转换?
【发布时间】:2019-06-03 21:05:07
【问题描述】:

我想将 .yaml (OpenAPI) 转换为 GraphQL 架构(即生成 schema.graphql)并找到 2 个可以帮助我的转换器(脚本):

每个都有一个独立的示例,但由于我是 JS 的新手,我无法让它工作。任何人都可以发布完整的说明,我应该运行哪个命令来在我的.yaml 文件上运行这些存储库?

我为repo #1尝试过的事情:

$ git clone https://github.com/yarax/swagger-to-graphql
$ cd swagger-to-graphql/
$ npm i -g swagger-to-graphql
$ npm i -g graphql # to eliminate the error above
$ swagger-to-graphql --swagger=doc.yaml > types.graphql
$ #doc.yaml is my OpenAPI file that I use as the input
$ swagger-to-graphql (master) $ cat types.graphql
Error: Error opening file "/Users/alex/Dev/temp/swagger-to-graphql/$"
ENOENT: no such file or directory, open '/Users/alex/Dev/temp/swagger-to-graphql/$'
    at ReadFileContext.callback (/usr/local/lib/node_modules/swagger-to-graphql/node_modules/json-schema-ref-parser/lib/resolvers/file.js:51:20)
    at FSReqCallback.readFileAfterOpen [as oncomplete] (fs.js:230:13)

Error: ENOENT: no such file or directory, open '/Users/alex/Dev/temp/swagger-to-graphql/$' {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '/Users/klinou/Dev/temp/swagger-to-graphql/$',
  toJSON: [Function: errorToJSON],
  inspect: [Function: errorToString]
}

然后我收到一个错误:

internal/modules/cjs/loader.js:626
    throw err;
    ^

Error: Cannot find module 'graphql'
Require stack:
- /usr/local/lib/node_modules/swagger-to-graphql/lib/index.js
- /usr/local/lib/node_modules/swagger-to-graphql/bin/swagger2graphql
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/usr/local/lib/node_modules/swagger-to-graphql/lib/index.js',
    '/usr/local/lib/node_modules/swagger-to-graphql/bin/swagger2graphql'
  ]

我为 repo #2 尝试过的事情:

$ git clone https://github.com/graphql-binding/graphql-binding-openapi
$ yarn add graphql-binding-openapi
$ cd example/
$ graphql get-schema
# output: project petstore - endpoint default - No changes
# replace the last line .graphqlconfig.yml
# definition: schema.yaml (to use my input file instead of a sample)
$ graphql get-schema
⚠ Error opening file "/Users/alex/Dev/graphql-binding-openapi/example/$"

【问题讨论】:

  • 您看过自述文件中的示例了吗?
  • @Dom 哦,我明白了。
  • @SLaks 你能再看看这个问题吗?我已经添加了一些细节。
  • @Dom 你能再看看这个问题吗?我没有设法运行那个独立的例子。

标签: javascript graphql yarnpkg


【解决方案1】:

我可以建议您使用 github.com/ibm/openapi-to-graphql 吗?如果您的 OAS(或 swagger)格式正确,您可以在几分钟内快速启动并运行 GraphQL 界面。

https://github.com/IBM/openapi-to-graphql/blob/master/README.md 描述了 CLI 的工作原理。

【讨论】:

    【解决方案2】:

    您在全局范围内安装了swagger-to-graphql(使用-g)。

    因此,它的对等依赖项也必须全局安装。

    【讨论】:

    • 这有帮助,但它仍然不会生成最终输出(请参阅编辑)
    猜你喜欢
    • 2020-12-29
    • 1970-01-01
    • 2019-04-20
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 2020-02-23
    相关资源
    最近更新 更多