Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jer-sen opened this issue Jul 21, 2022 · 10 comments · May be fixed by #91
Open

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

jer-sen opened this issue Jul 21, 2022 · 10 comments · May be fixed by #91

Comments

@jer-sen
Copy link

jer-sen commented Jul 21, 2022

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';
@andresusanto
Copy link

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?

@jer-sen
Copy link
Author

jer-sen commented Jul 27, 2022

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

@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

@jer-sen
Copy link
Author

jer-sen commented Jul 27, 2022

@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
Copy link
Collaborator

ivome commented Aug 28, 2022

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

@andresusanto
Copy link

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.

@martin-cycle
Copy link

martin-cycle commented Sep 8, 2022

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.

@Avadakedvr
Copy link

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.

@Avadakedvr
Copy link

Avadakedvr commented Aug 21, 2023

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)

@hqtoan94
Copy link

hqtoan94 commented May 4, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants