@@ -25,7 +25,42 @@ Install last stable version from Pypi.
2525 pip install django-graphql-extensions
2626
2727
28- Configure your ``GraphQLView `` to return appropriate **error responses **.
28+ Authentication
29+ --------------
30+
31+ - ``@login_required ``
32+ - ``@staff_member_required ``
33+ - ``@permission_required ``
34+
35+ .. code :: python
36+
37+ from django.contrib.auth import get_user_model
38+
39+ import graphene
40+ from graphql_extensions.auth.decorators import (
41+ login_required, staff_member_required
42+ )
43+
44+
45+ class Query (graphene .ObjectType ):
46+ me = graphene.Field(UserType)
47+ users = graphene.List(UserType)
48+
49+ @login_required
50+ def resolve_me (self , info , ** kwargs ):
51+ return info.context.user
52+
53+ @staff_member_required
54+ def resolve_users (self , info , ** kwargs ):
55+ return get_user_model().objects.all()
56+
57+
58+ Errors
59+ ------
60+
61+ Returning appropriate **error responses ** and **masking ** error messages sent to the client.
62+
63+ Configure your ``GraphQLView ``.
2964
3065.. code :: python
3166
@@ -34,36 +69,67 @@ Configure your ``GraphQLView`` to return appropriate **error responses**.
3469 from graphql_extensions.views import GraphQLView
3570
3671 urlpatterns = [
37- path(' ' , GraphQLView.as_view(), name = ' graphql- index' ),
72+ path(' ' , GraphQLView.as_view(), name = ' index' ),
3873 ]
3974
75+ **Exceptions **
4076
41- Some features
42- -------------
77+ .. code :: python
4378
44- - **Authentication ** extensions, ``@login_required ``, ``@staff_member_required `` and ``@permission_required ``.
45- - Returning appropriate **error responses **, ``graphql_extensions.views.GraphQLView ``.
46- - **Pre-built mutations ** that provide for commonly used patterns, ``RetrieveMixin `` and ``UpdateMixin ``.
47- - Complete support for `Relay `_.
48- - Custom *Graphene * **types **, ``Email ``, ``Timestamp ``, ``Choices ``, ``CamelJSON ``...
49- - Helper classes to improve support for **testing ** ``GraphQLTestCase ``.
79+ from graphql_extensions import exceptions
5080
51- .. _Relay : https://facebook.github.io/relay/
81+
82+ raise exceptions.GraphQLError()
83+ raise exceptions.NotAuthenticated()
84+ raise exceptions.PermissionDenied()
85+ raise exceptions.ValidationError()
86+ raise exceptions.NotFound()
87+
88+
89+ **Payload **
90+
91+ .. code :: js
92+
93+ {
94+ " errors" : [
95+ {
96+ " type" : " NotFound" ,
97+ " message" : " GraphQL object not found" ,
98+ " code" : " notFound" ,
99+ " data" : {
100+ " id" : 1
101+ },
102+ " path" : [" updateGroup" ],
103+ " operation" : " mutation" ,
104+ " trace" : [
105+ " File \" /app/schema.py\" , line 30, in mutate\n group = cls.update(info, **kwargs)\n " ,
106+ " File \" /graphql_extensions/mixins.py\" , line 32, in update\n instance = cls.get_object(context, id=id)\n " ,
107+ " File \" /graphql_extensions/mixins.py\" , line 21, in get_object\n raise exceptions.NotFound(**kwargs)\n "
108+ ]
109+ }
110+ ],
111+ " data" : {
112+ " updateGroup" : null
113+ }
114+ }
52115
53116
54- Demo
55- ----
117+ Mixins
118+ ------
56119
57- **Code **
120+ **Pre-built mutations ** that provide for commonly used patterns.
121+
122+ - ``RetrieveMixin ``
123+ - ``UpdateMixin ``
58124
59125.. code :: python
60126
61127 from django.contrib.auth.models import Group
62128
63129 import graphene
130+ from graphene_django import DjangoObjectType
64131 from graphql_extensions import mixins
65132 from graphql_extensions.auth.decorators import login_required
66- from graphene_django import DjangoObjectType
67133
68134
69135 class GroupType (DjangoObjectType ):
@@ -90,32 +156,73 @@ Demo
90156 return cls (group = group)
91157
92158
93- **Payload **
159+ Testing
160+ -------
94161
95- .. code :: js
162+ Helper classes to improve support for ** testing **.
96163
97- {
98- " errors" : [
99- {
100- " type" : " NotFound" ,
101- " message" : " GraphQL object not found" ,
102- " code" : " not_found" ,
103- " data" : {
104- " id" : 1
105- },
106- " path" : " updateGroup" ,
107- " operation" : " mutation" ,
108- " trace" : [
109- " File \" /app/schema.py\" , line 30, in mutate\n group = cls.update(info, **kwargs)\n " ,
110- " File \" /graphql_extensions/mixins.py\" , line 32, in update\n instance = cls.get_object(context, id=id)\n " ,
111- " File \" /graphql_extensions/mixins.py\" , line 21, in get_object\n raise exceptions.NotFound(**kwargs)\n "
112- ]
113- }
114- ],
115- " data" : {
116- " updateGroup" : null
117- }
118- }
164+ - ``GraphQLTestCase ``
165+
166+
167+ .. code :: python
168+
169+ from graphql_extensions.testcases import GraphQLTestCase
170+
171+
172+ class UsersTests (GraphQLTestCase ):
173+
174+ def test_create_user (self ):
175+ query = '''
176+ mutation CreateUser($username: String!, $password: String!) {
177+ createUser(username: $username, password: $password) {
178+ user {
179+ id
180+ }
181+ }
182+ }'''
183+
184+ username = ' test'
185+ password = ' dolphins'
186+
187+ response = self .client.execute(query, {
188+ ' username' : username,
189+ ' password' : password,
190+ })
191+
192+ self .assertFalse(response.errors)
193+ self .assertTrue(response.data[' user' ])
194+
195+ self .client.login(username = username, password = password)
196+
197+ query = '''
198+ {
199+ me {
200+ username
201+ }
202+ }'''
203+
204+ response = self .client.execute(query)
205+ self .assertEqual(response.data[' me' ][' username' ], username)
206+
207+
208+ Types
209+ -----
210+
211+ Custom *Graphene * **types **.
212+
213+ - ``Email ``
214+ - ``Timestamp ``
215+ - ``Choices ``
216+ - ``CamelJSON ``
217+ - ...
218+
219+
220+ Relay
221+ -----
222+
223+ Complete support for `Relay `_.
224+
225+ .. _Relay : https://facebook.github.io/relay/
119226
120227
121228.. |Pypi | image :: https://img.shields.io/pypi/v/django-graphql-extensions.svg
0 commit comments