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.88k stars 1.34k forks source link

Updating typescript-operations from 1.9.1 to 1.12.1 broke our build #3445

Closed 0xClpz closed 4 years ago

0xClpz commented 4 years ago

Describe the bug when running the graphql code generator we end up with the following output

  ✔ Parse configuration
  ❯ Generate outputs
    ❯ Generate packages/web/src/graphql/types.codegen.tsx
      ✔ Load GraphQL schemas
      ✔ Load GraphQL documents
      ✖ Generate
        → Cannot read property 'definitions' of undefined
    ✔ Generate packages/web/schema.json

 Found 1 error

  ✖ packages/web/src/graphql/types.codegen.tsx
    TypeError: Cannot read property 'definitions' of undefined
        at /Users/antonio/dev/shippr/node_modules/graphql/utilities/concatAST.js:21:18
        at Array.flatMap (<anonymous>)
        at flatMap (/Users/antonio/dev/shippr/node_modules/graphql/polyfills/flatMap.js:13:24)
        at Object.concatAST (/Users/antonio/dev/shippr/node_modules/graphql/utilities/concatAST.js:20:39)
        at Object.plugin (/Users/antonio/dev/shippr/node_modules/@graphql-codegen/typescript-operations/index.cjs.js:107:28)
        at executePlugin (/Users/antonio/dev/shippr/node_modules/@graphql-codegen/core/index.cjs.js:50:41)
        at /Users/antonio/dev/shippr/node_modules/@graphql-codegen/core/index.cjs.js:106:30
        at Array.map (<anonymous>)
        at Object.codegen (/Users/antonio/dev/shippr/node_modules/@graphql-codegen/core/index.cjs.js:96:54)
        at processTicksAndRejections (internal/process/task_queues.js:93:5)
        at async process (/Users/antonio/dev/shippr/node_modules/@graphql-codegen/cli/bin.js:783:56)
        at async Promise.all (index 0)
        at async /Users/antonio/dev/shippr/node_modules/@graphql-codegen/cli/bin.js:790:37
        at async Task.task (/Users/antonio/dev/shippr/node_modules/@graphql-codegen/cli/bin.js:582:17)
    TypeError: Cannot read property 'definitions' of undefined
        at /Users/antonio/dev/shippr/node_modules/graphql/utilities/concatAST.js:21:18
        at Array.flatMap (<anonymous>)
        at flatMap (/Users/antonio/dev/shippr/node_modules/graphql/polyfills/flatMap.js:13:24)
        at Object.concatAST (/Users/antonio/dev/shippr/node_modules/graphql/utilities/concatAST.js:20:39)
        at Object.plugin (/Users/antonio/dev/shippr/node_modules/@graphql-codegen/typescript-operations/index.cjs.js:107:28)
        at executePlugin (/Users/antonio/dev/shippr/node_modules/@graphql-codegen/core/index.cjs.js:50:41)
        at /Users/antonio/dev/shippr/node_modules/@graphql-codegen/core/index.cjs.js:106:30
        at Array.map (<anonymous>)
        at Object.codegen (/Users/antonio/dev/shippr/node_modules/@graphql-codegen/core/index.cjs.js:96:54)
        at processTicksAndRejections (internal/process/task_queues.js:93:5)
        at async process (/Users/antonio/dev/shippr/node_modules/@graphql-codegen/cli/bin.js:783:56)
        at async Promise.all (index 0)
        at async /Users/antonio/dev/shippr/node_modules/@graphql-codegen/cli/bin.js:790:37
        at async Task.task (/Users/antonio/dev/shippr/node_modules/@graphql-codegen/cli/bin.js:582:17)

Something went wrong
error Command failed with exit code 1.

To Reproduce

When running codegen on it, it crashes, reverting to 1.9.1 fixes it

  1. My GraphQL schema:

  2. My GraphQL operations:

  3. My codegen.yml config file:

schema: packages/backend/src/graphql/schema.graphql
documents:
  - packages/web/src/**/*.ts
  - packages/web/src/**/*.tsx
overwrite: true
generates:
  packages/web/src/graphql/types.codegen.tsx:
    plugins:
      - typescript
      - typescript-operations
      - typescript-resolvers
      - typescript-react-apollo
    config:
      withHooks: true
      withComponent: false
      withHOC: false
  packages/web/schema.json:
    plugins:
      - introspection

Expected behavior

Environment:

