Defining GraphQL types
Strawberry uses Python classes, functions, enums, and annotations to define the public GraphQL type system. Decorators give Python declarations a GraphQL role, while annotations describe the values that fields and arguments accept or return. strawberry.Schema connects these declarations through query, mutation, and subscription roots. For the wider model, see Schema and type system; for annotation rules, see Type annotations and resolution; for the conversion boundary, see Schema conversion and printing.
Definition model
A Strawberry definition is a Python declaration with GraphQL meaning. A decorated class defines a named GraphQL type, its annotated members become fields or input fields, and decorated functions provide work for requested fields. The schema builder follows these links from the roots and turns them into the GraphQL type system.
The main declaration roles are:
| Python declaration | GraphQL role | Main use |
|---|---|---|
@strawberry.type class | Object type | Return data and group fields |
@strawberry.input class | Input object | Receive structured client input |
@strawberry.interface class | Interface | Define fields shared by object types |
@strawberry.enum enum | Enum | Limit a value to named choices |
@strawberry.scalar type | Scalar | Define leaf-value parsing and output rules |
Annotated[Union[...], strawberry.union(...)] | Union | Return one of several object types |
@strawberry.field member | Field | Expose a value or resolver |
strawberry.argument(...) metadata | Argument | Describe a field parameter |
@strawberry.mutation member | Mutation field | Change state through the mutation root |
Decorated classes keep their Python identity for resolver code and also carry a Strawberry definition. That definition records the GraphQL name, kind, fields, descriptions, directives, and relationships to other types. The class decorator also gives the type dataclass behavior so application code can create returned values and input values in Python. A subclass intended as a separate schema type must have its own declaration; inheriting a parent definition does not create a new published type.
References: strawberry/…/base.py• L36-65 strawberry/…/base.py• L272-325 strawberry/…/object_type.py• L348-431
Object type declarations
The @strawberry.type decorator marks a class as a GraphQL object type. Its annotated members and resolver methods become fields, and the class name is converted to a GraphQL name unless the decorator sets another name.
@strawberry.type
class User:
id: strawberry.ID
display_name: str
@strawberry.field
def greeting(self) -> str:
return f"Hello, {self.display_name}"
Use object types for values that clients can select in a query or receive from a mutation. A field may read a stored attribute or call a resolver when the value needs work at request time. Object types can inherit fields from other Strawberry object types and can implement interfaces through their Python class relationship.
The decorator can set the public name, description, directives, and type-extension status. Field metadata can also be placed in an outermost Annotated declaration. When a resolver supplies the field type, Strawberry uses its return annotation unless the field declares a type override. An annotation added only to make a resolver field compatible with Python dataclass creation is removed after that step, so the resolver's synthetic type does not remain as a class attribute annotation for other tools.
References: strawberry/…/object_type.py• L218-313 strawberry/…/object_type.py• L348-556 strawberry/…/object_type.py• L202-215
Input type declarations
The @strawberry.input decorator marks a class as a GraphQL input object. Its fields describe values that clients may send to a field, and Strawberry converts received input into the Python class shape before calling the resolver.
@strawberry.input
class UserInput:
display_name: str
age: int | None = None
Input types are separate from output object types because GraphQL applies different rules to values sent to the server and values returned by it. An input can carry descriptions, directives, defaults, and the one_of constraint. A one-of input requires the client to provide exactly one non-null member.
strawberry.Maybe[T] is useful when omission and explicit null have different meaning. Maybe[T | None] permits both omission and explicit null. During input conversion, nested values become their declared Python types, while invalid nulls and invalid scalar values become input-coercion errors. Input fields cannot use resolvers because the fields must be constructible from the client-supplied input.
References: strawberry/…/object_type.py• L316-328 strawberry/…/object_type.py• L367-390 strawberry/…/object_type.py• L559-638 strawberry/…/arguments.py• L213-364
Interface declarations
The @strawberry.interface decorator defines a shared field contract for object types. A field typed as an interface can return different implementing objects, while clients select the fields guaranteed by the interface and use GraphQL fragments for type-specific fields.
@strawberry.interface
class Node:
id: strawberry.ID
@strawberry.type
class User(Node):
name: str
An implementing object inherits the interface relationship through Python class structure. At runtime, Strawberry resolves the concrete object so GraphQL can report the correct type. Use an explicit resolve_type or is_type_of when the returned Python value does not provide enough information through the normal class relationship.
References: strawberry/…/object_type.py• L202-215 strawberry/…/object_type.py• L362-403 strawberry/…/object_type.py• L641-711
Enum declarations
The @strawberry.enum decorator maps a Python Enum to a GraphQL enum. GraphQL clients use the public enum member names, while Python resolvers work with the Python enum values.
from enum import Enum
@strawberry.enum
class UserRole(Enum):
ADMIN = "admin"
MEMBER = "member"
By default, GraphQL names come from Python member keys. graphql_name_from="value" selects member values instead. Use strawberry.enum_value(...) when one member needs a public name, description, deprecation reason, or directive that differs from the Python declaration. The enum itself can also carry a public name and description.
References: strawberry/…/enum.py• L63-101 strawberry/…/enum.py• L108-183 strawberry/…/enum.py• L188-268
Scalar declarations
A scalar is a GraphQL leaf type. It represents a value with no selectable subfields, such as an identifier, date, or application-specific encoded value. Strawberry provides common scalar primitives and lets a schema define rules for output serialization and client input parsing.
Base64 = strawberry.scalar(
name="Base64",
serialize=encode_base64,
parse_value=decode_base64,
)
The scalar name is part of the public schema contract. A scalar definition can include a description, specification URL, directives, and separate rules for variables and literal values. The built-in registry maps Python primitives and common values such as ID, dates, times, UUIDs, decimals, JSON, uploads, and Base16/Base32/Base64 data to GraphQL scalar definitions.
Output serialization and input parsing form one contract. A value must be readable from client input and representable in responses according to the scalar definition. Invalid client values become input-coercion errors.
References: strawberry/…/scalar.py• L21-100 strawberry/…/base_scalars.py• L13-44 strawberry/…/base_scalars.py• L50-120 strawberry/…/scalar.py• L36-229
Union declarations
A union describes a field that may return one of several object types without requiring those objects to share fields. Strawberry defines a named union by attaching strawberry.union(...) metadata to a Python union annotation.
SearchResult = Annotated[
User | Company,
strawberry.union("SearchResult"),
]
Union members must be valid output object types. When a resolver returns a value, Strawberry uses an explicit strawberry.cast marker first when it names one of the union's object types. It then uses the declared object relationship or is_type_of to identify the concrete GraphQL type. A value outside the declared members is rejected at runtime, so resolver return values and public union members must stay aligned.
strawberry.cast(Type, value) is useful for values from another model that look like a Strawberry object but are not instances of it. The marker selects a matching union object type. An ambiguous cast raises an error instead of choosing silently.
References: strawberry/…/union.py• L47-97 strawberry/…/union.py• L170-235 strawberry/…/union.py• L257-304 strawberry/…/cast.py• L28-73
Field declarations
The @strawberry.field decorator exposes a class member as a GraphQL field. A plain annotated member reads a value from the returned object, while a decorated method supplies a resolver that can compute a value and receive field arguments or strawberry.Info.
@strawberry.type
class Query:
welcome: str = "Welcome"
@strawberry.field(name="currentUser")
def current_user(self) -> User:
return load_current_user()
Field metadata controls the public name, description, deprecation, directives, permissions, extensions, and optional schema type override. A resolver return annotation normally supplies the field type. An explicit class annotation takes priority when both are present. The graphql_type option supplies the schema type when the resolver's Python type and its public GraphQL type must differ.
Resolver parameters named or typed as root, parent, or strawberry.Info are runtime inputs for resolver execution. Strawberry does not expose them as client arguments. Other parameters become GraphQL arguments and need annotations. A field decorator in Annotated must be outermost so it applies to the field itself. If a resolver field lacks a usable class or return annotation, Strawberry cannot define its GraphQL type.
The object decorator prepares resolver fields for dataclass creation, then removes annotations that it added only for that preparation. This keeps the Strawberry field definition available while avoiding a synthetic resolver annotation on the finished Python class.
References: strawberry/…/field.py• L86-177 strawberry/…/field.py• L203-245 strawberry/…/field.py• L341-387 strawberry/…/field.py• L452-480 strawberry/…/field.py• L579-658 strawberry/…/object_type.py• L104-199 strawberry/…/object_type.py• L218-313
Argument declarations
Field parameters become GraphQL arguments through their Python annotations. Use strawberry.argument(...) inside typing.Annotated when an argument needs GraphQL metadata without changing the Python value passed to the resolver.
@strawberry.field
def user(self, user_id: Annotated[str, strawberry.argument(name="id")]) -> User:
return load_user(user_id)
Argument metadata supports a public name, description, deprecation reason, directives, custom metadata, and a schema type override. Strawberry maps the GraphQL argument name back to the Python parameter name and converts nested input values before calling the resolver. Only one strawberry.argument(...) declaration may describe a parameter. Other Annotated metadata remains attached to the underlying type, so argument metadata does not hide type metadata such as enum or lazy-type information.
strawberry.Maybe[T] lets a resolver distinguish an omitted argument from a supplied value. strawberry.Maybe[T | None] also permits explicit null, while Maybe[T] rejects explicit null. This distinction matters when omission means “leave unchanged” but null means “clear the value.” A supplied value is represented by Some, while omission is represented separately from that value.
References: strawberry/…/arguments.py• L38-147 strawberry/…/arguments.py• L213-364 strawberry/…/arguments.py• L376-413 strawberry/…/arguments.py• L416-464 strawberry/…/base.py• L162-174 strawberry/…/maybe.py• L14-68
Mutation declarations
The @strawberry.mutation decorator marks a field as a state-changing operation. It uses the same resolver and argument model as a normal field, but the field belongs in the type passed as the schema's mutation root.
@strawberry.type
class Mutation:
@strawberry.mutation
def rename_user(self, user_id: strawberry.ID, name: str) -> User:
return rename_user(user_id, name)
A mutation can be synchronous or asynchronous and can use field metadata such as permissions, deprecation, directives, and extensions. Keeping mutations on a separate root tells GraphQL clients which operations may change server state and preserves GraphQL execution rules for those operations.
References: strawberry/…/mutation.py• L31-184 strawberry/…/schema.py• L256-313 strawberry/…/schema.py• L352-374
Type annotation primitives
Python annotations provide the type shape used by fields, arguments, and nested input values. Strawberry resolves them into GraphQL leaf types, named types, lists, nullability wrappers, enums, and unions. See Type annotations and resolution for the full annotation model.
The common primitives are:
| Python form | GraphQL meaning |
|---|---|
str, int, float, bool, strawberry.ID | Built-in or registered scalar |
list[T], tuple[T, ...], Sequence[T] | List of T |
T | None, Optional[T] | Nullable T |
A | B or Union[A, B] | Union when used as an output type and named with strawberry.union(...) |
Annotated[T, metadata] | T plus Strawberry declaration metadata |
strawberry.Maybe[T] | Input value that distinguishes an omitted value from a supplied value |
Strawberry represents list, optional, and maybe shapes as wrappers around another type. A non-null annotation becomes a required GraphQL value. Adding None makes the value nullable. Maybe adds a separate omitted state for input values: a supplied value is wrapped in Some, while omission is represented by None.
Types are resolved when Strawberry has the right namespace and enough declaration context. Forward references and lazy types support declarations whose names are not available at the first point of evaluation. When an annotation contains field options that cannot yet be evaluated, Strawberry checks the names used by those options and raises an unresolved-field error rather than dropping the options.
References: strawberry/…/base.py• L93-174 strawberry/…/base.py• L218-295 strawberry/…/maybe.py• L14-68 strawberry/annotation.py• L1-250 strawberry/…/unresolved_annotations.py• L1-219
Root schema types
Root types are the entry points that connect declarations to client operations. A schema requires a query root and may add separate mutation and subscription roots. Fields reached from those roots define the types clients can use.
schema = strawberry.Schema(
query=Query,
mutation=Mutation,
subscription=Subscription,
)
The root classes use the same @strawberry.type and field declarations as other object types. The schema may also receive extra named types when a type is not reachable from a root field. Schema construction converts the roots and validates the resulting GraphQL relationships before the schema is used.
This diagram shows how Python declarations become operation entry points and the reachable GraphQL type graph.
flowchart LR
A[Python type declarations] --> B[Query root]
A --> C[Mutation root]
A --> D[Subscription root]
B --> E[Reachable output and input types]
C --> E
D --> E
E --> F[GraphQL schema]
F --> G[Client operations]
References: strawberry/…/schema.py• L256-313 strawberry/…/schema.py• L341-374 strawberry/…/schema.py• L380-440
Change checklist
When these files or sections are changed, remember to consider these:
- A change to a decorator's public name or a type annotation can change field lookup, argument coercion, introspection, and generated SDL at the same time.
- A change to object decoration must preserve the distinction between annotations needed to create the dataclass and annotations that should remain on the finished Python class.
- A change to an object subclass used in the schema must ensure it has its own declaration rather than relying on an inherited definition.
- A change to an interface or union member must keep runtime concrete-type resolution aligned with the set of GraphQL member types. This includes any
strawberry.castuse for values that are not instances of the declared class. - A change to scalar parsing or serialization must cover both values sent by clients and values returned by resolvers.
- A change to input defaults,
Maybe, or one-of behavior must preserve the difference between an omitted value and an explicit null. - A change to field or argument metadata inside
Annotatedmust preserve its outermost placement and the other type metadata that remains after Strawberry extracts the declaration metadata. - A change to a field or argument's
graphql_typeoverride must preserve the contract between the resolver value and the schema type exposed to clients. - A change to reserved resolver parameters must not expose
root,parent, orInfoas client arguments. - A change to a root type must preserve the schema's operation entry points and the reachability of types used only by those roots.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A field or resolver has no usable type annotation | Schema construction raises a missing annotation or unresolved field error | Annotate every exposed field or resolver return value |
A field option in an unresolved Annotated declaration uses a name that is not yet available | Strawberry raises an unresolved field error instead of silently dropping the option | Define field option dependencies before the type or use a resolvable reference |
| A field decorator is nested inside a list, optional, or other type wrapper | Strawberry rejects the annotation because the field metadata has no clear field target | Put strawberry.field(...) in the outermost Annotated metadata |
A resolver parameter is intended as runtime context but is not recognized as root, parent, or Info | The parameter becomes a client argument or fails argument analysis | Use the supported reserved parameter names or types |
| A resolver argument uses a union or interface type | Field creation raises an invalid argument type error because these are output types | Use an input object, enum, scalar, or other valid input type for arguments |
| An input type is used where an output object is required | Schema construction rejects the type relationship | Keep input declarations for arguments and output declarations for returned data |
| An input type contains a resolver field | Type creation rejects the input because its field cannot be supplied through the input constructor | Use annotated input data fields without resolvers |
| A union contains an input or non-object member | Schema construction raises an invalid union type error | Use only valid output object members in a union |
| A resolver returns a value that does not match an interface or union member | GraphQL reports a runtime type resolution error | Return a declared member, provide is_type_of, or use an unambiguous strawberry.cast |
| A cast value matches more than one possible generic object type | Runtime type resolution raises an ambiguous cast error | Cast to a type whose schema specialization is unique |
| A scalar parser receives an invalid value shape or content | GraphQL reports an input-coercion error | Keep parser input rules aligned with client values and use the scalar's conversion hooks |
| Two scalar definitions use the same GraphQL name with different rules | Schema construction raises a scalar registration error | Keep scalar names and mappings consistent across the schema |
| A subclass used as a schema type only inherits its parent's Strawberry definition | Schema construction raises an undecorated-subclass error | Decorate each subclass that represents a separate schema type |
| A schema has no query root or has an invalid root relationship | Schema creation fails before client execution | Provide a valid query type and validate root fields as part of schema setup |
Key files
| File | Why to read it |
|---|---|
| strawberry/__init__.py | Shows the public decorators and type primitives exposed by import strawberry. |
| strawberry/…/object_type.py | Defines object, input, and interface declarations and prepares their fields and class annotations. |
| strawberry/…/base.py | Defines the shared type-definition model and list, optional, maybe, and generic wrappers. |
| strawberry/…/maybe.py | Defines the value wrapper that preserves the difference between omission and a supplied input value. |
| strawberry/…/field.py | Defines field metadata, schema type overrides, argument checks, and the two ways to expose a value or resolver. |
| strawberry/…/resolver.py | Shows how resolver signatures become GraphQL arguments and how runtime parameters are excluded. |
| strawberry/…/arguments.py | Defines argument metadata and the path from GraphQL input to Python resolver values. |
| strawberry/…/enum.py | Defines enum names, values, and per-member metadata. |
| strawberry/…/scalar.py | Defines the scalar registry and the boundary between scalar definitions and GraphQL scalar types. |
| strawberry/…/base_scalars.py | Defines built-in date, time, decimal, UUID, and void scalar behavior. |
| strawberry/…/union.py | Defines named unions and their concrete member resolution. |
| strawberry/…/cast.py | Defines the marker used to resolve union and interface results from external object models. |
| strawberry/…/mutation.py | Defines mutation fields as operation-specific fields. |
| strawberry/…/unresolved_annotations.py | Explains how unresolved Annotated metadata is checked before field options are lost. |
| strawberry/annotation.py | Explains how Python annotations become Strawberry type primitives. |
| strawberry/…/schema.py | Shows how query, mutation, and subscription roots create the public schema. |
| strawberry/…/schema_converter.py | Shows how each declaration category becomes a GraphQL type at the schema boundary. |