Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error loading schema from URL or GraphQL Endpoint #8012

Open
edramos-97 opened this issue Jul 1, 2022 · 14 comments
Open

Error loading schema from URL or GraphQL Endpoint #8012

edramos-97 opened this issue Jul 1, 2022 · 14 comments
Labels
core Related to codegen core/cli

Comments

@edramos-97
Copy link

edramos-97 commented Jul 1, 2022

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:

graphql-codegen-issue-template@1.0.0
├── @graphql-codegen/add@3.1.1
├── @graphql-codegen/cli@2.6.4
├── @graphql-codegen/typescript-operations@2.4.1
├── @graphql-codegen/typescript@2.5.0
└── graphql@16.5.0

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

@adam-nielsen
Copy link

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.

@zo0m
Copy link

zo0m commented Jul 1, 2022

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

@edramos-97
Copy link
Author

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!

@adam-nielsen
Copy link

adam-nielsen commented Jul 1, 2022

I just found the problem. It's in the undici package. You can fix it temporarily by running:

npm install undici@5.5.1
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.

@saihaj
Copy link
Collaborator

saihaj commented Jul 1, 2022

@edramos-97 can you please try latest version of @graphql-codegen/cli@2.6.4 ?

@edramos-97
Copy link
Author

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.

@franzwilhelm
Copy link

franzwilhelm commented Jul 1, 2022

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

@tylermenezes
Copy link

That worked for me too!

@franzwilhelm if you're using yarn, try adding this to your package.json:

  "resolutions": {
    "undici": "5.5.1"
  }

@franzwilhelm
Copy link

@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

@saihaj
Copy link
Collaborator

saihaj commented Jul 1, 2022

So it is broken in downstream dependency. codegen -> graphql-tools -> cross-undici-fetch

@Destreyf
Copy link

Destreyf commented Jul 1, 2022

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"
   }
}

@vuletuanbt
Copy link

That worked for me too!

@franzwilhelm if you're using yarn, try adding this to your package.json:

  "resolutions": {
    "undici": "5.5.1"
  }

I confirm it works

@ardatan
Copy link
Collaborator

ardatan commented Jul 4, 2022

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.
ardatan/graphql-tools#4559

You can find a detailed explanation there. Thanks for your patience!

@AllanDutra
Copy link

I ran
npm i undici@5.5.1 --save-dev
and then
npm run graphql-codegen
and it worked for me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Related to codegen core/cli
Projects
None yet
Development

No branches or pull requests