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

"No operations or fragments" error during client:codegen #719

Closed
chrishandorf opened this issue Nov 17, 2018 · 50 comments
Closed

"No operations or fragments" error during client:codegen #719

chrishandorf opened this issue Nov 17, 2018 · 50 comments

Comments

@chrishandorf
Copy link

I'm running client:codegen on apollo/2.1.2 darwin-x64 node-v10.13.0
(iMac)

The endpoint is successfully reached but codegen can't seem to find any files that are specified using the --queries option. The files do exist though. I get the same error if I use --localSchemaFile=schema.json instead of --endpoint. See the 2 commands run below:

$ apollo client:codegen --target=typescript --endpoint=http://localhost:8080/graphql --queries="./src/app/graphqlPublic/*.graphql"
✔ Loading Apollo Project
✖ Generating query files with 'typescript' target
→ No operations or fragments found to generate code for.
Error: No operations or fragments found to generate code for.
at write (/.npm-global/lib/node_modules/apollo/lib/commands/client/codegen.js:58:39)
at Task.task (
/.npm-global/lib/node_modules/apollo/lib/commands/client/codegen.js:83:46)

$ cat ./src/app/graphqlPublic/page-visit.graphql
query PageVisit($page: String!){
pageVisit(page:$page)
}

@ghost ghost added the blocking label Nov 17, 2018
@ZenSoftware
Copy link

Since Apollo 2, I have been getting the same error using client:codegen. No matter what I try, I can't seem to get apollo cli to pick up the files that I have defined. Things that I have tried:

  • Used the includes field defined in an apollo.config.js.
  • Used the --queries flag.
  • Used different glob patterns, including: *.ts files that use gql tagged template literals, *.gql and *.graphql.

The files do exist, have proper syntax, and a legitimate schema.json because I can generate types using apollo v1.

My machine specs:
Windows 10
Node v10.13.0
[email protected]

The error:

apollo client:codegen --target=typescript --outputFlat --localSchemaFile="libs/gql/schema.json" --queries="libs/**/*.gql"
 √ Loading Apollo Project
 × Generating query files with 'typescript' target
   → No operations or fragments found to generate code for.
Error: No operations or fragments found to generate code for.
    at write (C:/Users/Zen/AppData/Roaming/npm/node_modules/apollo/lib/commands/client/codegen.js:58:39)
    at Task.task (C:/Users/Zen/AppData/Roaming/npm/node_modules/apollo/lib/commands/client/codegen.js:83:46)

@chenglabs
Copy link

chenglabs commented Nov 18, 2018

I have the same problem.. It can't handle relative paths in the includes field.
It only works when I configure it with the full path

apollo client:codegen src/types -c=apollo.config.js --target=typescript --outputFlat

apollo.config.js.
module.exports = {
client: {
includes: [__dirname+'/src/queries/**'],
service: {
name: "hello-service",
url: "http://localhost:4000/graphql",
}
}
}

@ZenSoftware
Copy link

@chenglabs Interesting that you managed to get it to work at least. I just tried it with absolute paths, and I was not able to get it to work. Could you please provide us with the specifications of your system you are working on?

@chrishandorf
Copy link
Author

I used the full pathname for the --queries arg instead of the relative path used above. Still no luck. Same result and completely blocked.

@chrishandorf
Copy link
Author

I just saw in another issue thread this command was working in [email protected]. I downgraded and yay it worked fine.

@chenglabs
Copy link

@chenglabs Interesting that you managed to get it to work at least. I just tried it with absolute paths, and I was not able to get it to work. Could you please provide us with the specifications of your system you are working on?

Hi,

check example at https://github.com/chenglabs/test_apollo_client
Quickly added some code together with create-react-app to test with
Generate the types with yarn types

@JakeDawkins
Copy link
Contributor

@chenglabs I was able to reproduce the issue you were seeing. If you remove the leading / in your includes path, it should work. Like this:

module.exports = {
   client: {
     includes: ['src/queries/**'],
     service: {
       name: "pokemon",
       url: "https://graphql-pokemon.now.sh",
     }
   }
 }

@JakeDawkins
Copy link
Contributor

@ZenSoftware @chrishandorf do either of you have a reproduction on codesandbox/glitch/github that I could look at?

@chenglabs
Copy link

@JakeDawkins
Hi Jake,
Tried your solution, it results in the same errors. I think it's Windows related, if I run the script in Windows Bash shell it works without any problems.

@delannoyk
Copy link

Hey.

Got the same issue on macOS so I don't think it's Windows related 🤔 Unfortunately I don't have a lot of insights to offer...

@chenglabs
Copy link

@JakeDawkins

