Introducing a new way to review your PRs.

Read the announcement

strawberry-graphql / strawberry

A GraphQL library for Python that leverages type annotations 🍓

View on GitHub

21 documents · Updated Nov 18, 2026

docs / schema-conversion-and-printing

Schema conversion and printing

Strawberry turns declared Python types into a graphql-core schema that can validate, execute, introspect, and render GraphQL operations. This page explains that boundary, how names and schema settings shape it, and how the same schema becomes introspection data or SDL. For the wider model of Strawberry types, see Schema and type system; for declarations and annotation handling, see Defining GraphQL types and Type annotations and resolution.

Schema boundary

strawberry.Schema is the boundary between Strawberry declarations and the GraphQL runtime. It accepts root types, optional extra types, and schema directives. It builds the GraphQL schema and checks that the result is valid before the schema is used for execution.

The main construction path is:

flowchart LR
    A[Strawberry declarations] --> B[Schema configuration]
    B --> C[GraphQL type conversion]
    C --> D[GraphQL schema]
    D --> E[Execution]
    D --> F[Introspection]
    D --> G[SDL printing]

The schema starts with the query root and may add mutation and subscription roots. Types reached through fields are converted as needed. The types argument adds named types that are not reached from a root field, so they can still take part in the schema. Attached schema directives are collected and converted before the single GraphQL schema is built, so their argument types and uses are part of the same validated type graph.

References: strawberry/…/schema.py• L307-347 strawberry/…/schema.py• L390-499

Type conversion

The converter maps Strawberry’s type definitions to the matching graphql-core type objects. It keeps a shared type map during schema construction so references to the same GraphQL type reuse one object and so type-name conflicts can be detected.

Conversion categories

strawberry/…/schema_converter.py defines the conversion boundary through GraphQLCoreConverter. The converter supports the GraphQL type categories used by Strawberry and prepares the resolver bridge that GraphQL uses at run time. It resolves lazy and annotated references before conversion, validates Pydantic model fields against the schema’s registered types when Pydantic is in use, then applies field extensions before it fixes the field’s GraphQL result type.

Strawberry declarationGraphQL result
Object typeGraphQLObjectType
Input objectGraphQLInputObjectType
InterfaceGraphQLInterfaceType
EnumGraphQLEnumType
UnionGraphQLUnionType
ScalarGraphQLScalarType
List or optional annotationGraphQL list or nullability wrapper

Fields and arguments carry their GraphQL type, name, description, default, and deprecation data into the resulting schema. Resolver behavior remains attached to fields so GraphQL execution can call the Python resolver after schema construction. Field extensions and configured exception handlers are also aligned with the final field type during this step, which lets a handled exception become a member of a declared result union without changing the GraphQL contract later.

References: strawberry/…/schema_converter.py• L385-439 strawberry/…/schema_converter.py• L613-649 strawberry/…/schema_converter.py• L1292-1402 strawberry/…/fields.py• L398-420

Input value conversion

Input defaults and other declared input values are normalized to the GraphQL shape during schema conversion. The conversion preserves the difference between omitted values and explicit values while it applies GraphQL field names to nested input objects. Values recognized by the scalar registry pass through as scalar values instead of being treated as nested input objects.

An input object instance can provide a to_input hook that supplies the values to expose to GraphQL. Lists, optional values, global IDs, mappings, and nested input objects are then converted through the same path. One-of inputs keep one selected non-null field. This keeps schema defaults, introspection, and resolver inputs aligned with values sent by clients.

References: strawberry/…/schema_converter.py• L651-743 strawberry/…/schema_converter.py• L745-770

Maybe input semantics

Maybe[T] represents an input value that can be omitted or supplied with a value. Schema conversion exposes it as a nullable GraphQL input type, while validation rejects an explicit literal null; Maybe[T | None] also permits an explicit null. This keeps absence distinct from null when Strawberry builds resolver inputs.

The same rule checks fields inside input object literals by using their final GraphQL names. Input types that build their own values can handle these checks with their broader input conversion path instead. This validation runs with the normal document validation rules, so invalid literal nulls fail before resolver execution.

References: strawberry/…/schema_converter.py• L791-816 strawberry/…/schema_converter.py• L1392-1407 strawberry/…/maybe_null.py• L17-79 strawberry/…/maybe_null.py• L81-125 strawberry/…/schema.py• L187-201

Type identity and reuse

The type map is keyed by GraphQL name. A second use of the same definition reuses the existing GraphQL object. A different definition with that name is rejected instead of silently changing the schema. This rule also protects specialized generic types whose names are derived from their type arguments.

