Skip to content

Commit 981a7f6

Browse files
committed
Updated docs adding resolver page with @with_context instructions. Fixed #164
1 parent c1c4af8 commit 981a7f6

File tree

3 files changed

+99
-33
lines changed

3 files changed

+99
-33
lines changed

docs/config.toml

+27-26
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,36 @@ siteTitle = "Graphene"
22
ga = "UA-12613282-7"
33

44
[docs.quickstart]
5-
name = "Quickstart"
6-
pages = [
7-
"/docs/quickstart/",
8-
]
5+
name = "Quickstart"
6+
pages = [
7+
"/docs/quickstart/",
8+
]
99

1010
[docs.walkthrough]
11-
name = "Walkthrough"
12-
pages = [
13-
"/docs/interfaces/",
14-
"/docs/objecttypes/",
15-
"/docs/mutations/",
16-
"/docs/basic-types/",
17-
"/docs/enums/",
18-
"/docs/relay/",
19-
]
11+
name = "Walkthrough"
12+
pages = [
13+
"/docs/interfaces/",
14+
"/docs/objecttypes/",
15+
"/docs/resolvers/",
16+
"/docs/mutations/",
17+
"/docs/basic-types/",
18+
"/docs/enums/",
19+
"/docs/relay/",
20+
]
2021

2122
[docs.django]
22-
name = "Django"
23-
pages = [
24-
"/docs/django/tutorial/",
25-
"/docs/django/filtering/",
26-
"/docs/django/authorization/",
27-
"/docs/django/introspection-schema/",
28-
"/docs/django/debug/",
29-
]
23+
name = "Django"
24+
pages = [
25+
"/docs/django/tutorial/",
26+
"/docs/django/filtering/",
27+
"/docs/django/authorization/",
28+
"/docs/django/introspection-schema/",
29+
"/docs/django/debug/",
30+
]
3031

3132
[docs.sqlalchemy]
32-
name = "SQLAlchemy"
33-
pages = [
34-
"/docs/sqlalchemy/tutorial/",
35-
"/docs/sqlalchemy/tips/",
36-
]
33+
name = "SQLAlchemy"
34+
pages = [
35+
"/docs/sqlalchemy/tutorial/",
36+
"/docs/sqlalchemy/tips/",
37+
]

docs/pages/docs/django/authorization.md

+10-7
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class Query(ObjectType):
5858
## User-based Queryset Filtering
5959

6060
If you are using `graphql-django-view` you can access Django's request object
61-
via `info.request_context`.
61+
via `with_context` decorator.
6262

6363
```python
6464
from graphene import ObjectType
@@ -71,18 +71,20 @@ class Query(ObjectType):
7171
class Meta:
7272
abstract = True
7373

74-
def resolve_my_posts(self, args, info):
75-
if not info.request_context.user.is_authenticated():
74+
@with_context
75+
def resolve_my_posts(self, args, context, info):
76+
# context will reference to the Django request
77+
if not context.user.is_authenticated():
7678
return []
7779
else:
78-
return Post.objects.filter(owner=info.request_context.user)
80+
return Post.objects.filter(owner=context.user)
7981
```
8082

8183
If you're using your own view, passing the request context into the schema is
8284
simple.
8385

8486
```python
85-
result = schema.execute(query, request_context=request)
87+
result = schema.execute(query, context_value=request)
8688
```
8789

8890
## Filtering ID-based node access
@@ -100,13 +102,14 @@ class PostNode(DjangoNode):
100102
only_fields = ('title', 'content')
101103

102104
@classmethod
103-
def get_node(Cls, id, info):
105+
@with_context
106+
def get_node(Cls, id, context, info):
104107
try:
105108
post = Cls._meta.model.objects.get(id=id)
106109
except Cls._meta.model.DoesNotExist:
107110
return None
108111

109-
if post.published or info.request_context.user is post.owner:
112+
if post.published or context.user is post.owner:
110113
return Cls(instance)
111114
else:
112115
return None

docs/pages/docs/resolvers.md

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
title: Resolvers
3+
description: Walkthrough Resolvers
4+
---
5+
6+
# Resolvers
7+
8+
A resolver is a method that resolves certain field within a `ObjectType`.
9+
The resolver of a field will be, if not specified otherwise, the `resolve_{field_name}` within the `ObjectType`.
10+
11+
By default a resolver will take the `args`, and `info` arguments.
12+
*This is likely to be simplified in the future*.
13+
14+
15+
## Quick example
16+
17+
This example model defines a `Query` type, which has a reverse field that reverses the given `word`
18+
argument using the `resolve_reverse` method in the class.
19+
20+
```python
21+
import graphene
22+
23+
class Query(graphene.ObjectType):
24+
reverse = graphene.String(word=graphene.String())
25+
26+
def resolve_reverse(self, args, info):
27+
word = args.get('word')
28+
return word[::-1]
29+
```
30+
31+
## Resolvers outside the class
32+
33+
A field could also specify a custom resolver outside the class:
34+
35+
```python
36+
import graphene
37+
38+
def reverse(root, args, info):
39+
word = args.get('word')
40+
return word[::-1]
41+
42+
class Query(graphene.ObjectType):
43+
reverse = graphene.String(word=graphene.String(), resolver=reverse)
44+
```
45+
46+
47+
## Context
48+
49+
A query in a GraphQL schema could have some context that we can use in any resolver.
50+
In this case we need to decorate the resolver function with `with_context`.
51+
52+
```python
53+
class Query(graphene.ObjectType):
54+
name = graphene.String()
55+
56+
@with_context
57+
def resolve_name(self, args, context, info):
58+
return context['name']
59+
60+
61+
result = schema.execute(query, context_value={'name': 'Peter'})
62+
```

0 commit comments

Comments
 (0)