Ok, did some debugging in the Apollo code
I think there is a bug in packages/apollo/src/Command.ts line 175
const rootURI = file://${parse(filepath).dir};
should be
const rootURI = file:///${parse(filepath).dir};

It's missing one slash.. (https://en.wikipedia.org/wiki/File_URI_scheme)
When I added the slash in node_modules\Apollo\lib\Command.js everything works.

@chrishandorf
Copy link
Author

@chenglabs If I make the above change you suggested locally I get this:

$ apollo client:codegen --queries='src/app/graphqlPublic/*.graphql' --localSchemaFile=schema.json --target=typescript
Error: [UriError]: If a URI does not contain an authority component, then the path cannot begin with two slash characters ("//")
at _validateUri (~/.npm-global/lib/node_modules/apollo/node_modules/vscode-uri/lib/umd/index.js:57:27)

@JakeDawkins I could work on a minimal set of files to reproduce the problem if that would be helpful. I haven't used codesandbox. Is there another way I could send you the fileset?

@trevor-scheer
Copy link
Member

This seems like the root of the issue:

Tried your solution, it results in the same errors. I think it's Windows related, if I run the script in Windows Bash shell it works without any problems.

I'm going to try to get a Windows environment set up since I need to be able to reproduce issues like this that pop up.

@chenglabs @chrishandorf I see the same issue with the 3 slashes. It looks like VSCode's URI implementation takes care of the third slash:

let uri = Uri.file('/users/me/c#-projects/');

assert.ok(uri.scheme === 'file');
assert.ok(uri.authority === '');
assert.ok(uri.path === '/users/me/c#-projects/');
assert.ok(uri.query === '');
assert.ok(uri.fragment === '');
assert.ok(uri.toString() === 'file:///users/me/c%23-projects/')

https://github.com/Microsoft/vscode-uri

@chrishandorf a repo that we can clone is best!

@chrishandorf
Copy link
Author

@JakeDawkins @trevor-scheer please see this: https://github.com/chrishandorf/apolloError

Its completely bare bones and not working

@trevor-scheer
Copy link
Member

@chrishandorf super helpful, thank you!

I've got a PR open now for better CLI flag support here:
#733

Your configless example helped me find an edge case that I'll be submitting a fix for soon. Looks like behavior is a little wonky when no config is provided.

@trevor-scheer
Copy link
Member

@chrishandorf PR for configless bug: #734

@chrishandorf
Copy link
Author

@trevor-scheer many thanks and I think its getting better, but it seems to be picking up some graphql files outside of the ones I specified in my --queries. I have 3 graphql folders containing *.graphql files. They are basically public/customer/admin graphql folders. When I try to process the public folder (graphqlPublic) it gets errors from the customer folder (graphqlCust):

$ apollo client:codegen --queries='src/app/graphqlPublic/*.graphql' --localSchemaFile=publicSchema.json --target=typescript --outputFlat src/app/graphqlPublic/gen
.../src/app/graphqlCust/account.graphql: Cannot query field "account" on type "uncQuery".
Many similar errors omitted here for brevity.

I currently have everything working by using [email protected] to download the schema.json file using the --endpoint option, then I revert to [email protected] to do the codegen. Clearly I would like to do it all with 2.1.2.

Should I create another repo?

@trevor-scheer
Copy link
Member

@chrishandorf I just published 2.1.3 with the couple fixes listed above, would you mind giving that a shot?

If there are still issues, another reproduction is always appreciated! I'd like for this thread to not turn into an assimilation of multiple codegen related issues, so if you don't mind opening another issue that would be preferable. Thanks again 😄

@jeffwillette
Copy link

jeffwillette commented Nov 22, 2018

I am still getting the error with 2.1.3... but I am not sure if it is coming from #723 not finding the tags or from my glob not reading the correct files...

