GraphQL middleware are Python functions and callable objects that can be used to inject custom logic into query executor.
Middlewares share most of their arguments with
resolvers, but take one extra argument:
resolver callable that is resolver associated with currently resolved field:
def lowercase_middleware(resolver, obj, info, **args)
GraphQL middleware is sometimes confused with the ASGI or WSGI middleware, bit its not the same thing!
Middleware is not supported by subscriptions.
Custom middleware example
Code below implements custom middleware that converts any strings returned by resolvers to lower case:
def lowercase_middleware(resolver, obj, info, **args): value = resolver(obj, info, **args) if isinstance(value, str): return value.lower() return value
To use this middleware in your queries, simply pass it to
from ariadne.asgi import GraphQL app = GrapqhQL(schema, middleware=[lowercase_middleware])
In case when more than one middleware is enabled on the server, the
resolver argument will point to the partial function constructed from the next middleware in the execution chain.
Middleware and extensions
resolve hook is actually a middleware. In case when GraphQL server is configured to use both middleware and extensions, extensions
resolve hook will be executed before the