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

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

Closed
atinylittleshell opened this issue Oct 13, 2018 · 25 comments
Closed

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

atinylittleshell opened this issue Oct 13, 2018 · 25 comments
Labels
🌹 has-reproduction ❤ Has a reproduction in a codesandbox or single minimal repository 🥇 good-first-issue Issues that are suitable for first-time contributors.

Comments

@atinylittleshell
Copy link

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)

@ghost ghost added 🥇 good-first-issue Issues that are suitable for first-time contributors. 🌹 has-reproduction ❤ Has a reproduction in a codesandbox or single minimal repository labels Oct 13, 2018
@bopfer
Copy link

bopfer commented Oct 17, 2018

I get the same error running scripts with yarn. Currently using apollo@1.10.0-register.1.

@MariuszKogut
Copy link

MariuszKogut commented Oct 23, 2018

Same here on Windows 10...

@Dremora
Copy link

Dremora commented Oct 23, 2018

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

@ellsclytn
Copy link

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

@filipsuk
Copy link

Same issue with yarn and apollo@1.9.2

@stevenpetryk
Copy link

stevenpetryk commented Oct 29, 2018

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

@tbo
Copy link

tbo commented Oct 29, 2018

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

@johnadamson
Copy link

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

@lednhatkhanh
Copy link

Doesn't work for me either

@malimccalla
Copy link

malimccalla commented Nov 6, 2018

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

@ksrb
Copy link

ksrb commented Nov 6, 2018

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

@jcarraway
Copy link

@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 ¯_(ツ)_/¯

@gsong
Copy link

gsong commented Feb 3, 2019

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

@vitrukroman
Copy link

"graphql": "~14.0.2"

@basicdays
Copy link

basicdays commented Feb 6, 2019

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

@basicdays
Copy link

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.

@sh
Copy link

sh commented Feb 9, 2019

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.

@sh
Copy link

sh commented Feb 13, 2019

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

@trevor-scheer
Copy link
Member

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

langpavel pushed a commit to kriasoft/react-starter-kit that referenced this issue Mar 21, 2019
* small README.md grammar change (#1675)

* Upgrade all dependencies including Babel 7 (#1673)

* Upgrade all dependencies including Babel 7

1. ncu --upgradeAll
4. Leave "graphql" to be "^13.2.0" that apollo packages depend on
2. Apply patch of "npx babel-upgrade" output
3. Leave only necessary babel plugins
4. Pass "yarn fix" by disabling stricter rules

fixes #1654 #1626 #1607

* Also ignore babel config file from webpack.config.js

* Resolve conflict and follow upstream/master

* small README.md grammar change (#1675)

* Upgrade all dependencies including Babel 7 (#1673)

* Upgrade all dependencies including Babel 7

* update apollo packages

* Use new local state management of Apollo Client 2.5

* Client Schema
* Simplify cache and cache defaults
* User in local state instead of context

* bye apollo-link-state

* mod: better suppressing flow error for .graphql import

* fix: jest error by updating jest

* Upgrade all dependencies

fix 2 breaking changes:
* css-loader no longer accepts 'minimize' option
* isomorphic-style-loader uses new React Context now

* refac: Use functional component for components

* fix: resolve apollo and graphql versions problem

ref. apollographql/apollo-tooling#630 (comment)
@rgoldfinger-quizlet
Copy link

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.

@basicdays
Copy link

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.

@larsenwork
Copy link

@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

@t0wer001
Copy link

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

@alexsullivan114
Copy link

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.

@AhmadAlhajKarim
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🌹 has-reproduction ❤ Has a reproduction in a codesandbox or single minimal repository 🥇 good-first-issue Issues that are suitable for first-time contributors.
Projects
None yet
Development

No branches or pull requests