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

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)

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 7
  • Comments: 25 (1 by maintainers)

Commits related to this issue

Most upvoted comments

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.

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.

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 graphql@14.0
  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 graphql@14.0 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
your-project@1.0.0 /some-directory
+-- apollo@2.4.3
| +-- apollo-language-server@1.4.2
| | `-- graphql@14.0.2  deduped
| `-- graphql@14.0.2  deduped
`-- graphql@14.0.2

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

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

For those of you following along, apollo@2.5.0 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.

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

@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

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.

Thanks for unpinning graphql in #1010 The issue described above has been resolved in apollo@2.5.0 😃

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 {} +"
	}
}

“graphql”: “~14.0.2”

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

Doesn’t work for me either

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"
}

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