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 / developer-tooling

Developer tooling

Strawberry's developer tooling turns a Python schema or a GraphQL document into useful artifacts for local work and client development. The command-line interface loads a schema from a Python module and can export SDL, generate client-facing types from operations, rebuild Python schema code from SDL, locate schema definitions, run a reloadable local server, and apply upgrade codemods. See CLI and code generation for the detailed command and generator guide, and schema and type system for the schema model that these tools consume.

Tooling model

The tooling has two main directions. Schema-first input can become Strawberry Python code, while a loaded Strawberry schema plus a GraphQL operation can become client types or a printed operation. The same schema-loading path also supports inspection and local serving, so these tools work with the schema that an application actually defines.

The main tool groups are:

Tool groupStarting materialMain result
Schema exportPython schemaGraphQL SDL
Schema code generationGraphQL SDLStrawberry Python schema
Operation code generationPython schema and named GraphQL operationsPython, TypeScript, or GraphQL files
Development supportPython schema or source pathsLocal server, source locations, or upgraded source

Command-line entry point

The strawberry command is the public entry point for the tooling. It exposes commands for artifact generation, local development, source lookup, and source upgrades; the CLI extra supplies the packages needed by these commands.

References: pyproject.toml• L36-37 pyproject.toml• L53-62 strawberry/…/__init__.py• L1-15

Schema loading

Commands that work from an application schema accept a Python import selector such as package.module:schema. The selector may name a schema object or a callable that creates one, and the default symbol is schema when the selector names only a module. --app-dir makes an application directory available for that import.

References: strawberry/…/export_schema.py• L11-35 strawberry/…/codegen.py• L92-129 strawberry/…/dev.py• L23-59 strawberry/…/importer.py• L4-20

Artifact generation

Artifact generation keeps schema definitions and client code aligned. It either publishes the current Python schema as standard SDL or uses SDL to create a new Strawberry schema module.

Schema export

The export-schema command turns a loaded Strawberry schema into GraphQL schema definition language. It prints the SDL by default or writes it to a named file, which lets IDEs and other GraphQL tools consume the application's public schema.

References: strawberry/…/export_schema.py• L9-38 docs/…/schema-export.md• L7-38

Schema code generation

The schema-codegen command turns an SDL file into Python definitions that describe the schema. It preserves the schema's main type shapes and relationships, then emits a module that can construct a Strawberry schema; this supports a workflow in which SDL is the source used to start or refresh a Python schema. The generator also handles empty field, enum, and union lists without requiring those optional GraphQL collections to be present. Generated descriptions and other string values retain their content, including multi-line text, in valid Python literals.

References: strawberry/…/schema_codegen.py• L9-31 strawberry/…/__init__.py• L243-284 strawberry/…/__init__.py• L553-591 strawberry/…/__init__.py• L607-733 strawberry/…/__init__.py• L828-944

Operation code generation

The codegen command turns a named GraphQL operation into files shaped by the fields selected in that operation. It loads the Strawberry schema to resolve the selected fields and types, then sends one shared operation model to one or more plugins. Built-in plugins produce Python types, TypeScript types, or a normalized GraphQL operation. Custom plugins can produce other artifacts. See CLI and code generation for the detailed command and generator guide.

The operation model keeps the query's result shape aligned with the schema. It includes selected fields, aliases, variables, directives, fragments, enums, scalars, objects, unions, and list or nullable wrappers. Plugin output therefore changes with the operation's selection set rather than with the full schema. The generator orders dependent types before the artifacts that use them and writes all plugin results as named output files.

References: strawberry/…/codegen.py• L92-143 strawberry/…/query_codegen.py• L102-152 strawberry/…/query_codegen.py• L281-342 strawberry/…/query_codegen.py• L344-382 strawberry/…/query_codegen.py• L789-865 strawberry/…/python.py• L37-64 strawberry/…/typescript.py• L22-47 strawberry/…/print_operation.py• L36-58

The following flow shows how the two schema directions and the operation path relate.

