diff --git a/docs/config.toml b/docs/config.toml index 74b843ea..59c4ea5f 100644 --- a/docs/config.toml +++ b/docs/config.toml @@ -17,6 +17,7 @@ ga = "UA-12613282-7" "/docs/basic-types/", "/docs/enums/", "/docs/relay/", + "/docs/middleware/", ] [docs.django] diff --git a/docs/pages/docs/django/debug.md b/docs/pages/docs/django/debug.md index c4b124e8..f5ac2adc 100644 --- a/docs/pages/docs/django/debug.md +++ b/docs/pages/docs/django/debug.md @@ -13,14 +13,19 @@ For that, you will need to add the plugin in your graphene schema. ## Installation -For use the Django Debug plugin in Graphene, just import `DjangoDebugPlugin` and add it to the `plugins` argument when you initiate the `Schema`. +For use the Django Debug plugin in Graphene: +* Import `DjangoDebugMiddleware` and add it to the `middleware` argument when you initiate the `Schema`. +* Add the `debug` field into the schema root `Query` with the value `graphene.Field(DjangoDebug, name='__debug')`. ```python -from graphene.contrib.django.debug import DjangoDebugPlugin +from graphene.contrib.django.debug import DjangoDebugMiddleware, DjangoDebug -# ... -schema = graphene.Schema(query=Query, plugins=[DjangoDebugPlugin()]) +class Query(graphene.ObjectType): + # ... + debug = graphene.Field(DjangoDebug, name='__debug') + +schema = graphene.Schema(query=Query, middlewares=[DjangoDebugMiddleware()]) ``` This plugin, will add another field in the `Query` named `__debug`. diff --git a/docs/pages/docs/middleware.md b/docs/pages/docs/middleware.md new file mode 100644 index 00000000..252fe384 --- /dev/null +++ b/docs/pages/docs/middleware.md @@ -0,0 +1,43 @@ +--- +title: Middleware +description: 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. 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 + +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'`: + +```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) +``` + +Then, add the middleware to your schema: + +```python +schema = Schema(middlewares=[AuthorizationMiddleware]) +```