Enums

Enums are a special kind of type that is restrict 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:

from enum import Enum
class IceCreamFlavour(Enum):

Then, list options as variables in that class:

class IceCreamFlavour(Enum):
VANILLA = "vanilla"
STRAWBERRY = "strawberry"
CHOCOLATE = "chocolate"

Finally we need to register our class as a strawberry type. It's done with the strawberry.enum decorator:

@strawberry.enum
class IceCreamFlavour(Enum):
VANILLA = "vanilla"
STRAWBERRY = "strawberry"
CHOCOLATE = "chocolate"

Let's see how we can use Enums in our schema.

@strawberry.type
class Query:
@strawberry.field
def best_flavour(self) -> IceCreamFlavour:
return IceCreamFlavour.STRAWBERRY

Defining the enum type above would produce this schema in GraphQL:

enum IceCreamFlavour {
VANILLA
STRAWBERRY
CHOCOLATE
}

Here's an example of how you'd use this newly created query:

query {
bestFlavour
}

Here is result of executed query:

{
"data": {
"bestFlavour": "STRAWBERRY"
}
}

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:

@strawberry.type
class Cone:
flavour: IceCreamFlavour
num_scoops: int
@strawberry.type
class Query:
@strawberry.field
def cone(self) -> Cone:
return Cone(flavour=IceCreamFlavour.STRAWBERRY, num_scoops=4)

And here's an example of how you'd use this query:

query {
cone {
flavour
numScoops
}
}

Here is result of executed query:

{
"data": {
"cone": {
"flavour": "STRAWBERRY",
"numScoops": 4
}
}
}

NOTE: 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.

Additional resources

Was this helpful?

Edit on Github

Newsletter 💌

Do you want to receive the latest updates on Strawberry? Subscribe to our newsletter!