Lazy references and generic declarations are resolved before the converter commits their GraphQL form. A bare GraphQL generic cannot be converted because it has no concrete type arguments. Unions are checked for valid output members, and nested unions are flattened into their object members. An undecorated subclass of a Strawberry type is rejected rather than being treated as a new schema type.

References: strawberry/…/schema_converter.py• L114-198 strawberry/…/schema_converter.py• L1404-1545

Names and configuration

GraphQL names form the public contract of a schema. Strawberry keeps Python names for resolver lookup but uses configured GraphQL names for fields, arguments, types, directives, and scalar references.

GraphQL names

An explicit GraphQL name takes priority over all naming rules. Without one, field and argument names use camel case by default. For example, first_name is exposed as firstName. Type and enum names use their declared names, while generic and union names can be formed from their concrete members.

Directive names follow the same configuration but keep a lower-case first letter when automatic camel casing is active. This keeps directive names such as sensitiveData consistent with GraphQL conventions.

References: strawberry/…/name_converter.py• L32-60 strawberry/…/name_converter.py• L65-107 strawberry/…/name_converter.py• L136-197

Schema configuration

StrawberryConfig controls naming and selected schema-wide behavior. auto_camel_case=False exposes Python field and argument names as written. A custom NameConverter can provide a different naming policy.

Scalar configuration extends the built-in scalar registry. scalar_map provides schema configuration for Python types such as NewType, while scalar_overrides supplies per-schema replacements. The configuration also controls the default resolver, introspection error suggestions, Relay limits, custom Info objects, operation batching, optional incremental execution, and whether the schema is sorted lexicographically. Sorting changes the order seen in introspection and exported schema text; it does not change the schema’s type meaning.

References: strawberry/…/config.py• L20-65 strawberry/…/schema.py• L389-399 strawberry/…/schema.py• L488-499 strawberry/…/schema_converter.py• L391-439 strawberry/…/schema_converter.py• L1247-1299

Introspection behavior

The schema exposes the graphql-core introspection model after construction. Introspection therefore reports the final GraphQL names, nullability, fields, arguments, directives, and reachable types rather than the original Python spelling.

Introspection query

Schema.introspect() runs GraphQL’s standard introspection query synchronously against the current schema and returns its data as a dictionary. It raises ValueError when the query produces errors or no data, so callers do not receive a partial description of an invalid schema.

Schema construction validates the GraphQL schema early. An invalid root or type relationship fails during Schema creation instead of waiting for the first client request.

References: strawberry/…/schema.py• L481-499 strawberry/…/schema.py• L1372-1387 tests/…/test_schema_generation.py• L35-49

One-of input metadata

Strawberry extends the introspection __Type object with isOneOf. For an input object declared as a one-of input, the field reports true; for other types it reports false. The value comes from the Strawberry definition attached to the GraphQL type, so the introspection result stays aligned with Strawberry’s input rules.

References: strawberry/…/schema.py• L1361-1370 tests/…/test_one_of.py• L249-280

SDL rendering

Schema.as_str() and strawberry.printer.print_schema render the built schema as GraphQL SDL. The printer uses the GraphQL type map as its source but adds Strawberry metadata that the standard GraphQL printer does not know about. Registered schema directives reuse the converted directive objects from schema construction, so their printed argument types and defaults match introspection.

Schema text

The printer emits defined types in a stable name order. When sort_schema is enabled, the underlying GraphQL schema is first sorted lexicographically, so introspection and exported schema order use the same setting. The output includes descriptions, interfaces, deprecations, default values, nullability, custom scalars, unions, and input objects. It omits the explicit schema block when the roots use the common Query, Mutation, and Subscription names and no schema directive requires that block.

The output is an artifact for inspection, export, and schema comparison. It does not replace the runtime schema object used for execution or introspection.

References: strawberry/…/printer.py• L506-527 strawberry/…/printer.py• L545-579 strawberry/…/printer.py• L704-773 strawberry/…/schema.py• L488-499 strawberry/…/schema.py• L1372-1375

Directive rendering

Schema directives are printed both as declarations and as uses when their location permits. Directive arguments are rendered as GraphQL values, including nested input objects, lists, enums, and omitted UNSET values. A directive with print_definition=False keeps its use in SDL but suppresses its declaration. The printer reuses directives that were converted during schema construction and converts hidden definitions only when it needs them for SDL.

Types used only by directive arguments are added to the output when needed. The printer checks their GraphQL names against the schema type map to avoid printing a type twice. It first discovers directive-linked extra types and then filters directive definitions, so a shared type or directive is printed once. Support types hidden from SDL remain omitted unless a visible field or directive makes them reachable.

References: strawberry/…/printer.py• L130-207 strawberry/…/printer.py• L210-240 strawberry/…/printer.py• L529-601 strawberry/…/printer.py• L616-701 strawberry/…/printer.py• L704-773 strawberry/…/schema_converter.py• L508-571 strawberry/schema_directive.py• L28-73

