Error Cannot use GraphQLScalarType [*Type*] from another module or realm

I am trying to use apollo:codegen to generate schema and interfaces. I go through everything I found on the net, but with no success. I will appreciate any help on the topic.

Node: v16.14
react: 17
graphql: ^15.0.0
apollo: "^2.33.10
@apollo/client: ^3.5.10

Hi @Todor_Petkov, I suppose you’ve got a graphql version conflict.

A couple things to understand:

  • graphql doesn’t like to be installed more than once inside a project. For this reason, most libraries list graphql as a peer dependency and leave it up to the user to install graphql themselves to ensure only a single installation.
  • The Apollo CLI (apollo package) brings graphql@14 as a runtime dependency which is unusual for a library. Since it doesn’t support graphql@15, you are likely ending up with 2 copies of graphql in your node_modules (you can confirm this by running npm ls graphql to see where it’s coming from)

For this reason, we generally recommend not installing the CLI into a project’s dependencies and instead using npx apollo .... I realize there are places in our documentation which don’t say this, so I’ve gone ahead and flagged this to our docs lead to update it!

So my recommendation to you is:
npm uninstall apollo
…and anytime you want to use the CLI you would:
npx apollo <your command>

If this doesn’t work for you, please let me know! The output of npm ls graphql will probably be helpful in diagnosing the issue further.

Hi @trevor.scheer thank you a lot for the help. I follow your guides, but unfortunately now there is another error:
Error: Cannot find module ‘graphql/validation/rules/KnownArgumentNamesRule’

and npm ls graphql:

@Todor_Petkov can you share a reproduction or at the very least your package.json? Do you have any peer dependency warnings and are your packages all up to date?

The dependency on graphql/validation/rules/KnownArgumentNamesRule is a bug and has probably been fixed in a more current version of whatever library is having the problem. For example, we fixed this same issue in federation awhile ago: ERROR: Cannot find module 'graphql/validation/rules/KnownArgumentNamesRule' · Issue #904 · apollographql/federation · GitHub

Actually I don`t have any warnings and am keeping packages to the latest versions.

and I don`t have any related devDependencies only esLint-plugin, postcss, tailwind etc.

and the command that I use based on your recomendation:
npx apollo codegen:generate --target typescript --excludes=node_modules/* --includes=**/*.graphql.ts --endpoint https://server/graphql --header “authorization: Bearer token” --tagName=gql --outputFlat src/generated

@Todor_Petkov sorry for the delay. I was able to reproduce in a fresh project but then the error went away. Can you try using an older version of the CLI? This seems like a bad dependency update on our end that I’ll need to get sorted out.

To try an older version, for example, you’d type
npx apollo@2.32.0 <your command>

Here’s a list of the available versions:

Sorry I’m not more helpful at the moment, this is something we’ll need to fix for current versions to work and I’m not sure which version introduced the error. This repo has been generally unmaintained for some time. I intend to get it back to a healthy state but the long term plan is to deprecate it entirely.

Our recommendation now is to use graphql-code-generator for codegen purposes, it’s a great tool!

@trevor.scheer Thank you for the help and assistant! Is there a plan to have something new and similar from Apollo in future also?

@Todor_Petkov for now I think we’re happy with the codegen story belonging to graphql-code-generator (it really is a superior tool!). Other functionality from the CLI (Studio related) currently lives on in the Rover package and is being actively developed over there.