apollo-tooling: 2.12.3: Error: Cannot use GraphQLSchema "[object GraphQLSchema]" from another module or realm

After upgrading from apollo@2.12.2 --> apollo@2.12.3, I started receiving the following error while trying to download my schema:

> apollo service:download --endpoint=$API_ENDPOINT_PROXY/v2/graphql graphql-schema.json

  ✔ Loading Apollo Project
  ✖ Saving schema to graphql-schema.json
    → spurious results.
Error: Cannot use GraphQLSchema "[object GraphQLSchema]" from another module or realm.
Ensure that there is only one instance of "graphql" in the node_modules
directory. If different versions of "graphql" are the dependencies of other
relied on modules, use "resolutions" to ensure only one version is installed.
https://yarnpkg.com/en/docs/selective-version-resolutions
Duplicate "graphql" modules cannot be used at the same time since different
versions may have different capabilities and behavior. The data from one
version used in the function from another could produce confusing and
spurious results.
    at instanceOf (~/Sites/nextradata/mmdp/src/mmdp-app/node_modules/apollo/node_modules/graphql/jsutils/instanceOf.js:37:13)
    at isSchema (~/Sites/nextradata/mmdp/src/mmdp-app/node_modules/apollo/node_modules/graphql/type/schema.js:34:34)
    at assertSchema (~/Sites/nextradata/mmdp/src/mmdp-app/node_modules/apollo/node_modules/graphql/type/schema.js:38:4)
    at validateSchema (~/Sites/nextradata/mmdp/src/mmdp-app/node_modules/apollo/node_modules/graphql/type/validate.js:53:28)
    at assertValidSchema (~/Sites/nextradata/mmdp/src/mmdp-app/node_modules/apollo/node_modules/graphql/type/validate.js:77:16)
    at assertValidExecutionArguments (~/Sites/nextradata/mmdp/src/mmdp-app/node_modules/apollo/node_modules/graphql/execution/execute.js:146:35)
    at executeImpl (~/Sites/nextradata/mmdp/src/mmdp-app/node_modules/apollo/node_modules/graphql/execution/execute.js:67:3)
    at execute (~/Sites/nextradata/mmdp/src/mmdp-app/node_modules/apollo/node_modules/graphql/execution/execute.js:62:256)
    at Object.introspectionFromSchema (~/Sites/nextradata/mmdp/src/mmdp-app/node_modules/apollo/node_modules/graphql/utilities/introspectionFromSchema.js:38:37)
    at Task.task (~/Sites/nextradata/mmdp/src/mmdp-app/node_modules/apollo/lib/commands/service/download.js:21:82)
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! mmdp-app@0.0.7 update:schema: `apollo service:download --endpoint=$API_ENDPOINT_PROXY/v2/graphql graphql-schema.json`
npm ERR! Exit status 2
npm ERR! 
npm ERR! Failed at the mmdp-app@0.0.7 update:schema script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/joncursi/.npm/_logs/2019-05-23T20_52_56_722Z-debug.log
ERROR: "update:schema" exited with 2.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! mmdp-app@0.0.7 update: `dotenv -e .env -- npm-run-all update:schema update:types`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the mmdp-app@0.0.7 update script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/joncursi/.npm/_logs/2019-05-23T20_52_56_754Z-debug.log

Downgrading back to 2.12.2 causes the error to go away.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 28
  • Comments: 22

Most upvoted comments

If you are using npm:

  • Copy the version of graphql from your node_modules/apollo/package.json
    • For me at the time of writing it’s "graphql": "~14.2.1
  • Paste that in your project’s package.json to make sure it matches exactly what apollo is using
  • Run rm -rf node_modules/
  • Run npm i
  • Run npm dedupe

Yup, and all sort of others things like burning sand in a cup shouting chinese words but no success…

Just to add on here, I was able to do what @xsats did, but using “graphql”: “^15.0.0”. There was no need to downgrade the version. All I did was

  1. Remove node_modules in the base directory of the monorepo
  2. Add “resolutions”:{“graphql”:“^15.0.0”} to the package.json in the base directory
  3. Run yarn install in the base directory

If you are using npm:

  • Copy the version of graphql from your node_modules/apollo/package.json

    • For me at the time of writing it’s "graphql": "~14.2.1
  • Paste that in your project’s package.json to make sure it matches exactly what apollo is using

  • Run rm -rf node_modules/

  • Run npm i

  • Run npm dedupe