Change checklist

When these files or sections are changed, remember to consider these:

  • A naming rule or explicit name change affects field lookup, type lookup, introspection, SDL, and client operation names together.
  • A new converter category must have a matching SDL printer path and must preserve the Strawberry definition link needed by directives and introspection.
  • Changes to input value conversion or to_input hooks must keep Python names, GraphQL names, omitted values, explicit nulls, defaults, and nested input values aligned.
  • Changes to Maybe conversion or null validation must keep omission, explicit literal nulls, nested input fields, custom input builders, and resolver values consistent.
  • Changes to scalar_map, scalar overrides, or built-in scalar names must preserve registry precedence and reject conflicting GraphQL scalar definitions.
  • Changes to Pydantic field validation must keep scalar registration, nested model discovery, forward references, and input-versus-output error reporting aligned.
  • Changes to schema directive fields must keep argument names, GraphQL input types, value serialization, extra-type discovery, and the introspection directive registry aligned.
  • Changes to one-of metadata must keep schema validation, runtime input checks, SDL output, and __Type.isOneOf consistent.
  • Changes to exception-to-union handling must keep extension-applied field types, handler matching, resolver behavior, and the resulting GraphQL union contract aligned.
  • Changes to lazy or annotated type resolution must keep forward references, union members, generic specializations, and the converter's type identity checks aligned.
  • Changes to sort_schema must keep the ordering seen by introspection and SDL consistent, while preserving the Strawberry back-reference used during execution.

Important failure modes

TriggerConsequenceGuard
A field type is still unresolved when conversion startsSchema construction raises an unresolved-field errorResolve forward and lazy annotations before building the schema
An undecorated subclass is used as a schema typeConversion rejects the type instead of creating an incomplete GraphQL typeDecorate the subclass with the required Strawberry type declaration
Two different definitions use one GraphQL type nameSchema construction raises DuplicatedTypeNameGive each public GraphQL type a unique name
A bare generic reaches conversionSchema construction raises MissingTypesForGenericErrorUse a concrete generic specialization
A Pydantic model field is not registered as a GraphQL typeSchema construction raises an unregistered-model error when Pydantic validation is activeRegister the model with the Strawberry Pydantic integration or use a supported scalar
Two scalar definitions resolve to the same GraphQL nameSchema construction raises ScalarAlreadyRegisteredErrorKeep scalar names unique across defaults, maps, and overrides
A schema directive field has an unresolved typeSchema construction raises UnresolvedFieldTypeError before GraphQL conversionResolve directive field annotations before schema creation
An input object default cannot be represented by its declared input typeSchema defaults and introspection do not match the declared input contractKeep to_input output and nested values valid for the GraphQL input type
A literal null is sent to a Maybe[T] argument or input fieldValidation raises StrawberryInputCoercionError before resolver executionOmit the value or use `Maybe[T
A one-of input has a required field or a defaultSchema construction raises InvalidOneOfInputFieldErrorMake every one-of member nullable and omit member defaults
A union contains an input object or invalid memberSchema construction raises a union type errorUse output object types or unions as union members
An exception handler cannot resolve its exception or error typeSchema construction fails while validating the handler configurationKeep handler exception types and result types valid
GraphQL schema validation finds an invalid root or type relationSchema creation raises ValueError with the validation errorsValidate type links and root definitions before exposing the schema
The standard introspection query returns errorsSchema.introspect() raises ValueError instead of returning partial dataTreat introspection failure as a schema health error

Key files

FileWhy to read it
strawberry/…/schema.pyShows schema assembly, early validation, introspection extension, sorting, as_str(), and introspect().
strawberry/…/schema_converter.pyDefines the boundary from Strawberry definitions to graphql-core types, input value conversion, resolver adaptation, scalar registry, directive conversion, Pydantic field checks, and the type-name cache.
strawberry/…/maybe_null.pyShows how literal null values are checked against Strawberry’s distinction between omitted and nullable Maybe inputs.
strawberry/…/name_converter.pyDefines explicit-name precedence, automatic camel casing, and names for generic and union types.
strawberry/…/config.pyLists schema-wide naming, scalar, Relay, introspection, execution, batching, and schema-order settings.
strawberry/…/printer.pyShows SDL ordering, schema-block rules, directive output, reachability filtering, and extra-type handling.
strawberry/…/fields.pyShows how Pydantic model fields are checked against the schema’s registered GraphQL types.
strawberry/…/scalar.pyShows the built-in scalar registry and the shape of GraphQL scalar objects.
strawberry/schema_directive.pyDefines schema directive locations, names, fields, repeatability, and definition-printing policy.