type-graphql: Cannot use GraphQLSchema from another module or realm

Describe the bug I installed express-graphql and typed-graphql, then I defined my entities and built the schema, when I pass the Schema to express-graphql and added it to my express app. As soon as I open grahpiql in my browser and run a query I get the error:


{
  "errors": [
    {
      "message": "Cannot use GraphQLSchema \"[object Object]\" from another module or realm.\n\nEnsure that there is only one instance of \"graphql\" in the node_modules\ndirectory. If different versions of \"graphql\" are the dependencies of other\nrelied on modules, use \"resolutions\" to ensure only one version is installed.\n\nhttps://yarnpkg.com/en/docs/selective-version-resolutions\n\nDuplicate \"graphql\" modules cannot be used at the same time since different\nversions may have different capabilities and behavior. The data from one\nversion used in the function from another could produce confusing and\nspurious results."
    }
  ]
}

Enviorment (please complete the following information):

  • OS: Ubuntu 18.04
  • Node 10.9.0
  • Package version 0.13.1
  • TypeScript version 3.0.1

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 29 (14 by maintainers)

Commits related to this issue

Most upvoted comments

I got it to work, by explicitly specifying the same version in my package.json as needed by type-graphql. I also had to delete my node_modules and package-lock.json to make npm dedupe the graphql dependency.

But I guess it would be better if type-graphql would specify graphql as peerDependency, to prevent such problems.

To make it a bit clear for newcomers that are looking for a solution in this issue - there’s a little guide in FAQ for this problem: https://19majkel94.github.io/type-graphql/docs/faq.html#i-got-error-like-cannot-use-graphqlschema-object-object-from-another-module-or-realm-how-to-fix-that

I switched from npm to yarn (deleting ‘node_modules’ and ‘package-lock.json’ and ‘rebuilding’) and then added this to ‘package.json’:

"resolutions": {
    "graphql": "^15.3.0"
  }

It’s all working again! 😌

I got it to work by removing node_modules/type-graphql/node_modules/graphql, but this isn’t really feasible in a production environment. Helps unblock local development until all maintainers get onboard with GQL 14

package.json

{
  "scripts": {
    "postinstall": "rm -rf node_modules/{type-graphql}/node_modules/{@types/graphql,graphql}"
  }
}

So I can’t just put a wildcard ^0.12 || ^0.13 || >=14.0.

So don’t. Just specify the versions you know are compatible at the time of writing. Everyone does it like this. If a new version comes and it is compatible, you can add it later.

Also it’s error prone as you need to remember to install it too, even if you’re not using graphql-js directly anywhere in your code.

I think you need to use it always-even apollo-server-core expects it at as peer dep.

Error: Cannot find module 'graphql'
    at Function.Module._resolveFilename (module.js:547:15)
    at Function.Module._load (module.js:474:25)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/home/capaj/git_projects/graphql-repos/ts-gql-objection-blog/node_modules/apollo-server-core/dist/runQuery.js:3:19)

running an apollo server without graphql installed. To make it easier just put it in the readme.md. Everyone does it like that. Will open a PR.