In case anyone has a variable version like this 14.0.2 - 14.2.0 || ^14.3.1,:

  1. Pick one and put it in both your packadge.json and in node_modules/apollo/packadge.json
  2. Delete node_modules/apollo/node_modules
  3. CD into node_modules/apollo/
  4. Install deps in node_modules/apollo/
  5. Install deps in your project

I changed my backend’s folder structure around and started getting this error while using serverless-offline and refreshing the local version of my UI. The fix for me was to change this line in my webpack config

externals: [ nodeExternals() ],

to

externals: [ nodeExternals({ modulesDir: path.resolve(__dirname, 'node_modules') }) ],

This fix probably doesn’t apply to you if you are using a standard folder structure. I don’t even know why this worked.

This still seems to be an issue, should we re-open?

This is not a fix as it doesn’t work lol, i don’t even remember exactly but it’s like putting graphql with a precise version in the resolutions field of the package.json

I made my own tools

@Sceat did you made it work for you in monorepo ?

Nop it’s not possible, neither to link other project using Graphql.js this realm security is imo useless and makes things overcomplex, i resolved it by refactoring my whole monorepo to import ‘graphql’ in only one place. Note that this security also prevent us from using a commonjs library that use graphql & using it ourselve through esm, that said i also moved away from all that heavy apollo stuff which has enough line of code to sink the earth into a black hole 😁 good luck my friend, i opted to write my own graphql solution on top of the graphqljs lib

@amritk This is still an issue for me as well.

I am unable to understand where this error comes from…

My command: apollo codegen:generate -c apollo.config.js --target typescript --excludes=node_modules/* --includes=./src/apollo/queries/*.ts --tagName=gql --outputFlat src/graphql-schema

The error:

    ✖ Generating query files with 'typescript' target
    → spurious results.
    Error: Cannot use GraphQLObjectType "QueryType" from another 
    module or realm.

My graphql dependencies:

npm ls graphql        
my-package
├─┬ @apollo/client@3.5.10
│ ├─┬ @graphql-typed-document-node/core@3.1.1
│ │ └── graphql@15.8.0 deduped
│ └── graphql@15.8.0 deduped
├─┬ @vue/apollo-composable@4.0.0-alpha.16
│ └── graphql@15.8.0 deduped
├─┬ apollo@2.33.10
│ ├─┬ @apollographql/apollo-tools@0.5.3
│ │ └── graphql@15.8.0 deduped
│ ├─┬ apollo-graphql@0.9.6
│ │ └── graphql@15.8.0 deduped
│ ├─┬ apollo-language-server@1.26.8
│ │ ├─┬ @apollo/federation@0.27.0
│ │ │ └── graphql@15.8.0 deduped
│ │ ├─┬ @apollographql/graphql-language-service-interface@2.0.2
│ │ │ ├─┬ @apollographql/graphql-language-service-parser@2.0.2
│ │ │ │ └── graphql@14.7.0 deduped
│ │ │ ├─┬ @apollographql/graphql-language-service-types@2.0.2
│ │ │ │ └── graphql@14.7.0 deduped
│ │ │ ├─┬ @apollographql/graphql-language-service-utils@2.0.2
│ │ │ │ └── graphql@14.7.0 deduped
│ │ │ └── graphql@14.7.0 deduped
│ │ ├─┬ apollo-link-error@1.1.13
│ │ │ └─┬ apollo-link-http-common@0.2.16
│ │ │   └── graphql@15.8.0 deduped
│ │ ├─┬ apollo-link-http@1.5.17
│ │ │ └── graphql@15.8.0 deduped
│ │ ├─┬ apollo-link@1.2.14
│ │ │ ├─┬ apollo-utilities@1.3.4
│ │ │ │ └── graphql@15.8.0 deduped
│ │ │ └── graphql@15.8.0 deduped
│ │ ├─┬ apollo-server-errors@2.5.0
│ │ │ └── graphql@15.8.0 deduped
│ │ └── graphql@14.7.0
│ ├─┬ graphql-tag@2.12.4
│ │ └── graphql@15.8.0 deduped
│ └── graphql@15.8.0 deduped
├─┬ graphql-tag@2.12.6
│ └── graphql@15.8.0 deduped
└── graphql@15.8.0

All use graphql@15.8.0 except some sub-dependencies of apollo

To onlookers facing a similar problem, I ended up adding yarn resolutions to the base package.json in my yarn workspaces monorepo, specifying a version of GraphQL that previously worked fine.

‘“resolutions”: { “graphql”: “^14.3.2” }"’

This fully resolved the duplicate GraphQL dependency error above.