GraphQL Code Generator - Failed to load schema from http://localhost:8000/graphql - Request with GET/HEAD method cannot have body
Asked Answered
B

2

7

I've built a GraphQL API using strawberry and strawberry-django-plus that is hosted on http://localhost:8000/graphql using Django. I am able to successfully interact with the API using GraphiQL on the localhost page.

I'm now trying to access the API from my frontend using GraphQL Code Generator and React Query. When I run the command yarn graphql-codegen, the following error occurs:

✔ Parse configuration
  ❯ Generate outputs
    ❯ Generate src/generated/graphql.ts
      ✖ Load GraphQL schemas
        → Failed to load schema
        Load GraphQL documents
        Generate
    ❯ Generate ./graphql.schema.json
      ✖ Load GraphQL schemas
        → Failed to load schema
        Load GraphQL documents
        Generate


 Found 2 errors

  ✖ ./graphql.schema.json
    Failed to load schema from http://localhost:8000/graphql:

        Request with GET/HEAD method cannot have body.
        TypeError: Request with GET/HEAD method cannot have body.
    at new Request (mydir/.yarn/cache/undici-npm-5.6.0-b513316628-b9052c2cb9.zip/node_modules/
undici/lib/fetch/request.js:437:13)
    at Agent.fetch (mydir/.yarn/cache/undici-npm-5.6.0-b513316628-b9052c2cb9.zip/node_modules/
undici/lib/fetch/index.js:114:21)
    at fetch (mydir/.yarn/cache/undici-npm-5.6.0-b513316628-b9052c2cb9.zip/node_modules/undici
/index.js:90:22)
    at fetch (mydir/.yarn/cache/cross-undici-fetch-npm-0.4.8-4bdc960eeb-7645fde142.zip/node_mo
dules/cross-undici-fetch/dist/create-node-ponyfill.js:130:16)
    at fetch (mydir/.yarn/cache/cross-undici-fetch-npm-0.4.8-4bdc960eeb-7645fde142.zip/node_mo
dules/cross-undici-fetch/dist/create-node-ponyfill.js:125:18)
    at defaultAsyncFetch (mydir/.yarn/__virtual__/@graphql-tools-url-loader-virtual-1d0d6b13ee
/0/cache/@graphql-tools-url-loader-npm-7.12.0-ad4affabf8-6db87eec05.zip/node_modules/@graphql-tools/url-loader/cjs/defaultAsyncFetch.js:6:43)
    at mydir/.yarn/__virtual__/@graphql-tools-url-loader-virtual-1d0d6b13ee/0/cache/@graphql-t
ools-url-loader-npm-7.12.0-ad4affabf8-6db87eec05.zip/node_modules/@graphql-tools/url-loader/cjs/index.js:212:36
    at new ValueOrPromise (mydir/.yarn/cache/value-or-promise-npm-1.0.11-924f226d8c-13f8f2ef62
.zip/node_modules/value-or-promise/build/main/ValueOrPromise.js:14:21)
    at executor (mydir/.yarn/__virtual__/@graphql-tools-url-loader-virtual-1d0d6b13ee/0/cache/
@graphql-tools-url-loader-npm-7.12.0-ad4affabf8-6db87eec05.zip/node_modules/@graphql-tools/url-loader/cjs/index.js:186:20)
    at mydir/.yarn/__virtual__/@graphql-tools-url-loader-virtual-1d0d6b13ee/0/cache/@graphql-t
ools-url-loader-npm-7.12.0-ad4affabf8-6db87eec05.zip/node_modules/@graphql-tools/url-loader/cjs/index.js:504:35

        GraphQL Code Generator supports:
          - ES Modules and CommonJS exports (export as default or named export "schema")
          - Introspection JSON File
          - URL of GraphQL endpoint
          - Multiple files with type definitions (glob expression)
          - String in config file

        Try to use one of above options and run codegen again.
    Error: Failed to load schema
        at loadSchema (mydir/.yarn/__virtual__/@graphql-codegen-cli-virtual-7cdefe09f5/0/cache
/@graphql-codegen-cli-npm-2.6.4-2c119c03ca-664fbd395f.zip/node_modules/@graphql-codegen/cli/bin.js:507:15)
        at processTicksAndRejections (node:internal/process/task_queues:96:5)
        at async mydir/.yarn/__virtual__/@graphql-codegen-cli-virtual-7cdefe09f5/0/cache/@grap
hql-codegen-cli-npm-2.6.4-2c119c03ca-664fbd395f.zip/node_modules/@graphql-codegen/cli/bin.js:1037:65
    Error: Failed to load schema
        at loadSchema (mydir/.yarn/__virtual__/@graphql-codegen-cli-virtual-7cdefe09f5/0/cache
/@graphql-codegen-cli-npm-2.6.4-2c119c03ca-664fbd395f.zip/node_modules/@graphql-codegen/cli/bin.js:507:15)
        at processTicksAndRejections (node:internal/process/task_queues:96:5)
        at async mydir/.yarn/__virtual__/@graphql-codegen-cli-virtual-7cdefe09f5/0/cache/@grap
hql-codegen-cli-npm-2.6.4-2c119c03ca-664fbd395f.zip/node_modules/@graphql-codegen/cli/bin.js:1037:65

Here is my codegen.yml file:

overwrite: true
schema: "http://localhost:8000/graphql"
documents: "src/**/*.graphql"
generates:
  src/generated/graphql.ts:
    plugins:
      - "typescript"
      - "typescript-operations"
  ./graphql.schema.json:
    plugins:
      - "introspection"

This question deals with a similar problem; however, I am sure that my server is running and the error message in that question is different. I have also read this GitHub issue on graphql-code-generator; however, it does not deal with loading the schema from localhost.

Any help is much appreciated. I can share my backend code if needed; however, I believe the issue is occurring in the frontend.

Bernardo answered 2/7, 2022 at 22:17 Comment(6)
are you able to check if codegen is sending a POST or a GET request?Amphibian
@Amphibian From the error message, I'd assume that it's sending a GET request. However, the GET request is never actually reaching my server as it's simply failing as it attempts to sendBernardo
Experiencing the same now. My localhost is serving the GraphiQL interface just fine, showing the correct schema and all, but the code-generator script shows the same as yours. Funny thing is, it worked as expected a couple days ago. I hope it's not a faulty npm package.Slicer
What I did for now is download the SDL schema from Apollo Studio and change the codegen.yaml config file to generate from the schema file instead of fetching it. This error makes no sense and I'm pretty sure it's a dependency problem. Hope you figure it out soon.Slicer
if you can post a small reproductions somewhere I'll be happy to take a look 😊Amphibian
Running into the same problems here, on a relatively fresh Vue project running on an Apollo mock server. Generating from local file works.Navigable
S
11

I also encountered this issue. I found that using http://127.0.0.1:8000/graphql instead of http://localhost:8000/graphql worked for me.

Submission answered 12/1, 2023 at 6:23 Comment(0)
B
4

A temporary fix has been suggested, and it has worked for me

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

Got it from the github issue that is going on. https://github.com/dotansimha/graphql-code-generator/issues/8012#issuecomment-1172725253

Besotted answered 4/7, 2022 at 4:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.