Graphql-code-generator: Enums and interfacePrefix config don't work together

Created on 22 Feb 2019  Β·  9Comments  Β·  Source: dotansimha/graphql-code-generator

Describe the bug
If you have an enum in your schema and choose to use the 'interfacePrefix' option, the generated code won't compile

To Reproduce
Steps to reproduce the behavior:
Schema:

schema {
 query: Query
}

type Query {
 me: User
 user(id: ID!): User
 allUsers: [User]
}

enum Role {
 USER
 ADMIN
}

type User {
 id: ID!
 username: String!
 email: String!
 role: Role!
 persona: ShowPersona
}

enum ShowPersona {
 β€œβ€"No one may see this persona.β€œβ€"
 NO_ONE

 β€œβ€"This persona is public.β€œβ€"
 EVERYONE

 β€œβ€"This persona is limited to the player’s friends.β€œβ€"
 FRIENDS

 β€œβ€"
 This persona is available to the player’s friends and friends of friends.
 β€œβ€"
 FRIENDS_OF_FRIENDS
}

Documents:

query findUser($userId: ID!) {
 user(id: $userId) {
   ...UserFields
 }
}

fragment UserFields on User {
 id
 username
 role
 persona
}

config:

config:
    interfacePrefix: β€œI”
generates:
 client-types.ts:
   - add: β€œ// THIS IS A GENERATED FILE, DO NOT EDIT IT!”
   - typescript-common
   - typescript-client

Expected behavior
Generated code should compile

Schema/Documents
See above

Environment:

  • macOS: 10.14.1

    • graphql-code-gen: 0.16.0

    • react-native: 0.58.5

    • typescript: 3.3.0

      Also reproducible on the live demo site (https://graphql-code-generator.com/#live-demo)

Additional context
The generated code adds I to the enum name, but the usage of the enum does not have the I included. If you simply add the interfacePrefix to the live-demo site, you can also reproduce the issue.

It generates export enum IRole, but then generates the UserFragment using just Role

bug plugins waiting-for-release

Most helpful comment

confirmed, fixed my issue. Thank you πŸ™‡

All 9 comments

1355 should fix it

πŸŽ‰ awesome, thank you!

@jeffreyffs could you check if 0.18.0-alpha.852a57ce fixes the issue?

confirmed, fixed my issue. Thank you πŸ™‡

Fixed in 0.18.0 πŸŽ‰

I'm not sure if this is best served in a new issue but it seems related to this issue. According to the docs regarding the interfacePrefix config, I get the impression that only interfaces would be prefixed:

This will cause the codegen to prefix graphql type interfaces with the given value (if I is defined as prefix, type Foo will be generated as an interface named IFoo).

In addition to the documentation, it's rather unusual in TypeScript conventions for enums to be prefixed like interfaces. While the generated TypeScript now compiles after #1355, I suspect the change that would have also fixed this issue while respecting TS conventions would have been to disregard the interfacePrefix config for enums altogether and never add any prefix for generated enums.

@dotansimha, have I misunderstood both the interfacePrefix config option and TypeScript enum naming convention, or was #1355 perhaps a fix but in the wrong direction?

@opiation you are right. It was due to the fact that interface referred to the TypeScript interfaces we are generating, and not the GraphQL interfaces.
In the recent refactor we changed it's name to be typesPrefix because we are generating TypeScript types instead of interfaces.

I think this summarize it:
GraphQL type => TypeScript type
GraphQL enum => TypeScript enum (or type, there is a config for that)
GraphQL interface => TypeScript type
GraphQL union => TypeScript type

Okay. Thanks for the clarification. So if I understand correctly, the new typesPrefix option will prefix all generated TypeScript types and enums with a given string?

@opiation yeah exactly.

Was this page helpful?
0 / 5 - 0 ratings