Schema
Every GraphQL API has a schema and that is used to define all the
functionalities for an API. A schema is defined by passing 3
object types : Query
, Mutation
and Subscription
.
Mutation
and Subscription
are optional, meanwhile Query
has to always be
there.
This is an example of a schema defined using Strawberry:
API reference
query: Type
The root query Strawberry type. Usually called Query
.
A query type is always required when creating a Schema.
mutation: Optional[Type] = None
The root mutation type. Usually called Mutation
.
subscription: Optional[Type] = None
The root subscription type. Usually called Subscription
.
config: Optional[StrawberryConfig] = None
Pass a StrawberryConfig
object to configure how the schema is generated.
Read more .
types: List[Type] = []
List of extra types to register with the Schema that are not directly linked to from the root Query.
Defining extra `types` when using Interfaces
extensions: List[Type[SchemaExtension]] = []
List of extensions to add to your Schema.
scalar_overrides: Optional[Dict[object, ScalarWrapper]] = None
Override the implementation of the built in scalars. More information .
Methods
.execute()
(async)
Executes a GraphQL operation against a schema (async)
query: str
The GraphQL document to be executed.
variable_values: Optional[Dict[str, Any]] = None
The variables for this operation.
context_value: Optional[Any] = None
The value of the context that will be passed down to resolvers.
root_value: Optional[Any] = None
The value for the root value that will passed to root resolvers.
operation_name: Optional[str] = None
The name of the operation you want to execute, useful when sending a document
with multiple operations. If no operation_name
is specified the first
operation in the document will be executed.
.execute_sync()
Executes a GraphQL operation against a schema
query: str
The GraphQL document to be executed.
variable_values: Optional[Dict[str, Any]] = None
The variables for this operation.
context_value: Optional[Any] = None
The value of the context that will be passed down to resolvers.
root_value: Optional[Any] = None
The value for the root value that will passed to root resolvers.
operation_name: Optional[str] = None
The name of the operation you want to execute, useful when sending a document
with multiple operations. If no operation_name
is specified the first
operation in the document will be executed.
Handling execution errors
By default Strawberry will log any errors encountered during a query execution
to a strawberry.execution
logger. This behaviour can be changed by overriding
the process_errors
function on the strawberry.Schema
class.
The default functionality looks like this:
Filtering/customising fields
You can customise the fields that are exposed on a schema by subclassing the
Schema
class and overriding the get_fields
method, for example you can use
this to create different GraphQL APIs, such as a public and an internal API.
Here’s an example of this:
The get_fields
method is only called once when creating the schema, this is
not intended to be used to dynamically customise the schema.
Deprecating fields
Fields can be deprecated using the argument deprecation_reason
.
This does not prevent the field from being used, it’s only for documentation. See: GraphQL field deprecation .