Skip to content

Commit 10e5424

Browse files
committed
Updated documentation referencing middleware
1 parent b972335 commit 10e5424

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

docs/config.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ ga = "UA-12613282-7"
1717
"/docs/basic-types/",
1818
"/docs/enums/",
1919
"/docs/relay/",
20+
"/docs/middleware/",
2021
]
2122

2223
[docs.django]

docs/pages/docs/django/debug.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,19 @@ For that, you will need to add the plugin in your graphene schema.
1313

1414
## Installation
1515

16-
For use the Django Debug plugin in Graphene, just import `DjangoDebugPlugin` and add it to the `plugins` argument when you initiate the `Schema`.
16+
For use the Django Debug plugin in Graphene:
17+
* Import `DjangoDebugMiddleware` and add it to the `middleware` argument when you initiate the `Schema`.
18+
* Add the `debug` field into the schema root `Query` with the value `graphene.Field(DjangoDebug, name='__debug')`.
1719

1820

1921
```python
20-
from graphene.contrib.django.debug import DjangoDebugPlugin
22+
from graphene.contrib.django.debug import DjangoDebugMiddleware, DjangoDebug
2123

22-
# ...
23-
schema = graphene.Schema(query=Query, plugins=[DjangoDebugPlugin()])
24+
class Query(graphene.ObjectType):
25+
# ...
26+
debug = graphene.Field(DjangoDebug, name='__debug')
27+
28+
schema = graphene.Schema(query=Query, middlewares=[DjangoDebugMiddleware()])
2429
```
2530

2631
This plugin, will add another field in the `Query` named `__debug`.

docs/pages/docs/middleware.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
title: Middleware
3+
description: Walkthrough Middleware
4+
---
5+
6+
# Middleware
7+
8+
You can use _middleware_ to affect the evaluation of fields in your schema.
9+
10+
A middleware is any object that responds to `resolve(*args, next_middleware)`. Inside that method, it should either:
11+
12+
* Send `resolve` to the next middleware to continue the evaluation; or
13+
* Return a value to end the evaluation early.
14+
15+
Middlewares' `resolve` is invoked with several arguments:
16+
17+
* `next` represents the execution chain. Call `next` to continue evalution.
18+
* `root` is the root value object passed throughout the query
19+
* `args` is the hash of arguments passed to the field
20+
* `context` is the context object passed throughout the query
21+
* `info` is the resolver info
22+
23+
Add a middleware to a schema by adding to the `middlewares` list.
24+
25+
26+
### Example: Authorization
27+
28+
This middleware only continues evaluation if the `field_name` is not `'user'`:
29+
30+
```python
31+
class AuthorizationMiddleware(object):
32+
33+
def resolve(self, next, root, args, context, info):
34+
if info.field_name == 'user':
35+
return None
36+
return next(root, args, context, info)
37+
```
38+
39+
Then, add the middleware to your schema:
40+
41+
```python
42+
schema = Schema(middlewares=[AuthorizationMiddleware])
43+
```

0 commit comments

Comments
 (0)