»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:

import strawberry

@strawberry.type
class Query:
    @strawberry.field
    def hello(self) -> str:
        return "Hello World"


schema = strawberry.Schema(Query)

»API

strawberry.Schema(
    query: Type,
    mutation: Type = None,
    subscription: Type = None
    validate_queries: bool = True
)

Creates a GraphQL schema


🍓

».execute() (async)

Executes a GraphQL operation against a schema (async)

execute(query, variable_values, context_value, root_value, operation_name, validate_queries)

Parameter nameTypeDefaultDescription
querystrN/AThe document to be executed
variable_valuesOptional[Dict[str, Any]]NoneThe variables for this operation
context_valueOptional[Any]NoneThe value of the context that will be passed down to resolvers
root_valueOptional[Any]NoneThe value for the root type that will passed down to root resolvers
operation_nameOptional[str]NoneThe name of the operation you want to execute, useful when sending a document with multiple operations
validate_queriesboolTrueThis flag allows to disable query validation

🍓

».execute_sync()

Executes a GraphQL operation against a schema

execute_sync(query, variable_values, context_value, root_value, operation_name, validate_queries)

Parameter nameTypeDefaultDescription
querystrN/AThe document to be executed
variable_valuesOptional[Dict[str, Any]]NoneThe variables for this operation
context_valueOptional[Any]NoneThe value of the context that will be passed down to resolvers
root_valueOptional[Any]NoneThe value for the root type that will passed down to root resolvers
operation_nameOptional[str]NoneThe name of the operation you want to execute, useful when sending a document with multiple operations
validate_queriesboolTrueThis flag allows to disable query validation

»Query validation

When creating a schema you can decide to disable the validation of the queries sent from clients. This can be useful to improve performances in some specific cases, for example when dealing with internal APIs where queries can be trusted.

⚠️ NOTE: make sure you understand the trade-offs of disabling validation, for example when asking for field that don't exist the GraphQL schema won't return any error, which is something that breaks the safety of having a typed schema.

»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:

# strawberry/schema/schema.py
from strawberry.types import ExecutionContext

logger = logging.getLogger("strawberry.execution")

class Schema:
    ...

    def process_errors(self, errors: List[GraphQLError], execution_context: ExecutionContext) -> None:
        for error in errors:
            # A GraphQLError wraps the underlying error so we have to access it
            # through the `original_error` property
            # https://graphql-core-3.readthedocs.io/en/latest/modules/error.html#graphql.error.GraphQLError
            actual_error = error.original_error or error
            logger.error(actual_error, exc_info=actual_error)
Edit on Github

Newsletter 💌

Do you want to receive the latest updates on Strawberry? Subscribe to our newsletter!