Additional context I can't copy paste my graphql schema for privacy reasons, let me know if you want me to try and get a minimal reproduction up

dotansimha commented 4 years ago

@ardatan can you please take a look? it seems like an issue with toolkit?

henry-young commented 4 years ago

I've had a similar issue upgrading from 1.8.3 to 1.12.1. However I can generate code but it doesn't generate the import { ExampleFragmentDoc } from './ExampleFragment.generated in the generated query file. See below my codegen.yml config.

overwrite: true
schema:
  - "schema.graphql"
config: {}
documents:
  - "src/**/*.graphql"
generates:
  src/graphql-types.ts:
    - typescript
  src/:
    preset: near-operation-file
    presetConfig:
      extension: .generated.tsx
      baseTypesPath: graphql-types.ts
    config:
      noNamespaces: true
      withHooks: true
      withHOC: false
      hooksImportFrom: react-apollo
    plugins:
      - add: '/* eslint-disable import/first */'
      - typescript-operations
      - typescript-react-apollo
  src/introspection-result.ts:
    plugins:
      - fragment-matcher
require: []
ardatan commented 4 years ago

@henry-young I think your issue is different. Let's track yours there https://github.com/dotansimha/graphql-code-generator/issues/3259

@0xClpz Could you change versions of all codegen packages to 1.12.1 without ^ and ~, and run yarn install or npm install to install them? So we can avoid the version mismatch.

mribichich commented 4 years ago

I was getting exactly the same error, but now I can confirm upgrading dependencies fixed it for me

henry-young commented 4 years ago

@henry-young I think your issue is different. Let's track yours there #3259

@0xClpz Could you change versions of all codegen packages to 1.12.1 without ^ and ~, and run yarn install or npm install to install them? So we can avoid the version mismatch.

@ardatan Yes you are correct.

elarous commented 4 years ago

I'm having the same issue, but downgrading from 1.12.2 to 1.12.1 didn't fix it for me

johnnyBira commented 4 years ago

Same issue, downgrading to 1.12.1 fixed it for me.

cosmocoder commented 4 years ago

We are also getting the exact same error message as the OP, and setting the version for all codegen packages to 1.12.2, 1.12.1 or 1.12.0 didn't fix it. The latest version that works is 1.11.2.

dotansimha commented 4 years ago

@johnnyBira @elarouss @cosmocoder can you please make sure you have the latest versions of graphql-codegen and graphql-toolkit? If it's still happens, can you please share a reproduction in a repo/sandbox? Thanks!

cosmocoder commented 4 years ago

We are using the following codegen packages:

@graphql-codegen/cli: "1.12.2", @graphql-codegen/fragment-matcher: "1.12.2", @graphql-codegen/introspection: "1.12.2", @graphql-codegen/typescript": "1.12.2", @graphql-codegen/typescript-operations": "1.12.2", @graphql-codegen/typescript-react-apollo": "1.12.2"

Using the latest version for those packages didn't solve it for us.

dotansimha commented 4 years ago

Thanks @cosmocoder , can you please share a reproduction I can try locally?

cosmocoder commented 4 years ago

@dotansimha Here is the minimal setup that showcases the error we are getting. After installing the packages, run the graphql:generate script in package.json, and you should get the error message.

codegen-sandbox.zip

ardatan commented 4 years ago

@cosmocoder As I see in package.json, versions of codegen related packages mismatch

    "@graphql-codegen/cli": "^1.11.2",
    "@graphql-codegen/fragment-matcher": "1.12.2",
    "@graphql-codegen/introspection": "1.12.2",
    "@graphql-codegen/typescript": "1.12.2",
    "@graphql-codegen/typescript-operations": "1.11.2",
    "@graphql-codegen/typescript-react-apollo": "1.11.2"

So we asked you try latest versions like below;

    "@graphql-codegen/cli": "1.12.2",
    "@graphql-codegen/fragment-matcher": "1.12.2",
    "@graphql-codegen/introspection": "1.12.2",
    "@graphql-codegen/typescript": "1.12.2",
    "@graphql-codegen/typescript-operations": "1.12.2",
    "@graphql-codegen/typescript-react-apollo": "1.12.2"
cosmocoder commented 4 years ago

@ardatan apologies for uploading the wrong package.json. I updated the version numbers in the minimal setup, and the error did go away there. But I am still getting the error in our main project with the latest versions. Clearing node_modules and re-installing the packages didn't help also. Any ideas on what might cause this? We are using the same packages in the main project as in the minimal setup.

