I have the following schema:
enum PaymentTypeName {
PAYMENT_CARD
PAYMENT_CARD_TOKEN
}
interface Payment {
id: ID!
type: PaymentTypeName!
}
type PaymentCardPayment implements Payment {
id: ID!
type: PaymentTypeName!
card: PaymentCard!
}
type PaymentCardTokenPayment implements Payment {
id: ID!
type: PaymentTypeName!
card: PaymentCard!
}
When Payment
is PaymentCardPayment
or PaymentCardTokenPayment
is determined by the value of type, i.e. it is either PAYMENT_CARD
or PAYMENT_CARD_TOKEN
.
How do I signify in the interface, that PaymentCardPayment
/ PaymentCardTokenPayment
inherit a specific value of PaymentTypeName
?
I have tried various combinations of:
type PaymentCardPayment implements Payment {
id: ID!
type: PaymentTypeName.PAYMENT_CARD!
card: PaymentCard!
}
and:
type PaymentCardPayment implements Payment {
id: ID!
type: PaymentTypeName[PAYMENT_CARD]!
card: PaymentCard!
}
but all of these prompt a syntax error and I was unable to find the relevant documentation.
Introspection allows you to ask a GraphQL schema for information about what queries it supports. In the introspection system, there are 6 introspection types we can use __Schema, __Type, __TypeKind, __Field, __InputValue, __EnumValue, __Directive. To query the enums you need to use the __Type query resolver.
Enums limit the range of a variable's values to a set of predefined constants, which makes it easier to document intent. GraphQL also has enum type support, so TypeGraphQL allows us to use TypeScript enums in our GraphQL schema.
You don't have to worry about sending data as enum, just send it as String otherwise you will have to face JSON error, Graphql will take care of the value you send as a string (like 'MALE' or 'FEMALE') just don't forget to mention that gender is type of Gender(which is enum) in gql mutation as I did above.
What is an enum ? An enum is a GraphQL schema type that represents a predefined list of possible values. For example, in Airlock, listings can be a certain type of location: a spaceship, house, campsite, apartment, or room. We represent this in our schema through a locationType field.
What you're trying to do is not supported in GraphQL. If a field's type is declared to be PaymentTypeName
, and PAYMENT_CARD
and PAYMENT_CARD_TOKEN
are both valid values of PaymentTypeName
, then they must also be valid values for that field. There is no way to take an existing type9 whether it's an enum, scalar, or object type) and conditionally create a subset of possible values from the set of possible values already defined by the type.
That said, if PaymentCardPayment.type
will always resolve to PAYMENT_CARD
and PaymentCardTokenPayment.type
will always resolve to PAYMENT_CARD_TOKEN
, then it doesn't really make sense to use an enum here at all. In fact, in this specific case, we can omit the type
field entirely. After all, the only purpose of the field in this case is to allow the client to distinguish between the possible types that Payment
may resolve to. However, GraphQL already provides us with a __typename field that does just that by resolving to the name of the resolved type.
So in this specific instance, it's sufficient to just do:
interface Payment {
id: ID!
}
type PaymentCardPayment implements Payment {
id: ID!
card: PaymentCard!
}
type PaymentCardTokenPayment implements Payment {
id: ID!
card: PaymentCard!
}
and query a field whose type is Payment
like this:
{
payments {
id
__typename
... on PaymentCardPayment {
card {
# ...
}
}
... on PaymentCardTokenPayment {
card {
# ...
}
}
}
}
You are trying to declare the field value in your type schema, which is not what a schema is meant for. You should only be declaring your field type within your schema, in this case it is just type: PaymentTypeName
. You have it correct in your first code block.
Your PaymentCardPayment
's type
resolver function should return the value of the enum, in your case, PAYMENT_CARD
.
Your PaymentCardTokenPayment
's type
resolver function should return the value of PAYMENT_CARD_TOKEN
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With