apollo client:codegen \
	--addTypename \
	--outputFlat \
	--tagName=graphql \
	--target=typescript \
	--localSchemaFile=./gatsby-schema.json \
	--includes=./pages/team/*.tsx \
	gatsby-queries.d.ts

here is the exact error

 ✔ Loading Apollo Project
  ✖ Generating query files with 'typescript' target
    → No operations or fragments found to generate code for.
Error: No operations or fragments found to generate code for.
    at write (~/.config/yarn/global/node_modules/apollo/lib/commands/client/codegen.js:58:39)
    at Task.task (~/.config/yarn/global/node_modules/apollo/lib/commands/client/codegen.js:83:46)
gmake: *** [Makefile:65: codegen-gatsby] Error 2

@chenglabs
Copy link

chenglabs commented Nov 22, 2018

@deltaskelta
Are you running Windows ? If so, you need to pass the whole path in the includes argument

apollo client:codegen src/types --target=typescript --outputFlat --endpoint=https://graphql-pokemon.now.sh --includes=D:/Projects/GraphQL/demo/pokemon/src/queries/**

@ZenSoftware
Copy link

I am also still getting the error as of 2.1.3 unfortunately. Thanks for trying though @trevor-scheer. I have tried all sorts of things: Absolute paths, relative paths, different glob patterns, --queries flag, and using an apollo.config.js. Nothing has worked yet unfortunately.

I am on Windows, though I am not entirely convinced it is a Windows specific issue. I see that there are 2 users in this thread on Mac getting this error. ^_^

@jeffwillette
Copy link

I'm on Mac so it is definitely not a Windows only issue. I have tried different glob patterns, --queries, etc as @ZenSoftware mentioned as well

@chenglabs
Copy link

@ZenSoftware @deltaskelta
Not really a great answer to your problems, but you could try your luck with https://graphql-code-generator.com. I have played with it, quite nice features.

@chrishandorf
Copy link
Author

release 2.1.3 is now working perfectly for me

@trevor-scheer
Copy link
Member

@chrishandorf glad to hear it! Thanks for working with me on this 🎉

@deltaskelta this sounds like it's a tagName issue and I'd rather not conflate that with this issue. If you can still reproduce on Mac with a gql tag somewhere that falls in your --includes glob, then this issue may not be resolved yet.

@ZenSoftware I think the Windows issue is the last one standing here. I've got access to a Windows environment now, so if you can help me out with a basic reproduction that would be helpful!

@jeffwillette
Copy link

I just tried with gql tags and I still have the issue

@jeffwillette
Copy link

I made a reproduction here https://github.com/deltaskelta/apollo-cli-719

@ZenSoftware
Copy link

@trevor-scheer I just cloned @deltaskelta reproduction. I can confirm that apollo client:codegen is not working for me using this repo on Windows 10, Node v10.13.0, [email protected]

@trevor-scheer
Copy link
Member

Much appreciated! Not sure how soon I'll get to it, I'm away for the next week. If I find some free time to take a look then I may, otherwise maybe @JakeDawkins can take a look after the weekend?

@cmcaboy
Copy link

cmcaboy commented Nov 25, 2018

I was able to get this to work by upgrading to [email protected]. I am on on MacOS.

@brabeji
Copy link
Contributor

brabeji commented Nov 26, 2018

On MacOS and [email protected] my files with .gql extension weren't included. #740

@ZenSoftware
Copy link

ZenSoftware commented Dec 1, 2018

@trevor-scheer This issue is now resolved for me. I installed [email protected] and it is working just fine now! (I am one of the Windows users)

(Sorry I jumped the gun, read the comment below)

@ZenSoftware
Copy link

I'm sorry. I may have jumped the gun in claiming that I have gotten it to work. I managed to get it to work using a convoluted approach that happens to work for our current project.

I got it to work by using a Gulp Task that executes the apollo client:codegen command using a Node child_process that executes in the Gulp Task.

But if I just use apollo client:codegen the standard way, using the command line, it still fails.

@jeffwillette
Copy link

I just tried my https://github.com/deltaskelta/apollo-cli-719 with 2.1.7 and it is still failing for me

@trevor-scheer
Copy link
Member

@deltaskelta I just cloned your repo and can confirm that all that's left for you is the tagName issue. The fix isn't published yet but I just checked locally that this PR resolves the issue (at least on my Mac). I tested the current v. 2.1.7 (breaks for me as expected) and this PR #793 (fixes the issue).

@ZenSoftware can you share some more info for me?

  1. apollo.config.js file
  2. Exactly the command you're running w/flags
  3. Errors / output from running the command

Thanks everyone for all the feedback here, it's been super helpful 🎉

@blocka
Copy link

blocka commented Dec 5, 2018

I'm trying

npx apollo client:codegen --endpoint=http://localhost:3000/graphql --outputFlat --addTypename --target=typescript src/schema.ts

and getting

 ✔ Loading Apollo Project
 ✖ Generating query files with 'typescript' target
   → No document sets found to generate code for.

@alexcibotari
Copy link

Hello,

I use the latest version of project (2.1.7)

apollo codegen:generate --target=typescript --localSchemaFile=xxx.graphqls --outputFlat

and as result I'm receiving (on Windows 10 and MacOS 10.14)

  √ Loading Apollo Project
  × Generating query files with 'typescript' target
    → No operations or fragments found to generate code for.
Error: No operations or fragments found to generate code for.

@brabeji
Copy link
Contributor

brabeji commented Dec 5, 2018

Hi @alexcibotari , try to use .graphql or .gql extension for your --localSchemaFile

@alexcibotari
Copy link

alexcibotari commented Dec 5, 2018

I @brabeji
have tried

apollo codegen:generate --target=typescript --localSchemaFile=xxx.graphql --outputFlat
apollo codegen:generate --target=typescript --localSchemaFile=xxx.gql --outputFlat

and even json configuration

apollo codegen:generate --target=typescript --localSchemaFile=graphql.schema.json --outputFlat

result is the same: error

@trevor-scheer
Copy link
Member

@alexcibotari where are your query files located? By default, apollo will look in src/ for your queries. If your project is set up differently, you'll need to add --includes=your/project/dir/**/*.{ts,tsx,js,jsx} or something similar. includes is a glob pattern that points to your graphql queries.

