Apollo-tooling: apollo codegen:generate doesn't work in npm run-scripts

Created on 13 Oct 2018  Â·  25Comments  Â·  Source: apollographql/apollo-tooling

on osx, running "apollo codegen:generate --target=typescript --addTypename" in terminal works but putting it in package.json scripts and use "npm run" to execute the exact same command will report an error as below:

✖ Generating query files with 'typescript' target
→ Cannot query field "xxx" on type "Query"
GraphQLError: Cannot query field "xxx" on type "Query"
at Compiler.compileSelection (~/node_modules/apollo-codegen-core/lib/compiler/index.js:120:27)
at selections.selectionSetNode.selections.map.selectionNode (~/node_modules/apollo-codegen-core/lib/compiler/index.js:106:76)

good-first-issue has-reproduction

Most helpful comment

Ran into this issue today. I was getting this error after running:
yarn add graphql apollo.

Solution was to remove both packages:
yarn remove graphql apollo.

Then install the same version of graphql that apollo uses:
yarn add graphql@~14.2.1 apollo.

All 25 comments

I get the same error running scripts with yarn. Currently using [email protected].

Same here on Windows 10...

@kunchenguid is it because the version of apollo you have installed globally is different from the one that's in the package.json? I get the error consistently regardless of how I run it.

@Dremora in my case, I attempted with the same version locally and globally, and met the same result.

Failed on my local project (installed via Yarn)
Worked when run from a global install (installed via npm)

Same issue with yarn and [email protected]

Very likely related to #577, which was fixed in #624. Assuming this is not generally available yet.

For a short-term fix, place this in your package.json (thanks @stnwk):

"resolutions": {
  "@types/graphql": "14.0.1", /* if using typescript */
  "graphql": "14.0.2"
}

@stevenpetryk I tried the workaround and the vNext Branch, but neither worked for me.

@tbo The correct line for the package.json file is

"resolutions": {
  "apollo/graphql": "^14.0.2"
}

For the version number, just use whatever version your graphql dependency is; mine had the leading caret. In the case of yarn, you will know it worked correctly if the node_modules/apollo/node_modules folder does NOT have a graphql folder in it. If it does, remove your node_modules and reinstall your dependencies.

Doesn't work for me either

Using resolutions doesn't work for me, have to manually remove graphql from node_modules/apollo/node_modules to get it working

@malimccalla thanks that worked for me as hacky as it is 😕 .

Is it me or is the the Apollo project going through a phase of major change right now, a lot of their docs/examples seem out of date see:

Should probably ask this on slack but just trying to quickly gauge the situation.

@malimccalla solution worked for me as well...for a few days. then I stared getting the same error again, so I tried upgrading to v2.0.5 with no joy. dowgraded back to v1.9.2 and ran again with yarn just for giggles and it magically worked ¯_(ツ)_/¯

This is what solved it for me in my project after lots of trial and error:

  1. npm uninstall both apollo and graphql from your project
  2. npm install [email protected]
  3. npm install apollo

The key is to install graphql before apollo. In my case, codegen broke when graphql was upgraded to 14.1.1 after apollo was upgraded to 2.4.3. Simply reverting back to [email protected] didn't work for me.

For good measure (at least for the time being), I would also pin "graphql": "~14.0.2" in package.json.

This is what npm ls should look like:

$ npm ls graphql
[email protected] /some-directory
+-- [email protected]
| +-- [email protected]
| | `-- [email protected]  deduped
| `-- [email protected]  deduped
`-- [email protected]

"graphql": "~14.0.2"

In order to remove the oddly pinned dependencies in the Apollo CLI, and apparently another Apollo package, I resorted to using the following to nuking them all after an npm install occurs:

find node_modules/ -type d ! -wholename "node_modules/graphql" ! -wholename "node_modules/@types/graphql" -name "graphql" -exec rm -rf {} +

This will find any package named graphql in node_modules that _is not_ node_modules/graphql or node_modules/@types/graphql and remove them.

For those using npm or yarn users who don't want to use resolutions, the following addition to your package.json scripts section can automate this for you. Please note, this is not something to use if this is a distributed library, but is fine to use (imo) if this is just an application.

{
    "scripts": {
        "postinstall": "find node_modules/ -type d ! -wholename \"node_modules/graphql\" ! -wholename \"node_modules/@types/graphql\" -name \"graphql\" -exec rm -rf {} +"
    }
}

Looks like the dependency on graphql was properly unpinned for a bit, but then repinned on PR #952. No idea what's going on with their build system, but why does graphql have to be pinned to a very specific version for _everyone_ that is using the Apollo tooling at all. I guess this wouldn't matter if users were only installing apollo globally, but this completely busts up any project that installs apollo via devDependencies.

Looks like the dependency on graphql was properly unpinned for a bit, but then repinned on PR #952. No idea what's going on with their build system, but why does graphql have to be pinned to a very specific version for _everyone_ that is using the Apollo tooling at all. I guess this wouldn't matter if users were only installing apollo globally, but this completely busts up any project that installs apollo via devDependencies.

We're using the apollo package for apollo service:check/publish in our travis builds and for local dev.
The pinned version of graphql to 14.0.2 introduced in #952 prevents us from using any other version of graphql than the one defined in the apollo package in our stack.

I'm not really a fan of global installs either, it'll lead to outdated dependencies on developer machines over time.

Thanks for unpinning graphql in #1010
The issue described above has been resolved in [email protected] :)

@sh thanks for the quick feedback! Sorry about the trouble this was causing, honest mistake on my part.

For those of you following along, [email protected] should resolve this issue as @sh mentioned!
I'm going to close this now since it's a bit dated and represents a former issue that's no longer being discussed.

Ran into this issue today. I was getting this error after running:
yarn add graphql apollo.

Solution was to remove both packages:
yarn remove graphql apollo.

Then install the same version of graphql that apollo uses:
yarn add graphql@~14.2.1 apollo.

Yup, after updating the packages I ran into this error yet again. Setting graphql dependency to ~14.2.1 fixed it. Should this get switched to a peer dependency in these packages so that npm can at least warn if these tools aren't sharing the same graphql package instance? I'm also guessing this is probably best if this was made into a new issue.

@basicdays did you create a new issue?

For people landing here from a google search these are the steps to fix right now:

  • do a yarn why graphql to see the version apollo currently wants (for me it was 14.4.2)
  • change your package.json to use "graphql": "~14.4.2",
  • do a yarn

@basicdays did you create a new issue?

For people landing here from a google search these are the steps to fix right now:

  • do a yarn why graphql to see the version apollo currently wants (for me it was 14.4.2)
  • change your package.json to use "graphql": "~14.4.2",
  • do a yarn

Great! This worked for me as well.

Fwiw I was seeing a CLIError that read:

{ CLIError: Error in "Loading schema for Unnamed Project": TypeError: schema.toConfig is not a function ...
}

when trying to run the codegen from a yarn script. Everything worked from a global apollo installation.

Updating the graphql version fixed it. I can now run the codegen command from a yarn script.

npm install -g apollo
I had -D instead of -g(globally) and it worked for me!!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rasmusprentow picture rasmusprentow  Â·  3Comments

u-ashish picture u-ashish  Â·  4Comments

viridia picture viridia  Â·  3Comments

lirbank picture lirbank  Â·  4Comments

fenech picture fenech  Â·  3Comments