Apollo-tooling: client:codegen is broken

Created on 31 Jan 2019  ·  5Comments  ·  Source: apollographql/apollo-tooling

Maybe I'm just not reading the documentation right, but there is no explanation of this issue, I have no idea what the error logs even mean. Does "client:codegen" not supposed to generate type definitions?

Intended outcome:
I'm trying to generate type definitions, so it should generate types

Actual outcome:
Doesn't generate types and shows errors

How to reproduce the issue:
Create a graphql server and run:

apollo client:codegen myFile.ts --target=typescript --endpoint=http://localhost:3000/graphql

Versions

"apollo-server": "^2.0.5",
"graphql": "^0.13.2",
"apollo": "^2.3.1".

I'm getting these errors for all of the types, I have no idea what that even means...

 ›   Type "Writer" already exists in the schema. It cannot also be defined in this type definition.
 ›
 ›   Field "Writer.id" already exists in the schema. It cannot also be defined in this type extension.
 ›
 ›   Field "Writer.name" already exists in the schema. It cannot also be defined in this type extension.
....

If I try to use schema:download and then generate the types from a local file using:

apollo client:codegen file.ts --target=typescript --localSchemaFile=.graphql/schema.json

I get this error:

    Generating query files
 ›   Error: Error initializing Apollo GraphQL project "Unnamed Project": Error: Error in "Loading schema for Unnamed Project": TypeError: 
 ›   graphql_1.isTypeSystemDefinitionNode is not a function

Most helpful comment

Wait, one more idea

How to reproduce the issue:
Create a graphql server and run:

You're trying to generate typings for server? I.e get types and resolvers singatures from server SDL for apollo-server? If thats the case then apollo's client:codegen is not a tool for that. It generate typings for client-side graphql queries - OperationDefinitionNodes

You'll want to use something like https://github.com/dotansimha/graphql-code-generator or https://github.com/prisma/graphqlgen for server side

All 5 comments

It's working. You just need to tweak your apollo.config.js

Your first example:
You probably downloaded your schema as .graphql somewhere in your project (or redeclared same types) and now when trying to run codegen from remote endpoint apollo detects both and correctly report some nodes as duplicated definitions.

To fix it: add exclude field in config, ie:

module.exports = {
  client: {
    name: 'name',
    service: 'service-name',
    tagName: 'graphql',
    includes: ['src/**/*.{ts,tsx}'],
    excludes: ['src/generated/**']
  },
  engine: {
    apiKey: process.env.ENGINE_API_KEY,
  },
}

https://www.apollographql.com/docs/references/apollo-config.html

I'm not sure what happens in your local schema file command, but I would wager you're running it over something weird from node_modules. Excludes/Includes may fix it.

Random tip: Do not try to have service (under first level service key in config) and client configured in one config. I had an issue with that.

Random tip 2: Make sure you have only one version of graphql installed.

Wait, one more idea

How to reproduce the issue:
Create a graphql server and run:

You're trying to generate typings for server? I.e get types and resolvers singatures from server SDL for apollo-server? If thats the case then apollo's client:codegen is not a tool for that. It generate typings for client-side graphql queries - OperationDefinitionNodes

You'll want to use something like https://github.com/dotansimha/graphql-code-generator or https://github.com/prisma/graphqlgen for server side

@vadistic That was not clear from the documentation, especially since it does allow me to download the DSL.
Sorry for the false alarm then, but please update the documentation maybe? mention that it is only for clients?

Well - to clarify: apollo cli allow downloading server SDL for client type generation without continuously running endpoint (or apollo engine). It also does parse and generate typings based on your server & client side SDL - but it's used for typing for client operations.

Tbh I also tried to use it for server schema some time ago, so maybe docs are slightly lacking a word or two. But you know - it's client:codegen so maybe we should know better^^

@liron-navon @vadistic glad this got sorted out! We're still working a bit on clarifying our errors–they're a bit all over the place at the moment, so thanks for your patience 🙈

We're working on some of our platform/cli docs as well pretty soon, so I'll keep this in mind. Thanks for bringing it up! If this issue is good to go then, I'll close it. If not, feel free to say so 😄

Was this page helpful?
0 / 5 - 0 ratings