@patrys
Copy link
Contributor

patrys commented Dec 6, 2018

I've found the cause of this. This code breaks on Windows:

const rootURI =
filepath === process.cwd()
? `file://${filepath}`
: `file://${parse(filepath).dir}`;

The reason is that a proper absolute URI on Windows is file:///c:/... while this code generates file://c:/... (notice the number of slashes). A proper way to do that would be to use:

import Uri from "vscode-uri";

const rootURI =
  filepath === process.cwd()
  ? Uri.file(filepath)
  : Uri.file(parse(filepath).dir);

This fixes Apollo not being able to properly discover queries and fragments within a project. There is however another problem related to how relative URIs are generated later on.

During code generation rootPath is initialized to process.cwd() (c:\Users\...).

const { rootPath = process.cwd() } = options;

However it's later used here:

path.dirname(path.relative(rootPath, toPath(sourcePath))),

That call to path.relative receives a local file path (C:\Users\...) and the path part of a parsed URI (/c:/Users/...).

This can be fixed by initializing rootPath to an URI path:

const { rootPath = Uri.file(process.cwd()).path } = options;

This fixes files not being written. There is however yet another problem sitting even deeper: the relative import paths of the generated files use backslashes instead of forward slashes.

@jbaxleyiii
Copy link
Contributor

@patrys this is a great find! We should setup appveyor for sure!

@ZenSoftware
Copy link

@patrys you are an example of why the open source community gives me hope for humanity. Well done my friend ^_^

@jeffwillette
Copy link

My issue seems to be fixed as of 2.1.8

@ZenSoftware
Copy link

My issues are resolved as well as of 2.1.8. (I am Windows user)

@trevor-scheer
Copy link
Member

Thanks for reporting back! I believe all that's left is the Windows issue, so I'm going to close this and point to the open PR #810.

We're currently investigating CI for both Windows and Linux in order to catch env-related FS issues in our tests 🙂

@chenglabs
Copy link

Hi @trevor-scheer
I just did an update of the packages, Apollo 2.1.8
I don't see your changes in PR #810 in the latest version yet.

@trevor-scheer
Copy link
Member

@chenglabs that's correct, those changes haven't been merged yet. You can keep an eye on the status of the PR to know when it lands in master.

@SanchiSinghal
Copy link

Hi, Fail to generate the schema types when I'm using @client directive while using a query component and generating query using gql tag.

Getting the following error ::
.../queries/graphql/TransactionFee/Queries.ts: Cannot query field "abc" on type "Query".
.../queries/graphql/TransactionFee/Queries.ts: Cannot query field "xyz" on type "Query".
{ ToolError: Validation of GraphQL query document failed
at Object.validateQueryDocument (/home/projectname/.nvm/versions/node/v10.13.0/lib/node_modules/apollo/node_modules/apollo-language-server/lib/errors/validation.js:32:19)
at Object.generate [as default] (/home/projectname/.nvm/versions/node/v10.13.0/lib/node_modules/apollo/lib/generate.js:19:18)
at write (/home/projectname/.nvm/versions/node/v10.13.0/lib/node_modules/apollo/lib/commands/client/codegen.js:64:54)
at Task.task (/home/projectname/.nvm/versions/node/v10.13.0/lib/node_modules/apollo/lib/commands/client/codegen.js:83:46) name: 'ToolError' }
✔ Loading Apollo Project
✖ Generating query files with 'typescript' target
→ Validation of GraphQL query document failed

Here, abc and xyz are the local variables created using @client directive.

@trevor-scheer
Copy link
Member

Hey @SanchiSinghal, this sounds unrelated to the issue mentioned here. Would you mind opening a separate issue? Thanks!

@apollographql apollographql locked as resolved and limited conversation to collaborators Jan 10, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests