GraphQL 代码生成器 - 无法从 http://localhost:8000/graphql 加载架构 - 使用 GET/HEAD 方法的请求不能有正文

use*_*948 7 django graphql graphql-codegen react-query strawberry-graphql

我使用草莓草莓-django-plus构建了一个 GraphQL API ,该 API 使用 Django 托管在 http://localhost:8000/graphql 上。我能够在本地主机页面上使用 GraphiQL 成功与 API 交互。

\n

我现在尝试使用GraphQL 代码生成器和 React 查询从前端访问 API。当我运行命令时yarn graphql-codegen,出现以下错误:

\n
\xe2\x9c\x94 Parse configuration\n  \xe2\x9d\xaf Generate outputs\n    \xe2\x9d\xaf Generate src/generated/graphql.ts\n      \xe2\x9c\x96 Load GraphQL schemas\n        \xe2\x86\x92 Failed to load schema\n        Load GraphQL documents\n        Generate\n    \xe2\x9d\xaf Generate ./graphql.schema.json\n      \xe2\x9c\x96 Load GraphQL schemas\n        \xe2\x86\x92 Failed to load schema\n        Load GraphQL documents\n        Generate\n\n\n Found 2 errors\n\n  \xe2\x9c\x96 ./graphql.schema.json\n    Failed to load schema from http://localhost:8000/graphql:\n\n        Request with GET/HEAD method cannot have body.\n        TypeError: Request with GET/HEAD method cannot have body.\n    at new Request (mydir/.yarn/cache/undici-npm-5.6.0-b513316628-b9052c2cb9.zip/node_modules/\nundici/lib/fetch/request.js:437:13)\n    at Agent.fetch (mydir/.yarn/cache/undici-npm-5.6.0-b513316628-b9052c2cb9.zip/node_modules/\nundici/lib/fetch/index.js:114:21)\n    at fetch (mydir/.yarn/cache/undici-npm-5.6.0-b513316628-b9052c2cb9.zip/node_modules/undici\n/index.js:90:22)\n    at fetch (mydir/.yarn/cache/cross-undici-fetch-npm-0.4.8-4bdc960eeb-7645fde142.zip/node_mo\ndules/cross-undici-fetch/dist/create-node-ponyfill.js:130:16)\n    at fetch (mydir/.yarn/cache/cross-undici-fetch-npm-0.4.8-4bdc960eeb-7645fde142.zip/node_mo\ndules/cross-undici-fetch/dist/create-node-ponyfill.js:125:18)\n    at defaultAsyncFetch (mydir/.yarn/__virtual__/@graphql-tools-url-loader-virtual-1d0d6b13ee\n/0/cache/@graphql-tools-url-loader-npm-7.12.0-ad4affabf8-6db87eec05.zip/node_modules/@graphql-tools/url-loader/cjs/defaultAsyncFetch.js:6:43)\n    at mydir/.yarn/__virtual__/@graphql-tools-url-loader-virtual-1d0d6b13ee/0/cache/@graphql-t\nools-url-loader-npm-7.12.0-ad4affabf8-6db87eec05.zip/node_modules/@graphql-tools/url-loader/cjs/index.js:212:36\n    at new ValueOrPromise (mydir/.yarn/cache/value-or-promise-npm-1.0.11-924f226d8c-13f8f2ef62\n.zip/node_modules/value-or-promise/build/main/ValueOrPromise.js:14:21)\n    at executor (mydir/.yarn/__virtual__/@graphql-tools-url-loader-virtual-1d0d6b13ee/0/cache/\n@graphql-tools-url-loader-npm-7.12.0-ad4affabf8-6db87eec05.zip/node_modules/@graphql-tools/url-loader/cjs/index.js:186:20)\n    at mydir/.yarn/__virtual__/@graphql-tools-url-loader-virtual-1d0d6b13ee/0/cache/@graphql-t\nools-url-loader-npm-7.12.0-ad4affabf8-6db87eec05.zip/node_modules/@graphql-tools/url-loader/cjs/index.js:504:35\n\n        GraphQL Code Generator supports:\n          - ES Modules and CommonJS exports (export as default or named export "schema")\n          - Introspection JSON File\n          - URL of GraphQL endpoint\n          - Multiple files with type definitions (glob expression)\n          - String in config file\n\n        Try to use one of above options and run codegen again.\n    Error: Failed to load schema\n        at loadSchema (mydir/.yarn/__virtual__/@graphql-codegen-cli-virtual-7cdefe09f5/0/cache\n/@graphql-codegen-cli-npm-2.6.4-2c119c03ca-664fbd395f.zip/node_modules/@graphql-codegen/cli/bin.js:507:15)\n        at processTicksAndRejections (node:internal/process/task_queues:96:5)\n        at async mydir/.yarn/__virtual__/@graphql-codegen-cli-virtual-7cdefe09f5/0/cache/@grap\nhql-codegen-cli-npm-2.6.4-2c119c03ca-664fbd395f.zip/node_modules/@graphql-codegen/cli/bin.js:1037:65\n    Error: Failed to load schema\n        at loadSchema (mydir/.yarn/__virtual__/@graphql-codegen-cli-virtual-7cdefe09f5/0/cache\n/@graphql-codegen-cli-npm-2.6.4-2c119c03ca-664fbd395f.zip/node_modules/@graphql-codegen/cli/bin.js:507:15)\n        at processTicksAndRejections (node:internal/process/task_queues:96:5)\n        at async mydir/.yarn/__virtual__/@graphql-codegen-cli-virtual-7cdefe09f5/0/cache/@grap\nhql-codegen-cli-npm-2.6.4-2c119c03ca-664fbd395f.zip/node_modules/@graphql-codegen/cli/bin.js:1037:65\n
Run Code Online (Sandbox Code Playgroud)\n

