slicknode/graphql-query-complexity

Error Cannot use GraphQLSchema "[object GraphQLSchema]" from another module or realm.

jer-sen opened this issue · 10 comments

I face this error. It seems due to the presence of extensions in import file names.
The library generating my schema uses ".mjs" files of graphql module, whereas graphql-query-complexity uses ".js" ones, and so graphql module believe that there are 2 different versions of it used.

For instance:

	import {
	  getArgumentValues,
	  getDirectiveValues,
	  getVariableValues,
	} from 'graphql/execution/values.js';

should be replaced with

	import {
	  getArgumentValues,
	  getDirectiveValues,
	  getVariableValues,
	} from 'graphql/execution/values';

I'm also facing the same issue. I'm guessing the problem is affecting all people that have their application built as esm (for example in my case, I'm using esbuild).

But I think it is a requirement for esm to specify the file extensions, so I think that's why it was there in the first place. So, what I ended up doing was creating an additional step after the build to replace graphql/execution/values.js with graphql/execution/values.mjs : master...andresusanto:graphql-query-complexity:master . I've published the fix here https://www.npmjs.com/package/@susanto/graphql-query-complexity in case you're interested @jer-sen

^ @slicknode is this something you would consider merging into your repo?

@andresusanto I don't see any issue to remove the file extensions, it's a serverside module https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import

@andresusanto I don't see any issue to remove the file extensions, it's a serverside module https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import

I'm not quite sure what you meant by "serverside module", but perhaps check this one out?
https://nodejs.org/api/esm.html#mandatory-file-extensions

@andresusanto you are right. Relative and absolute specifiers must have a file extension (in the build directories) but this does not apply to bare specifiers. So graphql module should be imported without a file extension. Do you agree ?

ivome commented

@slicknode is this something you would consider merging into your repo?

@andresusanto it looks like this is a general issue with the ESM version of this library, so it is probably a good idea to fix this here. We just have to make sure we are not breaking things for particular graphql-js library versions. I think the 14.x versions didn't have .mjs exports, but since we dropped support for this it's probably safe to change it here.

thanks @ivome, I've confirmed that earlier v14 versions don't have the .mjs, but since 14.6 they all do, so I'll raise PR for this.

Any update on this issue ? I am facing the same problem with my graphql scalars. I tried to use your own version of the library @andresusanto but it still raises the same issue: Cannot use GraphQLNonNull \"ID!\" from another module or realm.

I also tried to simply copy paste the code in my codebase while removing the .js extension in the import but I got the same problem.

Having the same issue.

Error: Cannot use GraphQLScalarType \"String\" 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.

Anyone managed to solve this? I have tried the resolutions suggestion, ran yarn list --pattern graphql and made sure there is a single graphql version, still getting the error.

Can confirm have managed to get rid of the error by removing .js from that import. We have initially manually done this within node_modules folder to test, then have automated that by adding graphql/execution/values.js': 'graphql/execution/values' to the resolve->alias webpack config property (using webpack within this given project)

hi @ivome , would you mind reviewing the PR above for the resolution? The idea is to keep it run without the extension so the user could config on their end to use their expected module type from both graphql-query-complexity and graphqljs