Graphql-code-generator: [Federation] can't generate types when federation config is set to true

Created on 27 Nov 2019  路  10Comments  路  Source: dotansimha/graphql-code-generator

Describe the bug
When federation config is set to true, graphql-codegen --config codegen.yml results in an error stating, that codegen was unable to merge GraphQL directive "requires" (See picture below).

To Reproduce
Get a copy of the Apollo-Federation-Demo and initialize every service with graphql-codegen. Create a codegen.yml file as stated below and try to generate types.

My codegen.yml config file:

overwrite: true
schema: "http://localhost:<depending on the Service: 4001, 4002, 4003, 4004>/graphql"
documents: null
generates:
  generated/graphql-types.ts:
    plugins:
      - "typescript"
      - "typescript-resolvers"
    config:
      federation: true

Expected behavior
TypeScript types will be generated.

Environment:

  • OS: Windows 10 Enterprise
  • graphql-codegen cli + plugins are all v. 1.9.0
  • NodeJS: v 12.13.0

Additional context
generate federation types error

bug plugins waiting-for-release

Most helpful comment

overwrite: true
schema: 
  'src/schema/**/*.ts':
    noRequire: true
generates:
  src/generated/graphql.ts:
    plugins:
      - 'typescript'
      - 'typescript-resolvers'
    config:
      federation: true

Does seem to do the trick.

All 10 comments

+1
OS: Ubuntu 18.04.3 LTS
Same config, same versions, same output.

@Otard95 @Novalis15 Could you remove federation: true and try again?

Removing federation: true it runs like a charm. However After testing the demo provided and seeing that is worked, it possible my issue has a different source. I'll investigate and get back to you.

@Otard95 In that case, you wouldn't get correct typings for resolvers. Until it gets fixed, I recommend to point actual schema files instead of URL endpoint.

overwrite: true
schema: 
  'src/schema/**/*.ts':
    noRequire: true
generates:
  src/generated/graphql.ts:
    plugins:
      - 'typescript'
      - 'typescript-resolvers'
    config:
      federation: true

Does seem to do the trick.

As @Otard95 mentioned above, referencing the schema directly let graphql-codegen generate types:

overwrite: true
schema: './schema.ts'
generates:
  generated/graphql-types.ts:
    plugins:
      - 'typescript'
      - 'typescript-resolvers'
    config:
      federation: true

with schema.ts being:

import { gql } from "apollo-server";

const typeDefs = gql`
extend type Query {
  me: User
}

type User @key(fields: "id") {
  id: ID!
  name: String
  username: String
}
`;

export default typeDefs

Fixed in v1.10.0.

I'm having a similar issue with a directive from grapher. Is there something that we can do on the user side to make directives compatible?

    Error: Unable to merge GraphQL directive "link". 
    Existing directive:  
        directive @link(field: String, to: String, metadata: Boolean, unique: Boolean, autoremove: Boolean) on FIELD_DEFINITION 
    Received directive: 
        directive @link(overrideType: String) on FIELD_DEFINITION

@dotansimha While codegen is working good with federation: true for me, I am facing this issue: https://github.com/graphql/vscode-graphql/issues/250 with the VSCode extension which uses the same config. Have you managed to get it working with vscode-graphql as well? Thanks.

@tvvignesh this is weird, we have a piece of code that makes sure not to add the directives if they are already loaded. So adding the directives to your schema under graphql-config, and adding federation: true to your configuration should work...

https://github.com/dotansimha/graphql-code-generator/blob/ac067ea088011cd43a145aa1b021f76f3603b1eb/packages/graphql-codegen-core/src/codegen.ts#L52

Can you please report as new issue with a reproduction?

Was this page helpful?
0 / 5 - 0 ratings