ardatan commented 4 years ago

@cosmocoder Could you try to delete lock file and install dependencies? I recommend you to avoid ^ or ~ in package.json as much as possible.

cosmocoder commented 4 years ago

Yes, I had tried that too, but no luck.

NomadXD commented 4 years ago

@ardatan @dotansimha I got the exactly same issue. My devDependencies are as follows.

"devDependencies": {
    "@graphql-codegen/add": "1.12.2",
    "@graphql-codegen/typescript": "1.12.2",
    "@graphql-codegen/typescript-operations": "1.12.2",
    "@graphql-codegen/typescript-react-apollo": "1.12.2",
    "@graphql-codegen/typescript-resolvers": "1.12.2",
    "@test-graphql-cli/codegen": "4.0.1-beta.1",
    "@test-graphql-cli/coverage": "4.0.1-beta.1",
    "@test-graphql-cli/diff": "4.0.1-beta.1",
    "@test-graphql-cli/generate": "4.0.1-beta.1",
    "@test-graphql-cli/init": "4.0.1-beta.1",
    "@test-graphql-cli/serve": "4.0.1-beta.1",
    "@test-graphql-cli/similar": "4.0.1-beta.1",
    "@test-graphql-cli/validate": "4.0.1-beta.1",
    "graphql": "14.6.0",
    "graphql-cli": "4.0.1-alpha-0eab853.133",
    "schemats": "3.0.3",
    "tslint": "5.20.1",
    "typescript": "3.7.5"

I downgraded to 1.12.1 but didn't work for me. Also I downgraded to 1.11.2 and it did work for me as @cosmocoder mentioned. Any idea how to fix it with 1.12.2 ?

Also it's giving some incorrect peer dependency warnings when linking dependencies.

warning " > @graphql-codegen/typescript-react-apollo@1.12.2" has unmet peer dependency "graphql-tag@^2.0.0".
warning " > @test-graphql-cli/codegen@4.0.1-beta.1" has incorrect peer dependency "graphql@14.5.8".
warning " > @test-graphql-cli/coverage@4.0.1-beta.1" has incorrect peer dependency "graphql@14.5.8".
warning " > @test-graphql-cli/diff@4.0.1-beta.1" has incorrect peer dependency "graphql@14.5.8".
warning " > @test-graphql-cli/generate@4.0.1-beta.1" has incorrect peer dependency "graphql@14.5.8".
warning " > @test-graphql-cli/serve@4.0.1-beta.1" has incorrect peer dependency "graphql@14.5.8".
warning " > @test-graphql-cli/similar@4.0.1-beta.1" has incorrect peer dependency "graphql@14.5.8".
warning " > @test-graphql-cli/validate@4.0.1-beta.1" has incorrect peer dependency "graphql@14.5.8".

Not sure whether it's related anyway.

ardatan commented 4 years ago

@NomadXD Could you try to update your @test-graphql-cli/codegen to 4.0.1-beta.2 and try again? @cosmocoder Do you also use graphql-cli?

cosmocoder commented 4 years ago

@ardatan I use @graphql-codegen/cli. Is that what you meant?

ardatan commented 4 years ago

@cosmocoder I meant graphql-cli which is a different CLI tool but anyway we need to reproduce your issue somehow.

cosmocoder commented 4 years ago

@ardatan on checking the yarn.lock file in my project I saw that @graphql-codegen/cli 1.12.2 has a dependency on graphql-config "3.0.0-alpha.18", and that in turn has a dependency on @graphql-toolkit 0.9.1. I saw that yarn resolves some of the packages from the toolkit (file-loading and schema-merging) to be v0.9.1. Could this cause the error?

NomadXD commented 4 years ago

@NomadXD Could you try to update your @test-graphql-cli/codegen to 4.0.1-beta.2 and try again?

Great !!! I updated the @test-graphql-cli/codegen to 4.0.1-beta.2 and it works fine now. Thanks a lot!!! @ardatan

iamdanthedev commented 4 years ago

Hi Any updates on this issue? We have all the versions set to 1.12.2 and it produces the same error

ardatan commented 4 years ago

@iamdanthedev @cosmocoder Could you share your package.json files?

cosmocoder commented 4 years ago

@ardatan here it is:

{
  "name": "frontend",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@apollo/react-hooks": "3.1.3",
    "@apollo/react-testing": "^3.1.3",
    "@ckies/library": "0.2.0",
    "@graphql-codegen/cli": "1.12.2",
    "@graphql-codegen/fragment-matcher": "1.12.2",
    "@graphql-codegen/introspection": "1.12.2",
    "@graphql-codegen/typescript": "1.12.2",
    "@graphql-codegen/typescript-operations": "1.12.2",
    "@graphql-codegen/typescript-react-apollo": "1.12.2",
    "@reach/router": "1.2.1",
    "@testing-library/jest-dom": "^5.1.1",
    "@testing-library/react": "^9.5.0",
    "@types/classnames": "2.2.9",
    "@types/file-saver": "2.0.1",
    "@types/fontfaceobserver": "0.0.6",
    "@types/graphql": "^14.5.0",
    "@types/i18next-xhr-backend": "^1.4.2",
    "@types/jest": "25.1.3",
    "@types/js-cookie": "^2.2.5",
    "@types/lodash.isequal": "4.5.5",
    "@types/node": "13.7.7",
    "@types/reach__router": "^1.3.0",
    "@types/react": "16.9.23",
    "@types/react-css-transition-replace": "2.1.3",
    "@types/react-dom": "16.9.5",
    "@types/react-i18next": "^8.1.0",
    "@types/react-transition-group": "4.2.4",
    "@types/scroll": "3.0.0",
    "apollo-cache-inmemory": "^1.6.5",
    "apollo-client": "^2.6.8",
    "apollo-link": "^1.2.13",
    "apollo-link-batch-http": "^1.2.13",
    "apollo-link-error": "^1.1.12",
    "assert-never": "1.2.0",
    "auto-bind": "^4.0.0",
    "bundlesize": "0.18.0",
    "change-case": "4.1.1",
    "classnames": "2.2.6",
    "codecov": "^3.6.5",
    "eslint-config-prettier": "^6.10.0",
    "eslint-plugin-graphql": "^3.1.1",
    "eslint-plugin-prettier": "^3.1.2",
    "file-saver": "2.0.2",
    "fontfaceobserver": "2.1.0",
    "gh-pages": "2.2.0",
    "graphql": "^14.6.0",
    "graphql-tag": "^2.10.3",
    "i18next-browser-languagedetector": "^4.0.2",
    "i18next-scanner": "^2.10.3",
    "i18next-xhr-backend": "^3.2.2",
    "jest-date-mock": "1.0.8",
    "jest-fetch-mock": "^3.0.2",
    "jest-junit": "^10.0.0",
    "js-cookie": "^2.2.1",
    "lodash.isequal": "4.5.0",
    "node-sass": "4.13.1",
    "prettier": "^1.19.1",
    "query-string": "6.11.1",
    "react": "^16.13.0",
    "react-app-polyfill": "1.0.6",
    "react-cosmos": "^5.0.9",
    "react-css-transition-replace": "4.0.2",
    "react-dom": "^16.13.0",
    "react-i18next": "^11.3.3",
    "react-markdown": "4.3.1",
    "react-router": "5.1.2",
    "react-router-dom": "5.1.2",
    "react-scripts": "3.4.0",
    "react-transition-group": "4.3.0",
    "scroll": "3.0.1",
    "smooth-scroll-into-view-if-needed": "1.1.27",
    "source-map-explorer": "2.3.1",
    "typescript": "3.8.3",
    "use-debounce": "3.3.0"
  }
}
cosmocoder commented 4 years ago

