graphql-code-generator
graphql-code-generator copied to clipboard
Error loading schema from URL or GraphQL Endpoint
Describe the bug
When attempting to run the code generator with a schema variable that is a URL it fail with the following error:
Failed to load schema from http://my-graphql-api.com/graphql: Request with GET/HEAD method cannot have body.
Stack Trace:
TypeError: Request with GET/HEAD method cannot have body.
at new Request (graphql-code-generator-issue-sandbox-template\node_modules\undici\lib\fetch\request.js:437:13)
at Agent.fetch (graphql-code-generator-issue-sandbox-template\node_modules\undici\lib\fetch\index.js:114:21)
at fetch (graphql-code-generator-issue-sandbox-template\node_modules\undici\index.js:90:22)
at fetch (graphql-code-generator-issue-sandbox-template\node_modules\cross-undici-fetch\dist\create-node-ponyfill.js:130:16)
at fetch (graphql-code-generator-issue-sandbox-template\node_modules\cross-undici-fetch\dist\create-node-ponyfill.js:125:18)
at defaultAsyncFetch (graphql-code-generator-issue-sandbox-template\node_modules\@graphql-tools\url-loader\cjs\defaultAsyncFetch.js:6:43)
at graphql-code-generator-issue-sandbox-template\node_modules\@graphql-tools\url-loader\cjs\index.js:212:36
at new ValueOrPromise (graphql-code-generator-issue-sandbox-template\node_modules\value-or-promise\build\main\ValueOrPromise.js:14:21)
at executor (graphql-code-generator-issue-sandbox-template\node_modules\@graphql-tools\url-loader\cjs\index.js:186:20)
at graphql-code-generator-issue-sandbox-template\node_modules\@graphql-tools\url-loader\cjs\index.js:504:35
Your Example Website or App
https://github.com/edramos-97/graphql-code-generator-issue-sandbox-template
Steps to Reproduce the Bug or Issue
Execute npm install followed by npm run generate
See error "Failed to load schema from http://my-graphql-api.com/graphql: Request with GET/HEAD method cannot have body."
Expected behavior
As a User, I expected the code generator to support loading the schema from a URL/GraphQL endpoint but I'm seeing a fatal error that prevent the schema from being loaded
Screenshots or Videos
No response
Platform
- OS: Windows 10
- PowerShell: 7.2.5
- NodeJS: 16.13.2
- npm: 8.5.5
npm tree as of reporting:
[email protected] ├── @graphql-codegen/[email protected] ├── @graphql-codegen/[email protected] ├── @graphql-codegen/[email protected] ├── @graphql-codegen/[email protected] └── [email protected]
Codegen Config File
schema: http://my-graphql-api.com/graphql documents: './src/**/*.tsx' generates: ./graphql/generated.ts: plugins: - typescript - typescript-operations - typescript-react-query config: fetcher: fetch
# Copied from the guide https://www.graphql-code-generator.com/docs/guides/react
Additional context
No response
I just ran into this problem too. I edited node_modules/undici/lib/fetch/request.js to dump the request and it came back with request.method set to GET and inputBody as:
{
stream: ReadableStream { locked: false, state: 'readable', supportsBYOB: false },
source: '{"query":"query IntrospectionQuery {\\n __schema...
I'm not sure where in the code exactly it's generating this request, but it does seem to be trying to do a HTTP GET with a body, which is not permitted.
Faced with the same issue, reinstalled packages in project and got the same error. After reverting to old package-lock.json it works good, so I think there is a bug in a minor version of one of packages
I believe I also tried with version 2.0.0 of @graphql-codegen/cli and the problem persisted which would point to a dependency issue but I might have missed something. Tomorrow I'll do some further testing and debugging.
Thanks for your feedback!
I just found the problem. It's in the undici package. You can fix it temporarily by running:
npm install [email protected]
npm remove undici
This has the effect of reverting the dependent version from 5.6.0 (broken) to 5.5.1 (previous release, working), without leaving it in your package.json.
5.5.1 is the previous version (released 18 days ago), it only broke in the version following this (5.6.0) released 11 hours ago.
After doing this I was able to run the code generator again successfully.
@edramos-97 can you please try latest version of @graphql-codegen/[email protected] ?
Hi @saihaj that's the one I was using initially and the same as in the bug report (Platform section). I only tried to downgrade @graphql-codegen/cli to check if it could have been a bug introduced in one of the latest versions.
Experiencing the exact same issue, but the undici reinstall does not work for me as you suggested @adam-nielsen. I have graphql-codegen running in multiple projects, but this new project is the only one I'm experiencing issues with. Not sure how to resolve it,as I've tried to force installing the same versions of graphql-codegen that I have in my other projects already
EDIT: undici forced to 5.5.1 works
That worked for me too!
@franzwilhelm if you're using yarn, try adding this to your package.json:
"resolutions": {
"undici": "5.5.1"
}
@tylermenezes thank you it worked! 🎉 I only added it to the sub-package of my project. So..
To yarn workspace users: you need to add resolutions to the root package.json as well
So it is broken in downstream dependency. codegen -> graphql-tools -> cross-undici-fetch
For people using NPM, you can use an overrides field in your package.json.
It requires npm 8.3 or later.
Mine looks like this:
{
...
"dependencies": ...
"devDependencies": ...
"overrides": {
"undici": "5.5.1"
}
}
That worked for me too!
@franzwilhelm if you're using
yarn, try adding this to yourpackage.json:"resolutions": { "undici": "5.5.1" }
I confirm it works
Hi!
Sorry for inconvenience!
We found out that the regression is caused by a change in undici that prevents us to use a Request object as a parameter of fetch just like cross-undici-fetch does internally.
So we released a new version of cross-undici-fetch that pins undici to 5.5.1 and we are now about to cut another release for @graphql-tools/url-loader with a latest cross-undici-fetch.
https://github.com/ardatan/graphql-tools/pull/4559
You can find a detailed explanation there. Thanks for your patience!
I ran
npm i [email protected] --save-dev
and then
npm run graphql-codegen
and it worked for me!