dotansimha / graphql-code-generator

A tool for generating code based on a GraphQL schema and GraphQL operations (query/mutation/subscription), with flexible support for custom plugins.
https://the-guild.dev/graphql/codegen/
MIT License
10.84k stars 1.33k forks source link

Unable to load a remote Contentful schema. #7837

Open mjrobinson86 opened 2 years ago

mjrobinson86 commented 2 years ago

Describe the bug

Seemingly without an update to graphql-codegen or any of the associated packages / plugins I am using, the process can no longer load the schema. I am not aware of any changes to Contentful in terms of the service so its a bit of a mystery. I made no updates to versions of graphql-codegen (other than trying to update to latest to see if that resolved the issue) so I'm not sure why it has just suddenly stopped working. I have no changes in my source code since the last successful run so I'm out of ideas...

Really found this to be a useful tool in my workflow so it would be a shame to do without it.

To Reproduce

npm run graphql-codegen (config provided bellow).

  1. My GraphQL schema:

Too large, but could be provided if necessary.

  1. My GraphQL operations:

Too many to list, but could be provided if necessary.

  1. My codegen.yml config file:
{
    "schema": [{
        "https://graphql.contentful.com/content/v1/spaces/***/environments/master": {
            "headers": {
                "Authorization": "Bearer ***"
            }
        }
    }],
    "documents": [
        "**/*.{graphql,gql,js,ts,vue}"
    ],
    "generates": {
        "./graphql/types.ts": {
            "plugins": [
                "typescript",
                "typescript-resolvers",
                "typescript-operations"
            ]
        },
        "./graphql/operations.ts": {
            "plugins": [
                "typescript-resolvers",
                "typescript-document-nodes"
            ]
        }
    }
}

Expected behavior

Codegen process should run as it has previously.

Environment:

Additional context

Terminal output error:

Failed to load schema for "./graphql/operations.ts"
        Failed to load schema from https://graphql.contentful.com/content/v1/spaces/***/environments/master:

        this.splice is not a function
        TypeError: this.splice is not a function
    at HeadersList.append (/Users/***/node_modules/cross-undici-fetch/dist/patch-headers-list.js:44:16)
    at Headers.append (/Users/***/node_modules/undici/lib/fetch/headers.js:215:31)
    at fill (/Users/***/node_modules/undici/lib/fetch/headers.js:72:15)
    at new Headers (/Users/***/node_modules/undici/lib/fetch/headers.js:168:5)
    at new Request (/Users/***/node_modules/cross-undici-fetch/dist/create-node-ponyfill.js:84:29)
    at Object.fetch (/Users/***/node_modules/cross-undici-fetch/dist/create-node-ponyfill.js:116:24)
    at defaultAsyncFetch (/Users/***/node_modules/@graphql-tools/url-loader/index.js:41:29)
    at /Users/***/node_modules/@graphql-tools/url-loader/index.js:492:36
    at new ValueOrPromise (/Users/***/node_modules/value-or-promise/build/main/ValueOrPromise.js:14:21)
    at executor (/Users/***/node_modules/@graphql-tools/url-loader/index.js:460:20)

        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.

NOTE: triple stars represent redacted paths / credentials.

bartels commented 2 years ago

I'm seeing the same error trying to load a schema from an apollo-server instance. The error appears to be happening in undici package (sub dependency of codegen). Using version 5.0.0 works fine for me, so some change in that package starting with version 5.1.0 is likely the culprit.

A temporary workaround using yarn resolutions in package.json:

  "resolutions": {
    "undici": "5.0.0"
  }
mjrobinson86 commented 2 years ago

@bartels thanks for this, I had identified where the problem came from but didn't think to provide an override.

After some digging I actually realised I had bumped a version of nuxt3 from rc.1 to rc.3 which resulted in a dedupe of the undici version causing 5.1.1 to be used which then triggered the version mismatch and issue.

I am using npm v8.5.0 so solution was:

"overrides": {
    "@graphql-codegen/cli@2.6.2": {
      "@graphql-tools/apollo-engine-loader@7.2.11": {
        "cross-undici-fetch@0.3.0": {
          "undici": "5.0.0" 
        }
      }
    }
  }

But this effectively mirrors the solution you mentioned.

Thanks again, not the first time I have been caught out with minor version dedupe mismatches...

Not sure if this closes the issue so to speak, but a workaround at least for now.

NOTE: In my case I was in a monorepo context, so this override had to be provided at the monorepo root... bit of a head scratcher at first why this didn't work at the relevant package level but this is currently an undocumented limitation of npm... just to avoid any headaches for anyone else using the setup.

alexbjorlig commented 2 years ago

I'm also on a turborepo npm v8.5.0, but the solutions described here don't help. I still get the this.splice is not a function error 🤔

When I run npm ls undici I get:

❯ npm ls undici
<root>
└─┬ @graphql-codegen/cli@2.6.2
  └─┬ @graphql-tools/github-loader@7.2.5
    └─┬ cross-undici-fetch@0.1.33
      └── undici@5.5.1

With the following in overrides:

    "overrides": {
        "@graphql-codegen/cli@2.6.2": {
            "@graphql-tools/github-loader@7.2.5": {
                "cross-undici-fetch@0.1.33": {
                    "undici": "5.0.0"
                }
            }
        }
    }
Waitak commented 2 years ago

I've tried the override as described above, but get the following error:

npm ERR! Override for @graphql-codegen/cli@^2.6.2 conflicts with direct dependency

Any words of wisdom?

alexbjorlig commented 2 years ago

I managed to hack around by simply adding "undici": "5.0.0" as a dev-dependency..... It does not look nice, but keeps the boat rocking

Waitak commented 2 years ago

Thank you, that did it.