这是我的codegen.yml文件:

\n
\xe2\x9c\x94 Parse configuration\n  \xe2\x9d\xaf Generate outputs\n    \xe2\x9d\xaf Generate src/generated/graphql.ts\n      \xe2\x9c\x96 Load GraphQL schemas\n        \xe2\x86\x92 Failed to load schema\n        Load GraphQL documents\n        Generate\n    \xe2\x9d\xaf Generate ./graphql.schema.json\n      \xe2\x9c\x96 Load GraphQL schemas\n        \xe2\x86\x92 Failed to load schema\n        Load GraphQL documents\n        Generate\n\n\n Found 2 errors\n\n  \xe2\x9c\x96 ./graphql.schema.json\n    Failed to load schema from http://localhost:8000/graphql:\n\n        Request with GET/HEAD method cannot have body.\n        TypeError: Request with GET/HEAD method cannot have body.\n    at new Request (mydir/.yarn/cache/undici-npm-5.6.0-b513316628-b9052c2cb9.zip/node_modules/\nundici/lib/fetch/request.js:437:13)\n    at Agent.fetch (mydir/.yarn/cache/undici-npm-5.6.0-b513316628-b9052c2cb9.zip/node_modules/\nundici/lib/fetch/index.js:114:21)\n    at fetch (mydir/.yarn/cache/undici-npm-5.6.0-b513316628-b9052c2cb9.zip/node_modules/undici\n/index.js:90:22)\n    at fetch (mydir/.yarn/cache/cross-undici-fetch-npm-0.4.8-4bdc960eeb-7645fde142.zip/node_mo\ndules/cross-undici-fetch/dist/create-node-ponyfill.js:130:16)\n    at fetch (mydir/.yarn/cache/cross-undici-fetch-npm-0.4.8-4bdc960eeb-7645fde142.zip/node_mo\ndules/cross-undici-fetch/dist/create-node-ponyfill.js:125:18)\n    at defaultAsyncFetch (mydir/.yarn/__virtual__/@graphql-tools-url-loader-virtual-1d0d6b13ee\n/0/cache/@graphql-tools-url-loader-npm-7.12.0-ad4affabf8-6db87eec05.zip/node_modules/@graphql-tools/url-loader/cjs/defaultAsyncFetch.js:6:43)\n    at mydir/.yarn/__virtual__/@graphql-tools-url-loader-virtual-1d0d6b13ee/0/cache/@graphql-t\nools-url-loader-npm-7.12.0-ad4affabf8-6db87eec05.zip/node_modules/@graphql-tools/url-loader/cjs/index.js:212:36\n    at new ValueOrPromise (mydir/.yarn/cache/value-or-promise-npm-1.0.11-924f226d8c-13f8f2ef62\n.zip/node_modules/value-or-promise/build/main/ValueOrPromise.js:14:21)\n    at executor (mydir/.yarn/__virtual__/@graphql-tools-url-loader-virtual-1d0d6b13ee/0/cache/\n@graphql-tools-url-loader-npm-7.12.0-ad4affabf8-6db87eec05.zip/node_modules/@graphql-tools/url-loader/cjs/index.js:186:20)\n    at mydir/.yarn/__virtual__/@graphql-tools-url-loader-virtual-1d0d6b13ee/0/cache/@graphql-t\nools-url-loader-npm-7.12.0-ad4affabf8-6db87eec05.zip/node_modules/@graphql-tools/url-loader/cjs/index.js:504:35\n\n        GraphQL Code Generator supports:\n          - ES Modules and CommonJS exports (export as default or named export "schema")\n          - Introspection JSON File\n          - URL of GraphQL endpoint\n          - Multiple files with type definitions (glob expression)\n          - String in config file\n\n        Try to use one of above options and run codegen again.\n    Error: Failed to load schema\n        at loadSchema (mydir/.yarn/__virtual__/@graphql-codegen-cli-virtual-7cdefe09f5/0/cache\n/@graphql-codegen-cli-npm-2.6.4-2c119c03ca-664fbd395f.zip/node_modules/@graphql-codegen/cli/bin.js:507:15)\n        at processTicksAndRejections (node:internal/process/task_queues:96:5)\n        at async mydir/.yarn/__virtual__/@graphql-codegen-cli-virtual-7cdefe09f5/0/cache/@grap\nhql-codegen-cli-npm-2.6.4-2c119c03ca-664fbd395f.zip/node_modules/@graphql-codegen/cli/bin.js:1037:65\n    Error: Failed to load schema\n        at loadSchema (mydir/.yarn/__virtual__/@graphql-codegen-cli-virtual-7cdefe09f5/0/cache\n/@graphql-codegen-cli-npm-2.6.4-2c119c03ca-664fbd395f.zip/node_modules/@graphql-codegen/cli/bin.js:507:15)\n        at processTicksAndRejections (node:internal/process/task_queues:96:5)\n        at async mydir/.yarn/__virtual__/@graphql-codegen-cli-virtual-7cdefe09f5/0/cache/@grap\nhql-codegen-cli-npm-2.6.4-2c119c03ca-664fbd395f.zip/node_modules/@graphql-codegen/cli/bin.js:1037:65\n
Run Code Online (Sandbox Code Playgroud)\n

这个问题涉及类似的问题;但是,我确信我的服务器正在运行,并且该问题中的错误消息是不同的。我还阅读了这个GitHub 问题graphql-code-generator;但是,它不处理从本地主机加载架构。

\n

任何帮助深表感谢。如果需要,我可以分享我的后端代码;但是,我相信问题发生在前端。

\n

小智 11

我也遇到了这个问题。我发现使用http://127.0.0.1:8000/graphql而不是http://localhost:8000/graphql对我有用。


小智 3

有人建议进行临时修复,它对我有用

{
  "resolutions": {
    "undici": "5.5.1"
  }
}
Run Code Online (Sandbox Code Playgroud)

从正在进行的 github 问题中得到它。 https://github.com/dotansimha/graphql-code-generator/issues/8012#issuecomment-1172725253