How to modify the content of the context
before it is passed to the resolver functions?
Checkout the documentation for the special Ariadne type ContextValue.
The GraphQL class accepts a keyword argument context_value
. It can be of any type and will be set as context.
If a callable is passed, then it will be called with the request
as an argument.
So:
Create a function to build the desired context
def get_context_value(request):
return {'request': request, 'test': "TEST"}
Pass the function at GraphQL initialization:
app = GraphQL(
schema,
context_value=get_context_value,
debug=True,
)
Context value inside resolvers:
{'request': <starlette.requests.Request object at 0x7fc363dbf370>, 'test': 'TEST'}