Schema and type system
Strawberry turns Python classes and type annotations into a GraphQL schema. Its type model keeps the Python definition, the resolved field types, and the final GraphQL types connected so schema construction can validate the contract before request execution.
System purpose
Schema boundary
The public boundary is strawberry.Schema. It owns the GraphQL schema used for execution while retaining the Strawberry definitions and settings that created it. This gives request handling one validated view of the API instead of asking each resolver to rebuild type information (strawberry/…/schema.py• L306-429).
References: strawberry/…/schema.py• L256-313 strawberry/…/schema.py• L306-429
Schema assembly
Schema assembly starts with decorated Python types and ends with a validated GraphQL schema. The converter follows links between types as it builds them, so the final schema contains the reachable types and their relationships.
The main flow is:
flowchart LR
A[Python classes and annotations] --> B[Strawberry definitions]
B --> C[Resolved fields and arguments]
C --> D[GraphQL core types]
D --> E[Root types and reachable definitions]
E --> F[Validated GraphQL schema]
Construction stages
When a caller creates Schema, Strawberry performs these high-level steps:
- It accepts the query root and optional mutation and subscription roots.
- It resolves the definitions and types reachable from those roots. Extra types can extend that set.
- It applies names, scalar mappings, directives, and other schema settings during conversion.
- It creates and checks the GraphQL schema. Invalid relationships or incomplete definitions fail during construction (strawberry/…/schema.py• L341-493).
This order gives execution a stable contract. The same schema then parses and validates client documents. GraphQL types also keep a link to the Strawberry definitions that produced them, which lets runtime and tooling move between the GraphQL view and the Python view (strawberry/…/schema.py• L148-172, strawberry/…/schema.py• L457-492, strawberry/…/schema_converter.py• L384-400).
References: strawberry/…/schema.py• L341-493 strawberry/…/schema.py• L148-172 strawberry/…/schema_converter.py• L384-400
Schema directives
Schema directives add metadata to definitions such as types, fields, arguments, and enum values. Strawberry collects directive uses from the types reached from the roots and follows directive argument types so directive-only input types also enter the schema (strawberry/…/directive_collector.py• L16-25, strawberry/…/directive_collector.py• L75-147).
The schema combines explicit directives with directives found during type traversal. Repeated references to one definition share one GraphQL directive. Different definitions with the same GraphQL name are rejected (strawberry/…/schema.py• L495-564). Attached schema directives resolve their field annotations during schema construction, so an incomplete directive argument fails at the same boundary as an incomplete field (strawberry/…/schema_converter.py• L527-571).
References: strawberry/…/directive_collector.py• L16-25 strawberry/…/directive_collector.py• L50-99 strawberry/…/directive_collector.py• L106-147 strawberry/…/schema.py• L448-564 strawberry/…/schema_converter.py• L527-571
Type definition model
Decorators turn Python classes into named GraphQL types. Strawberry attaches a definition to each decorated class, and fields connect that definition to Python values or resolver results.
Object and input definitions
An object definition describes values that a GraphQL client can read. An input definition describes values that a client can send. An interface definition describes shared output fields that object types implement. These forms share a definition model with names, fields, descriptions, directives, and relationship data (strawberry/…/base.py• L298-323).
The @strawberry.type, @strawberry.input, and @strawberry.interface decorators create these definitions from classes. Class annotations become fields. Resolver methods provide fields whose values need active computation. Inheritance links object types to interfaces. A definition can also record a concrete use of a generic type so one Python shape can produce distinct GraphQL types when its type variables are filled (strawberry/…/base.py• L365-420, strawberry/…/object_type.py• L348-431). Schema entry points must use a class with its own definition; an undecorated subclass cannot silently reuse a parent definition (strawberry/…/base.py• L272-296). See Defining GraphQL types for declaration rules.
Definitions also carry input conversion hooks for integrations that need to build Python values from GraphQL input or expose an instance as a GraphQL input value. These hooks affect input values and defaults without changing the shared object-definition model (strawberry/…/base.py• L324-363).
References: strawberry/…/base.py• L272-323 strawberry/…/base.py• L324-363 strawberry/…/base.py• L365-420 strawberry/…/object_type.py• L202-215 strawberry/…/object_type.py• L348-431 strawberry/…/object_type.py• L434-551 strawberry/…/object_type.py• L559-638 strawberry/…/object_type.py• L641-711
Fields and arguments
A field connects a GraphQL name to a value on a Python object. It carries the field type and can also describe a resolver, arguments, descriptions, deprecation, directives, permissions, and extensions (strawberry/…/field.py• L86-176). Resolver arguments become GraphQL arguments, so a resolver signature helps define both the client contract and the values passed to the resolver (strawberry/…/resolver.py• L210-305).
Fields without a resolver use the configured default value lookup. Fields with a resolver use that resolver instead. Both remain part of the same type definition (strawberry/…/field.py• L203-255). Argument annotations can add GraphQL metadata through Annotated and can override the GraphQL type when the schema type differs from the Python annotation. Other annotation metadata remains available to type resolution (strawberry/…/arguments.py• L38-133).
After GraphQL validates an input, Strawberry converts argument values to the resolved Python types. This conversion follows list and wrapper types, builds input objects, preserves omitted values, and passes the input path to custom input builders (strawberry/…/arguments.py• L184-219, strawberry/…/arguments.py• L222-371).
References: strawberry/…/field.py• L86-176 strawberry/…/field.py• L203-255 strawberry/…/resolver.py• L210-305 strawberry/…/arguments.py• L38-139 strawberry/…/arguments.py• L184-219 strawberry/…/arguments.py• L222-371
Leaf and composite types
GraphQL leaf types carry values. Composite types contain fields or other named types. Strawberry represents leaves with scalar and enum definitions. It represents field-bearing types with object, input, and interface definitions. Unions represent alternative output objects (strawberry/…/base.py• L35-90, strawberry/…/enum.py• L14-43, strawberry/…/scalar.py• L36-63, strawberry/…/union.py• L45-63).
Lists and optional values wrap another type instead of creating a new named type. Custom scalars add the rules that move values between Python and GraphQL. An input can also use a one-of contract that requires one selected field rather than a normal set of input values (strawberry/…/base.py• L93-174, strawberry/…/object_type.py• L589-638, strawberry/…/schema_converter.py• L745-881).
References: strawberry/…/base.py• L35-90 strawberry/…/base.py• L93-174 strawberry/…/enum.py• L14-43 strawberry/…/scalar.py• L36-63 strawberry/…/union.py• L45-63 strawberry/…/object_type.py• L589-638
Annotation resolution
Python annotations link class declarations to the Strawberry type graph. StrawberryAnnotation evaluates an annotation in its Python namespace and turns supported forms such as lists, optional values, unions, enums, lazy references, and generic types into Strawberry types (strawberry/annotation.py• L58-70, strawberry/annotation.py• L108-205).
Resolution can wait when a type is declared later or lives in another module. A lazy reference keeps the target module and type name until schema work needs the real Python type. This lets related types refer to each other without an import cycle (strawberry/…/lazy_type.py• L20-71). Maybe is a runtime-visible generic input annotation; its Some value marks a supplied value while None marks omission, and its annotation form can still be recognized while a module is being built (strawberry/…/maybe.py• L14-18, strawberry/…/maybe.py• L39-67). Schema conversion rejects a field that remains unresolved because every GraphQL field must have a known type (strawberry/…/field.py• L342-377, strawberry/…/schema_converter.py• L241-274).
Field resolution validates the current annotation before it uses the resolved type. It remembers the exact annotation that passed validation, so a later replacement is checked again. Field configuration can also sit in typing.Annotated metadata. Strawberry turns that metadata into the field definition while preserving other annotation metadata. The field configuration must appear at the outer field boundary and must be defined only once (strawberry/…/field.py• L342-377, strawberry/…/field.py• L379-481, strawberry/…/object_type.py• L51-199). Argument metadata follows the same outer Annotated boundary and rejects multiple argument configurations (strawberry/…/arguments.py• L88-133). See Type annotations and resolution for deferred and typing-specific behavior.
References: strawberry/annotation.py• L130-205 strawberry/…/lazy_type.py• L20-71 strawberry/…/maybe.py• L14-18 strawberry/…/maybe.py• L39-67 strawberry/…/field.py• L342-377 strawberry/…/field.py• L379-481 strawberry/…/schema_converter.py• L241-274 strawberry/…/object_type.py• L51-199 strawberry/…/arguments.py• L88-133
Type wrappers
Strawberry keeps list and optional meaning in wrappers around an inner type. During GraphQL conversion, a normal annotation is non-null by default. An optional wrapper removes that requirement. Maybe uses the same optional GraphQL shape but adds an input state for an omitted value, so a resolver can distinguish omission from an explicit value (strawberry/…/base.py• L93-174, strawberry/…/maybe.py• L14-67, strawberry/…/schema_converter.py• L1392-1407, strawberry/…/arguments.py• L234-262).
A plain Maybe[T] rejects explicit null. Maybe[T | None] permits omission, null, and a value. The validation rule checks both arguments and nested input object fields, while input types with their own input builder handle their field values themselves (strawberry/…/maybe_null.py• L17-125).
References: strawberry/…/base.py• L93-174 strawberry/…/maybe.py• L14-67 strawberry/…/schema_converter.py• L1392-1407 strawberry/…/arguments.py• L234-271 strawberry/…/maybe_null.py• L17-125
Generic definitions
Generic definitions describe one Python type shape that can be specialized for different GraphQL uses. Strawberry maps type variables to resolved types, copies the definition with those mappings, and keeps the specialized definition linked to its generic source (strawberry/…/base.py• L371-420).
The specialized definition carries the concrete field types used by conversion and runtime checks. Type comparison follows the source definition and its type-variable map. It can also compare nested generic definitions and classes that were recreated by a module reload. This keeps generic type identity aligned across schema construction and abstract result matching (strawberry/…/base.py• L389-420, strawberry/…/base.py• L486-571, strawberry/…/schema_converter.py• L124-198).
References: strawberry/…/base.py• L371-420 strawberry/…/base.py• L428-485 strawberry/…/base.py• L486-571 strawberry/…/schema_converter.py• L124-198
GraphQL type conversion
The schema converter maps Strawberry definitions to graphql-core type objects. It keeps a type map so shared references reuse one GraphQL type and so conflicting definitions with one GraphQL name are detected (strawberry/…/schema_converter.py• L405-425, strawberry/…/schema_converter.py• L1480-1508). Each GraphQL type also keeps a back-reference to the Strawberry definition that produced it.
Named type mapping
The mapping preserves the main GraphQL relationships:
| Strawberry concept | GraphQL role |
|---|---|
| Object definition | Object type with fields and implemented interfaces |
| Input definition | Input object with input fields |
| Interface definition | Abstract output type with shared fields |
| Enum definition | Named set of enum values |
| Scalar definition | Value type with parse and serialize rules |
| Union definition | Abstract output type with allowed object members |
| List or optional wrapper | List or nullable/non-null form of another type |
The converter chooses the mapping from the resolved Strawberry type. It unwraps lazy references and Annotated forms before mapping the result. It rejects unsupported shapes, unresolved generic types, and input types used where a union needs output objects (strawberry/…/schema_converter.py• L1392-1448, strawberry/…/schema_converter.py• L1450-1467). See Schema conversion and printing for conversion, introspection, and SDL output.
References: strawberry/…/schema_converter.py• L405-425 strawberry/…/schema_converter.py• L838-889 strawberry/…/schema_converter.py• L929-1018 strawberry/…/schema_converter.py• L1020-1091 strawberry/…/schema_converter.py• L1392-1448 strawberry/…/schema_converter.py• L1450-1467
Names and configuration
GraphQL names form a schema-wide contract. StrawberryConfig controls naming and scalar mappings. The schema supplies root types, extra types, directives, and extension settings to the converter (strawberry/…/config.py• L20-49, strawberry/…/schema.py• L306-456). A name collision is a schema conflict even when the Python classes differ (strawberry/…/schema_converter.py• L1480-1508).
Directive fields use the same naming and argument conversion rules as other schema inputs. Their annotations resolve during schema construction, so an unresolved directive argument fails at the schema boundary (strawberry/…/schema_converter.py• L527-571).
References: strawberry/…/config.py• L20-49 strawberry/…/schema.py• L306-456 strawberry/…/schema_converter.py• L527-571 strawberry/…/schema_converter.py• L1480-1508
Resolver exception mapping
The schema can map selected Python exceptions to GraphQL error objects in a field's union result. An exception handler declares the Python exception and GraphQL error types it supports. Schema construction checks the declaration, and conversion selects it only when the field publishes that error type (strawberry/…/exception_handlers.py• L28-61, strawberry/…/exception_handlers.py• L104-166, strawberry/…/schema_converter.py• L1047-1085).
Field extensions run before this match. This means a type-changing extension can add the error type that selects a handler. At execution, a matching exception from a resolver can become a union value. A handler that declines an exception leaves it as a normal GraphQL error. Subscription fields do not use this exception-to-union conversion (strawberry/…/exception_handlers.py• L48-52, strawberry/…/schema_converter.py• L1111-1131, strawberry/…/schema_converter.py• L1160-1225).
References: strawberry/…/exception_handlers.py• L28-61 strawberry/…/exception_handlers.py• L104-166 strawberry/…/schema_converter.py• L1047-1085 strawberry/…/schema_converter.py• L1111-1131 strawberry/…/schema_converter.py• L1160-1225
Abstract type relationships
Interfaces and unions let a field promise an abstract result while a resolver returns a concrete object. The schema stores the allowed relationship, and runtime type resolution selects the GraphQL object that matches the returned Python value.
Interfaces and implementations
An object may implement one or more interfaces. The converter exposes those links on the GraphQL object and interface types. If an object has no custom type check, Strawberry can match the returned object to its definition and use the GraphQL default resolver as a fallback (strawberry/…/schema_converter.py• L929-1018). Generic implementations use their specialized definitions when the returned value must match a concrete GraphQL type.
References: strawberry/…/schema_converter.py• L929-1018 strawberry/…/schema_converter.py• L1025-1091 strawberry/…/base.py• L488-573
Union members
A union contains allowed output object types. Its runtime resolver checks the declared members and then uses type checks when needed. A returned value outside those members violates the GraphQL contract (strawberry/…/union.py• L168-225). The converter flattens nested unions and rejects input objects or other invalid members before publishing the union (strawberry/…/schema_converter.py• L1450-1477).
References: strawberry/…/union.py• L168-225 strawberry/…/schema_converter.py• L1450-1477
Related pages
Read the child pages in this order when you need more detail:
- Defining GraphQL types explains the Python decorators and type primitives.
- Type annotations and resolution explains how Python typing forms become Strawberry types.
- Schema conversion and printing explains the GraphQL schema boundary and SDL output.
Change checklist
When these files or sections are changed, remember to consider these:
- Changes to
StrawberryObjectDefinitionfields must stay aligned with decorator output, generic specialization, input conversion hooks, and object, input, and interface conversion. - Changes to definition lookup or subclass checks must preserve the rule that schema entry points use their own decorated definition.
- Changes to field annotation validation or replacement must preserve re-validation of the current annotation before schema conversion.
- Changes to field or argument metadata in
Annotatedmust preserve one configuration at the outer annotation boundary and keep other metadata available to type resolution. - Changes to argument
graphql_typeoverrides must preserve the GraphQL argument contract and the Python input conversion type. - Changes to argument input conversion must preserve GraphQL names, nested input paths, omitted values, and custom input builders.
- Changes to annotation wrappers must preserve the list and nullability shape expected by GraphQL conversion.
- Changes to
Mayberecognition, wrapper conversion, or null validation must preserve the difference between omission, explicit null, and a supplied value for both arguments and nested input fields. - Changes to input builders must preserve their ownership of
Maybefield conversion and null handling. - Changes to lazy references must preserve module resolution for circular imports and the single-definition identity used by schema conversion.
- Changes to name conversion or scalar registration must preserve unique GraphQL names across the converter type map.
- Changes to generic specialization or type comparison must preserve the link between concrete definitions and their generic source during conversion and runtime type checks.
- Changes to union or interface membership must preserve the runtime type checks used for abstract results.
- Changes to extension ordering must preserve exception-handler matching against the field type after extensions change it.
- Changes to root or extra-type registration must preserve the set of named types passed to
GraphQLSchema.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A field has no usable annotation or resolver return type | Schema construction raises a missing-annotation error | Give every exposed field a resolvable type annotation or typed resolver result (strawberry/…/object_type.py• L218-289) |
| A schema entry point is an undecorated subclass of a Strawberry type | Schema construction rejects the class instead of publishing the parent definition by accident | Decorate each class used as a schema type or use the decorated parent (strawberry/…/base.py• L272-296) |
| A field or argument configuration is nested inside another annotation shape or declared twice | Type processing rejects the configuration | Keep configuration at the outer Annotated boundary and define it once (strawberry/…/field.py• L430-481, strawberry/…/arguments.py• L88-133) |
| A forward or lazy reference remains unresolved | Conversion rejects the field instead of creating an incomplete GraphQL type | Keep the target type available in its namespace before schema construction (strawberry/…/schema_converter.py• L262-295, strawberry/…/lazy_type.py• L37-71) |
| Two definitions use one GraphQL name with different meaning | The converter reports a conflicting type definition | Keep one shared definition for each named GraphQL type (strawberry/…/schema_converter.py• L1480-1508) |
A Maybe[T] input receives explicit null | Validation rejects the value before the resolver uses it | Omit the field for absence, or use `Maybe[T |
A custom input builder receives a Maybe[T] field with null | The builder must report or handle the null because the general document rule does not validate its fields | Keep the builder's input conversion aligned with the Maybe contract (strawberry/…/maybe_null.py• L91-96, strawberry/…/base.py• L324-342) |
| A generic type remains unspecialized where GraphQL needs a concrete type | Schema conversion cannot publish a complete field type | Supply concrete type arguments before schema conversion (strawberry/…/base.py• L371-420, strawberry/…/schema_converter.py• L1413-1419) |
| A union includes an input or unsupported type | Union construction fails because unions are output-only | Declare only valid object output members (strawberry/…/schema_converter.py• L1450-1467) |
| A returned abstract value is not a declared interface or union member | Runtime type resolution fails | Return a value that matches the published abstract relationship (strawberry/…/schema_converter.py• L929-1018, strawberry/…/union.py• L168-225) |
| A field extension changes the return union after exception handlers are matched | The configured handler is skipped and the exception remains a normal GraphQL error | Apply type-changing extensions before handler matching (strawberry/…/schema_converter.py• L1111-1131) |
Key files
| File | Why to read it |
|---|---|
| strawberry/…/object_type.py | Shows how Python classes become object, input, and interface definitions. |
| strawberry/…/base.py | Defines the shared type, wrapper, generic, definition lookup, and object-definition model. |
| strawberry/…/field.py | Shows how fields connect annotations and resolvers to a type definition. |
| strawberry/…/arguments.py | Shows how resolver arguments carry GraphQL metadata and how GraphQL input becomes Python values. |
| strawberry/annotation.py | Shows how Python annotations become Strawberry type objects. |
| strawberry/…/maybe.py | Defines the input marker that distinguishes omitted values from supplied values. |
| strawberry/…/schema.py | Shows the public schema boundary, root assembly, and early validation. |
| strawberry/…/schema_converter.py | Shows the mapping from Strawberry definitions to GraphQL core types. |
| strawberry/…/maybe_null.py | Shows how explicit null is checked for Maybe arguments and input fields. |
| strawberry/…/lazy_type.py | Defines deferred references used across modules and circular imports. |
| strawberry/…/union.py | Shows the contract and runtime selection rules for abstract union results. |
| strawberry/…/config.py | Shows schema-wide naming and scalar configuration inputs. |