Enums
Enums are a special kind of type that is restricted to a particular set of values.
For example, we have a few options of ice cream available, and we want to allow user to choose only from those options.
Strawberry supports defining enums using enums from python’s standard library. Here’s a quick tutorial on how to create an enum type in Strawberry:
First, create a new class for the new type, which extends class Enum:
Then, list options as variables in that class:
Finally we need to register our class as a strawberry type. It's done with the
strawberry.enum
decorator:
In some cases you already have an enum defined elsewhere in your code. You can safely use it in your schema and strawberry will generate a default graphql implementation of it.
The only drawback is that it is not currently possible to configure it
(documentation / renaming or using strawberry.enum_value
on it).
Let’s see how we can use Enums in our schema.
Defining the enum type above would produce this schema in GraphQL:
Here's an example of how you'd use this newly created query:
Here is result of executed query:
We can also use enums when defining object types (using strawberry.type
). Here
is an example of an object that has a field using an Enum:
And here's an example of how you'd use this query:
Here is result of executed query:
GraphQL types are not a map of name: value, like in python enums. Strawberry uses the name of the members of the enum to create the GraphQL type.
You can also deprecate enum value. To do so you need more verbose syntax using
strawberry.enum_value
and deprecation_reason
. You can mix and match string
and verbose syntax.