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:
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
π 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
Iis defined as prefix, typeFoowill be generated as an interface namedIFoo).
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.
Most helpful comment
confirmed, fixed my issue. Thank you π