Skip to content

Commit d9ab8ac

Browse files
authored
document auth pattern: return None with resolve method (#1106)
* document auth pattern: return None with resolve method * (doc, auth): also show that one can raise an exception in a resolve method
1 parent 5dea6ff commit d9ab8ac

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Diff for: docs/authorization.rst

+25
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,31 @@ conversely you can use ``exclude`` meta attribute.
4848
exclude = ('published', 'owner')
4949
interfaces = (relay.Node, )
5050
51+
52+
Another pattern is to have a resolve method act as a gatekeeper, returning None
53+
or raising an exception if the client isn't allowed to see the data.
54+
55+
.. code:: python
56+
57+
from graphene import relay
58+
from graphene_django.types import DjangoObjectType
59+
from .models import Post
60+
61+
class PostNode(DjangoObjectType):
62+
class Meta:
63+
model = Post
64+
fields = ('title', 'content', 'owner')
65+
interfaces = (relay.Node, )
66+
67+
def resolve_owner(self, info):
68+
user = info.context.user
69+
if user.is_anonymous:
70+
raise PermissionDenied("Please login")
71+
if not user.is_staff:
72+
return None
73+
return self.owner
74+
75+
5176
Queryset Filtering On Lists
5277
---------------------------
5378

0 commit comments

Comments
 (0)