-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Enum as scalar type #496
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Unfortunately My suggestion would be to simply remove I'm leaving this issue open as "Strawman" in case I missed something about your motivating use case |
I agree with you, in my case I just create a generic Enum with all values supported. But ideally would be nice to enforce a specific enum in the implementation. As well, when I tried to leave the interface without |
You may need an extension which allows you to map types to types:
Where
An implementation can take two paths:
The above is sometimes called a higher kinded type. [0] The |
I didn't know that we can use it in a GraphQL file. Will try it today. Looks good. Could you link any graphql doc or source related to this? |
Hi, I am a GraphQL newbie here. Please let me know if any of my statement is wrong or there are better ways to do things. I am planning out the schema for a new project. Meanwhile, I try to find a good practice for error handling. Without the ability to define a generic enum field in an interface, it makes the query of union very verbose. For example type Mutation {
greetThemAll(userId: string!, dogId: String!, catId: String!): GreetThemAllResult
}
interface Error {
message: String!
code: Enum!
field: Enum!
}
type UserError implements Error {
message: String!
code: ErrorCodeEnum! # e.g. RECORD_NOT_FOUND, INVALID_MESSAGE
field: UserFieldEnum! # e.g. ID, AGE
}
type DogError implements Error {
message: String!
code: ErrorCodeEnum!
field: DogFieldEnum!
}
type CatError implements Error {
message: String!
code: ErrorCodeEnum!
field: CatFieldEnum!
}
union GreetThemAllResult = Success | UserError | DogError | CatError | ...more Ideally, I want a simple query mutation {
greetThemAll(userId: "1", dogId: "2", catId: "3") {
... on Error {
message
code
field
}
}
} Now because there is no generic enum. I have to # schema
interface Error {
message: String!
# code: Enum!
# field: Enum!
}
# client query
mutation {
greetThemAll(userId: "1", dogId: "2", catId: "3") {
... on Error {
message
}
... on UserError {
code
field
}
... on DogError {
code
field
}
... on CatError {
code
field
}
# ... and more if I have more types under the union
}
}
More importantly, the verbose version also makes the client query much less future-proof. As the lean version can support any future added error types as long as they are under the correct interface. The verbose version has the risks of missing new error types in response. |
Generic enums feels like more a case for first-class generics rather than creating another form of type hierarchy. And I agree: the Generic Error use case is the most obvious reason to support generics. Basically you want to define something like:
|
Yes supporting full feature generic type would certainly be an awesome thing. I'd even hope Graphql type system to be turing completed. However, if there will be a huge amount of effort and delay implementing that. I think it wouldn't hurt to take baby steps to support generic Enum first. In future, generic Enum like |
Sometimes, it's useful for declaring an interface that expect to receive an Enum, but you don't want to specify it on interface level, but in the implementation level.
Consider the example:
I tried to replicate this example as minimum as possible, if needed I can provide a more complex one.
But in this case, today if we want to achieve a behavior like this, we need to keep unit out of our interface, because we can't specify a generic type and override it in the implementation of the interface.
The text was updated successfully, but these errors were encountered: