mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-29 13:03:56 +03:00
Added middleware docs
This commit is contained in:
parent
9a3d84319d
commit
adfbffb267
8
docs/execution/index.rst
Normal file
8
docs/execution/index.rst
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
=========
|
||||||
|
Execution
|
||||||
|
=========
|
||||||
|
|
||||||
|
.. toctree::
|
||||||
|
:maxdepth: 1
|
||||||
|
|
||||||
|
middleware
|
44
docs/execution/middleware.rst
Normal file
44
docs/execution/middleware.rst
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
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.
|
||||||
|
|
||||||
|
|
||||||
|
Resolve arguments
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
Middlewares ``resolve`` is invoked with several arguments:
|
||||||
|
|
||||||
|
- ``next`` represents the execution chain. Call ``next`` to continue evalution.
|
||||||
|
- ``root`` is the root value object passed throughout the query.
|
||||||
|
- ``args`` is the hash of arguments passed to the field.
|
||||||
|
- ``context`` is the context object passed throughout the query.
|
||||||
|
- ``info`` is the resolver info.
|
||||||
|
|
||||||
|
|
||||||
|
Example
|
||||||
|
-------
|
||||||
|
|
||||||
|
This middleware only continues evaluation if the ``field_name`` is not ``'user'``
|
||||||
|
|
||||||
|
.. code:: python
|
||||||
|
|
||||||
|
class AuthorizationMiddleware(object):
|
||||||
|
def resolve(self, next, root, args, context, info):
|
||||||
|
if info.field_name == 'user':
|
||||||
|
return None
|
||||||
|
return next(root, args, context, info)
|
||||||
|
|
||||||
|
|
||||||
|
And then execute it with:
|
||||||
|
|
||||||
|
.. code:: python
|
||||||
|
|
||||||
|
result = schema.execute('THE QUERY', middleware=[AuthorizationMiddleware()])
|
|
@ -8,6 +8,7 @@ Contents:
|
||||||
|
|
||||||
quickstart
|
quickstart
|
||||||
types/index
|
types/index
|
||||||
|
execution/index
|
||||||
relay/index
|
relay/index
|
||||||
|
|
||||||
Integrations
|
Integrations
|
||||||
|
|
Loading…
Reference in New Issue
Block a user