Graphql-code-generator: Updating typescript-operations from 1.9.1 to 1.12.1 broke our build

Created on 5 Feb 2020  ยท  37Comments  ยท  Source: dotansimha/graphql-code-generator

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:

  • OS:
  • @graphql-codegen/cli: 1.11.2
  • NodeJS: 12.13.1

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

bug core

Most helpful comment

@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?

All 37 comments

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

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: []

@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.

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

@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.

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

Same issue, downgrading to 1.12.1 fixed it for me.

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.

@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!

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.

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

@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

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

@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.

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

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

@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/[email protected]" has unmet peer dependency "graphql-tag@^2.0.0".
warning " > @test-graphql-cli/[email protected]" has incorrect peer dependency "[email protected]".
warning " > @test-graphql-cli/[email protected]" has incorrect peer dependency "[email protected]".
warning " > @test-graphql-cli/[email protected]" has incorrect peer dependency "[email protected]".
warning " > @test-graphql-cli/[email protected]" has incorrect peer dependency "[email protected]".
warning " > @test-graphql-cli/[email protected]" has incorrect peer dependency "[email protected]".
warning " > @test-graphql-cli/[email protected]" has incorrect peer dependency "[email protected]".
warning " > @test-graphql-cli/[email protected]" has incorrect peer dependency "[email protected]".

Not sure whether it's related anyway.

@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?

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

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

@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 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

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

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

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

The problem persists with v1.13.0 too

@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.

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

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

Reverting to 1.9.1 fixes it for me.

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

@ardatan That doesn't work ๐Ÿ™

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

Sure will do!

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

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

@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",
Was this page helpful?
0 / 5 - 0 ratings

Related issues

edorivai picture edorivai  ยท  3Comments

NickClark picture NickClark  ยท  3Comments

quolpr picture quolpr  ยท  3Comments

dotansimha picture dotansimha  ยท  3Comments

jagregory picture jagregory  ยท  3Comments