flowchart LR
    PythonSchema[Python schema] --> Export[export-schema]
    Export --> SDL[GraphQL SDL]
    SDL --> SchemaCodegen[schema-codegen]
    SchemaCodegen --> PythonGenerated[Generated Strawberry Python]
    PythonSchema --> OperationCodegen[codegen]
    Query[Named GraphQL operation] --> OperationCodegen
    OperationCodegen --> Plugins[Generation plugins]
    Plugins --> ClientTypes[Python or TypeScript types]
    Plugins --> PrintedQuery[GraphQL operation]

Development workflows

The CLI also shortens the feedback loop around a schema. It can serve the loaded schema with reload support, map schema names back to source, and apply focused source changes during Strawberry upgrades.

Local development server

The dev command starts a local GraphQL server for a loaded schema. It exposes HTTP and WebSocket access at the GraphQL routes and reloads when files in the application directory change, so an engineer can inspect schema behavior without wiring a separate server entry point.

References: strawberry/…/dev.py• L23-72 strawberry/…/dev_server.py• L13-28

Definition lookup

The locate-definition command maps a schema type or type field to a source location. This connects the public GraphQL view back to the Python code that defines it and gives development tools a path, line, and column for navigation.

References: strawberry/…/locate_definition.py• L15-36 strawberry/…/locate_definition.py• L14-46

Upgrade codemods

The upgrade command applies a named codemod to Python files or directories. A codemod is a source transformation that updates code to a newer Strawberry form; the command reports changed and skipped files and returns a non-success result when it made changes so a workflow can review them.

References: strawberry/…/__init__.py• L19-24 strawberry/…/__init__.py• L28-93 strawberry/…/_run_codemod.py• L53-80

Change checklist

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

  • When a CLI command changes its schema selector or output contract, review the shared schema-loading behavior and the related command guide.
  • When a built-in codegen plugin changes its file name or type mapping, review output collisions and downstream client import paths.
  • When the schema model changes, review both SDL-to-Python generation and operation-to-client generation because they consume different views of schema information.
  • When a codemod is added or renamed, update the accepted command name and its source transformation together.
  • When local server startup changes, keep the schema import selector and the HTTP and WebSocket entry points aligned.
  • When GraphQL AST handling changes, review named-operation rules, fragment ordering, field-name mapping, and type conditions because code generation depends on them to build valid result types.

Important failure modes

TriggerConsequenceGuard
The schema selector names a missing module, symbol, or non-schema valueExport, code generation, definition lookup, or local serving stops before it can use the schemaUse a valid module selector and confirm the selected value is a Strawberry schema
A query has no operation, has more than one operation, or has no operation nameOperation code generation cannot choose the result type and failsGive each processed document one named operation
An inline fragment has no type conditionOperation code generation cannot resolve the branch to a schema object typeGive each inline fragment a schema type condition
A query uses a fragment together with separate fields in the same selection setThe operation model cannot combine that selection shape and generation failsKeep fragment-only selection sets within the supported operation shape
A requested codegen plugin cannot be imported or does not expose a supported plugin classNo artifact is generated for that queryUse a built-in plugin name or a valid import path for a plugin
CLI server dependencies are not installedThe dev command cannot start the local serverInstall the cli extra before using the development server
SDL contains a definition that schema code generation does not supportPython schema output is not producedKeep the input within the supported GraphQL definition set or extend the schema generator

Key files

FileWhy to read it
pyproject.tomlDefines the strawberry command and the optional packages that enable the CLI.
strawberry/…/__init__.pyShows which user-facing commands are registered.
strawberry/…/export_schema.pyShows the Python-schema-to-SDL flow.
strawberry/…/schema_codegen.pyShows the SDL-to-Python command boundary.
strawberry/…/codegen.pyShows how schemas, query files, and plugins are combined.
strawberry/…/query_codegen.pyDefines the shared operation model, schema-aware type collection, fragment handling, and plugin lifecycle.
strawberry/…/python.pyIllustrates Python client type output.
strawberry/…/typescript.pyIllustrates TypeScript client type output.
strawberry/…/__init__.pyDefines the SDL-to-Python generation boundary and supported schema shapes.
strawberry/…/dev.pyShows the local development server workflow and reload behavior.