sebtheiler
sebtheiler

Reputation: 2557

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

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.

Upvotes: 8

Views: 15986

Answers (2)

Victor Ezekiel
Victor Ezekiel

Reputation: 369

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.

Upvotes: 12

Twan
Twan

Reputation: 41

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

Upvotes: 4

Related Questions