Previously, I just typed my input KeyInput
as mode: String!
, and I'm looking to change the type from String! to a custom enum.
I have the tried following schema:
enum Mode= {
test
live
}
input KeyInput = {
mode: Mode!
}
type Key {
name,
mode
}
type Query {
Keys(input: KeyInput): [Key]
}
And my query looks like this:
query{
Keys(input: {mode: "test"}){
name
}
}
However, I get the following error:
"message": "Expected type Mode!, found \"test\"; Did you mean the enum value test?"
Is it possible to have enum values resolve String values? If I remove the quotes from the input, it will work. However, I need to be able to continue to resolve the Mode as Strings.
If you want to define a local enum on your client, you can use the typeDefs property during ApolloClient initialization: const client = new ApolloClient({ cache, typeDefs: gql` enum OrderTypes { FULL_BUY, PINK_BUY } `, });
The __typename field returns the object type's name as a String (e.g., Book or Author ). GraphQL clients use an object's __typename for many purposes, such as to determine which type was returned by a field that can return multiple types (i.e., a union or interface).
An enumeration type (or enum type) is a value type defined by a set of named constants of the underlying integral numeric type. To define an enumeration type, use the enum keyword and specify the names of enum members: C# Copy.
From the spec:
GraphQL has a constant literal to represent enum input values. GraphQL string literals must not be accepted as an enum input and instead raise a query error.
Query variable transport serializations which have a different representation for non‐string symbolic values (for example, EDN) should only allow such values as enum input values. Otherwise, for most transport serializations that do not, strings may be interpreted as the enum input value with the same name.
In other words, when using an enum as an input, if you're using it as a literal value, like this:
Keys(input: { mode: test }) {
name
}
the enum value (test
in this case) cannot be quoted, unlike Strings literals, which must be surrounded by double quotes.
On the other hand, if you're using a variable to substitute an enum value, you'll set the value of the variable to a string, just like you would for a regular String value:
Keys(input: { mode: $mode }) {
name
}
// in your component...
variables: {
mode: 'test'
}
Because JSON doesn't include the concept of enum values, whenever we deal with a JSON context (for example, when declaring variables, or when returning the data
for our request), enum values are simply serialized as string values.
All that aside, if you're using Apollo as a client, whether you have to include the quotation marks or not should be irrelevant -- if you need to substitute the value for the mode
input field, you should be using variables to do so, in which case (as shown above), you would be passing in a string value regardless.
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