apollo-tooling: "No operations or fragments" error during client:codegen

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

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 11
  • Comments: 50 (17 by maintainers)

Most upvoted comments

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”, } } }

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

Its completely bare bones and not working

@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

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

https://github.com/apollographql/apollo-tooling/blob/99bb221695569a882b4b89bf840a332094c97b6f/packages/apollo/src/Command.ts#L179-L182

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\...).

https://github.com/apollographql/apollo-tooling/blob/99bb221695569a882b4b89bf840a332094c97b6f/packages/apollo/src/generate.ts#L61

However it’s later used here:

https://github.com/apollographql/apollo-tooling/blob/99bb221695569a882b4b89bf840a332094c97b6f/packages/apollo/src/generate.ts#L166

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.

@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, apollo@2.1.3

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

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

My issue seems to be fixed as of 2.1.8

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

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

On MacOS and apollo@2.1.3 my files with .gql extension weren’t included. #740

I was able to get this to work by upgrading to apollo@2.1.3. I am on on MacOS.

release 2.1.3 is now working perfectly for me

@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/**

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!

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

I just saw in another issue thread this command was working in apollo@1.9.2. I downgraded and yay it worked fine.