mirror of
https://github.com/graphql-python/graphene.git
synced 2025-03-12 07:55:46 +03:00
1.2 KiB
1.2 KiB
title | description |
---|---|
Middleware | Walkthrough Middleware |
Middleware
You can use middleware to affect the evaluation of fields in your schema.
A middleware is any object that responds to resolve(*args, next_middleware)
. Inside that method, it should either:
- Send
resolve
to the next middleware to continue the evaluation; or - Return a value to end the evaluation early.
Middlewares' resolve
is invoked with several arguments:
next
represents the execution chain. Callnext
to continue evalution.root
is the root value object passed throughout the queryargs
is the hash of arguments passed to the fieldcontext
is the context object passed throughout the queryinfo
is the resolver info
Add a middleware to a schema by adding to the middlewares
list.
Example: Authorization
This middleware only continues evaluation if the field_name
is not 'user'
:
class AuthorizationMiddleware(object):
def resolve(self, next, root, args, context, info):
if info.field_name == 'user':
return None
return next(root, args, context, info)
Then, add the middleware to your schema:
schema = Schema(middlewares=[AuthorizationMiddleware])