Strawberry provides support for adding extensions. Extensions can be used to
hook into different parts of the GraphQL execution and to provide additional
results to the GraphQL response.
To create a custom extensions you can use extend from our SchemaExtension base
class:
Hooks
Resolve
resolve can be used to run code before and after the execution of all
resolvers. When calling the underlying resolver using _next , all of the
arguments to resolve need to be passed to _next , as they will be needed by the
resolvers.
If you need to wrap only certain field resolvers with additional logic, please
check out field extensions .
Note that resolve can also be implemented asynchronously.
Get results
get_results allows to return a dictionary of data or alternatively an
awaitable resolving to a dictionary of data that will be included in the GraphQL
response.
Lifecycle hooks
Lifecycle hooks runs before graphql operation occur and after it is done.
Lifecycle hooks uses generator syntax. In example: on_operation hook can be
used to run code when a GraphQL operation starts and ends.
Extend error response format
Supported lifecycle hooks:
Validation
on_validate can be used to run code on the validation step of the GraphQL
execution.
Parse
on_parse can be used to run code on the parsing step of the GraphQL execution.
Execution
on_execute can be used to run code on the execution step of the GraphQL
execution.
Examples:
In memory cached executionRejecting an operation before executing it
Execution Context
The SchemaExtension object has an execution_context property on self of
type ExecutionContext .
This object can be used to gain access to additional GraphQL context, or the
request context. Take a look at the
ExecutionContext type
for available data.