The problem persists with v1.13.0 too

dotansimha commented 4 years ago

@cosmocoder I can only recommend you to try remove lock file, remove node_modules and reinstall all dependencies. The issue mentioned here happens when there is a mismatch between graphql-config / graphlq-cli / graphql-codegen / graphql-toolkit versions, sometimes caused by an incorrect/outdated generated lock file.

Please share a live reproduction or a repo, as we not able to reproduce this. This is our only way to find and fix the issue, and understand if it's something related to the codegen itself, or something with your environment.

I'm closing this issue, since it seems like most users are able to run this correctly now, after cleaning deps, and reinstalling latest codegen version.

iamdanthedev commented 4 years ago

This configuration works for me:

    "@graphql-codegen/add": "1.8.1",
    "@graphql-codegen/cli": "1.8.1",
    "@graphql-codegen/fragment-matcher": "1.8.1",
    "@graphql-codegen/introspection": "1.8.3",
    "@graphql-codegen/plugin-helpers": "1.8.1",
    "@graphql-codegen/schema-ast": "1.9.1",
    "@graphql-codegen/typescript": "1.8.1",
    "@graphql-codegen/typescript-operations": "1.8.1",
    "@graphql-codegen/typescript-react-apollo": "1.13.2",
    "@graphql-codegen/typescript-resolvers": "1.8.1",
    "@graphql-codegen/visitor-plugin-common": "1.8.1",
