From adfbffb267b8e4c7bf6067c743a22a1e3c990d90 Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Mon, 31 Oct 2016 05:32:00 +0100 Subject: [PATCH] Added middleware docs --- docs/execution/index.rst | 8 +++++++ docs/execution/middleware.rst | 44 +++++++++++++++++++++++++++++++++++ docs/index.rst | 1 + 3 files changed, 53 insertions(+) create mode 100644 docs/execution/index.rst create mode 100644 docs/execution/middleware.rst diff --git a/docs/execution/index.rst b/docs/execution/index.rst new file mode 100644 index 00000000..beb4e3b1 --- /dev/null +++ b/docs/execution/index.rst @@ -0,0 +1,8 @@ +========= +Execution +========= + +.. toctree:: + :maxdepth: 1 + + middleware diff --git a/docs/execution/middleware.rst b/docs/execution/middleware.rst new file mode 100644 index 00000000..54eb55db --- /dev/null +++ b/docs/execution/middleware.rst @@ -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()]) diff --git a/docs/index.rst b/docs/index.rst index d7aa9dca..675051b3 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -8,6 +8,7 @@ Contents: quickstart types/index + execution/index relay/index Integrations