Apollo-tooling: Error: No operations or fragments found to generate code for.

Created on 7 Feb 2019  Â·  10Comments  Â·  Source: apollographql/apollo-tooling

Intended outcome:

Running apollo client:codegen my-output --target swift --localSchemaFile schema.graphql should generate code on trivial example.

Actual outcome:

$ apollo client:codegen my-output --target swift --localSchemaFile schema.graphql
  ✔ Loading Apollo Project
  ✖ Generating query files with 'swift' target
    → No operations or fragments found to generate code for.
Error: No operations or fragments found to generate code for.
    at write (~/***/clients/ios/node_modules/apollo/lib/commands/client/codegen.js:58:39)
    at Task.task (~/***/clients/ios/node_modules/apollo/lib/commands/client/codegen.js:83:46)
    at <anonymous>
error Command failed with exit code 2.

How to reproduce the issue:

apollo.config.js:

module.exports = {
  client: {
    service: {
      name: 'API',
      localSchemaFile: 'schema.graphql', // also tried './schema.graphql'
    },
  },
};

schema.graphql:

// below is downloaded directly from AppSync
schema {
  query: Query
}

type Health {
  isRunning: Boolean
}

type Query {
  healthcheck: Health
}

// below is added as CLI suggested adding operations
query Healthcheck {
  healthcheck {
    isRunning
  }
}

Versions

node: v8.11.4
apollo: v2.4.4

Most helpful comment

@daviskoh ah, good catch! Yeah, that's by design.

Most of the time, the graphql schema is defined separately from the operations consuming it. Usually the operations are written next to whatever component (in the case of react) or view that uses it, so there may be dozens or hundreds of files, all defining operations that need types generated.

The includes option lets you define a glob to match where to look for those operations. If it's in one file like yours, --includes=myFile.graphql works great.

And to answer your last question, yes. We recommend writing out operations manually. GraphQL allows a near infinite number of possibilities for possible operations, so automatically generating operations isn't really possible. Tools like GraphQL Playground (included in apollo-server) or GraphiQL make building those operations pretty easy though. I hope that answers your questions! Feel free to ping me here or in spectrum if not :)

All 10 comments

Inspecting the this.project object in the apollo cli, I can see that it manages to find type defs such as Health and Query but seems to struggle w/ operations. Am I missing something? Is the schema wrong? I can confirm that the query Healthcheck runs fine in the AppSync console.

I have same problem on version 2.5.1 !

@daviskoh @dev4us this is common with issues around your project structure/default config options. Where is your source located?

The default client.includes is includes: ["src/**/*.{ts,tsx,js,jsx,graphql}"], so if your source doesn't match that glob, it won't find any operations.

@JakeDawkins
Hello,
here is my Source

(is not working) https://github.com/dev4us/kakao_talk-dev4us/tree/master/frontend
(is working) https://github.com/dev4us/Uber_dev4us/tree/master/frontend

In my knowledge, different between two project is 'created from OS'
first one (kakao_talk) is created from Windows
second one (uber) is created from MacOS

thanks for your feedback!

@JakeDawkins my entire schema is in a single schema.graphql file at the root of the project that is passed into the --localSchemaFile flag. Is apollo client:codegen expecting all files to to be under a /src dir? If so, what is the purpose of the --localSchemaFile flag? How does it work?

After some more investigating, I have discovered that operations need to be in a separate file in order for apollo client:codegen to work.

The full command then becomes:

$ apollo client:codegen my-output --target swift --localSchemaFile schema.graphql --includes operations.graphql

Is this a conscious design decision?

Also, is there an expectation on the user of the CLI to write out operations manually or is there a way to generate operations using a GraphQL schema somehow?

@daviskoh ah, good catch! Yeah, that's by design.

Most of the time, the graphql schema is defined separately from the operations consuming it. Usually the operations are written next to whatever component (in the case of react) or view that uses it, so there may be dozens or hundreds of files, all defining operations that need types generated.

The includes option lets you define a glob to match where to look for those operations. If it's in one file like yours, --includes=myFile.graphql works great.

And to answer your last question, yes. We recommend writing out operations manually. GraphQL allows a near infinite number of possibilities for possible operations, so automatically generating operations isn't really possible. Tools like GraphQL Playground (included in apollo-server) or GraphiQL make building those operations pretty easy though. I hope that answers your questions! Feel free to ping me here or in spectrum if not :)

@dev4us I don't see anything obviously wrong from my first look. How are you running codegen? I see you don't have a config file. What options are you passing in?

The OS that it was created on _shouldn't_ matter. Are you running codegen on the same OS or running one on windows/mac?

@JakeDawkins
Oh sorry, I'm late I don't have a Config File
Just execute to console Command

Like this

"scripts": {
"start": "react-scripts-ts start",
"build": "react-scripts-ts build",
"test": "react-scripts-ts test --env=jsdom",
"eject": "react-scripts-ts eject",
"precodegen": "apollo schema:download --endpoint=http://localhost:4000/graphql",
"codegen": "apollo codegen:generate src/types/api.d.ts --queries='src/*/!(.local).ts' --addTypename --localSchemaFile schema.json --target typescript --outputFlat"
},

Thanks for the work on this! I'm going to close this for now, as I believe this is a config issue. If this is still blocking for anyone, let me know and please provide a reproduction repo that can use to confirm.

Was this page helpful?
0 / 5 - 0 ratings