alamothe commented 4 years ago

This is the error that I get:

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

 Found 2 errors

  ✖ src/fragmentTypes.json
    Failed to load schema from src/schema.json:

        Cannot read property 'extensions' of undefined
        TypeError: Cannot read property 'extensions' of undefined
    at applyExtensionObject (/Users/nidza/Projects/car-manager/client-web/node_modules/@graphql-tool
kit/schema-merging/index.cjs.js:834:41)
    at applyExtensions (/Users/nidza/Projects/car-manager/client-web/node_modules/@graphql-toolkit/s
chema-merging/index.cjs.js:840:9)
    at makeSchema (/Users/nidza/Projects/car-manager/client-web/node_modules/@graphql-toolkit/schema
-merging/index.cjs.js:951:5)
    at Object.mergeSchemasAsync (/Users/nidza/Projects/car-manager/client-web/node_modules/@graphql-
toolkit/schema-merging/index.cjs.js:921:12)

Configuration is:

    "@graphql-codegen/cli": "^1.13.2",
    "@graphql-codegen/fragment-matcher": "^1.13.2",
    "@graphql-codegen/typescript": "^1.13.2",
    "@graphql-codegen/typescript-operations": "^1.13.2",
alamothe commented 4 years ago

Reverting to 1.9.1 fixes it for me.

ardatan commented 4 years ago

Please remove ^ from your version just use exact version instead of ^1.13.2, use 1.13.2.

alamothe commented 4 years ago

@ardatan That doesn't work 🙁

ardatan commented 4 years ago

@alamothe Could you please open another issue with more details and a minimal reproduction? Thanks.

alamothe commented 4 years ago

Sure will do!

BTW also tried removing package-lock.json and node_modules.

alamothe commented 4 years ago

https://github.com/dotansimha/graphql-code-generator/issues/3865

fractalgelfo commented 4 years ago

updating worked for me, thanks humans who have been on this thread!

kunjee17 commented 4 years ago

@fractalmarc can you please share your versions from package.json files. I am facing the same issue. Even though pushing it to latest.

For me below configuration not working.

    "@graphql-codegen/add": "1.17.7",
    "@graphql-codegen/cli": "1.17.8",
    "@graphql-codegen/fragment-matcher": "1.17.8",
    "@graphql-codegen/introspection": "1.17.8",
    "@graphql-codegen/plugin-helpers": "1.17.8",
    "@graphql-codegen/schema-ast": "1.17.8",
    "@graphql-codegen/typescript": "1.17.9",
    "@graphql-codegen/typescript-operations": "1.17.8",
    "@graphql-codegen/typescript-react-apollo": "1.17.7",
    "@graphql-codegen/typescript-resolvers": "1.17.9",
    "@graphql-codegen/typescript-type-graphql": "1.17.8",
    "@graphql-codegen/visitor-plugin-common": "1.17.14",
MincePie commented 3 years ago

I'm having trouble with this too.

Im using:

"@graphql-codegen/cli": "2.1.1", "@graphql-codegen/typescript": "2.1.2", "@graphql-codegen/typescript-operations": "2.1.2", "@graphql-codegen/typescript-urql": "^3.1.2",

I have tried removing the ^ from the last package, but it didn't make any difference.

When I try generating the graphql, the first two steps complete and the generate step fails with an error that says:

✖ src/generated/graphql.tsx AggregateError: GraphQL Document Validation failed with 1 errors; Error 0: GraphQLDocumentError: Cannot query field "changePassword" on type "Mutation".

I don't know if it's possible that slow internet might be a factor. There has been a storm in my area and internet has been slow all week. I've found this error seems to go away after waiting a few hours, restarting VS code. Not sure if this could be a factor or if I need to be looking for something else that might be causing this problem.

m7kvqbe1 commented 3 years ago

These dependencies should really all be fixed to the same tag and bumped together.

It's currently not possible to safely bump one without potentially breaking another.

Maybe a monorepo and babel style versioning could be adopted?