@@ -20,27 +20,26 @@ Full example
20
20
# my_app/schema.py
21
21
22
22
import graphene
23
+ from graphene_django import DjangoObjectType
23
24
24
- from graphene_django.types import DjangoObjectType
25
25
from .models import Question
26
26
27
-
28
27
class QuestionType (DjangoObjectType ):
29
28
class Meta :
30
29
model = Question
30
+ fields = (" id" , " question_text" )
31
31
32
-
33
- class Query :
32
+ class Query (graphene .ObjectType ):
34
33
questions = graphene.List(QuestionType)
35
- question = graphene.Field(QuestionType, question_id = graphene.String())
34
+ question_by_id = graphene.Field(QuestionType, id = graphene.String())
36
35
37
- def resolve_questions (self , info , ** kwargs ):
36
+ def resolve_questions (root , info , ** kwargs ):
38
37
# Querying a list
39
38
return Question.objects.all()
40
39
41
- def resolve_question ( self , info , question_id ):
40
+ def resolve_question_by_id ( root , info , id ):
42
41
# Querying a single question
43
- return Question.objects.get(pk = question_id )
42
+ return Question.objects.get(pk = id )
44
43
45
44
46
45
Specifying which fields to include
@@ -60,21 +59,27 @@ Show **only** these fields on the model:
60
59
61
60
.. code :: python
62
61
62
+ from graphene_django import DjangoObjectType
63
+ from .models import Question
64
+
63
65
class QuestionType (DjangoObjectType ):
64
66
class Meta :
65
67
model = Question
66
- fields = (' id ' , ' question_text' )
68
+ fields = (" id " , " question_text" )
67
69
68
- You can also set the ``fields `` attribute to the special value ``' __all__' `` to indicate that all fields in the model should be used.
70
+ You can also set the ``fields `` attribute to the special value ``" __all__" `` to indicate that all fields in the model should be used.
69
71
70
72
For example:
71
73
72
74
.. code :: python
73
75
76
+ from graphene_django import DjangoObjectType
77
+ from .models import Question
78
+
74
79
class QuestionType (DjangoObjectType ):
75
80
class Meta :
76
81
model = Question
77
- fields = ' __all__'
82
+ fields = " __all__"
78
83
79
84
80
85
``exclude ``
@@ -84,10 +89,13 @@ Show all fields **except** those in ``exclude``:
84
89
85
90
.. code :: python
86
91
92
+ from graphene_django import DjangoObjectType
93
+ from .models import Question
94
+
87
95
class QuestionType (DjangoObjectType ):
88
96
class Meta :
89
97
model = Question
90
- exclude = (' question_text' ,)
98
+ exclude = (" question_text" ,)
91
99
92
100
93
101
Customising fields
@@ -97,16 +105,19 @@ You can completely overwrite a field, or add new fields, to a ``DjangoObjectType
97
105
98
106
.. code :: python
99
107
108
+ from graphene_django import DjangoObjectType
109
+ from .models import Question
110
+
100
111
class QuestionType (DjangoObjectType ):
101
112
102
113
class Meta :
103
114
model = Question
104
- fields = (' id ' , ' question_text' )
115
+ fields = (" id " , " question_text" )
105
116
106
117
extra_field = graphene.String()
107
118
108
119
def resolve_extra_field (self , info ):
109
- return ' hello!'
120
+ return " hello!"
110
121
111
122
112
123
Choices to Enum conversion
@@ -121,12 +132,19 @@ For example the following ``Model`` and ``DjangoObjectType``:
121
132
122
133
.. code :: python
123
134
124
- class PetModel ( models . Model ):
125
- kind = models.CharField( max_length = 100 , choices = (( ' cat ' , ' Cat ' ), ( ' dog ' , ' Dog ' )))
135
+ from django.db import models
136
+ from graphene_django import DjangoObjectType
126
137
127
- class Pet (DjangoObjectType ):
128
- class Meta :
129
- model = PetModel
138
+ class PetModel (models .Model ):
139
+ kind = models.CharField(
140
+ max_length = 100 ,
141
+ choices = ((" cat" , " Cat" ), (" dog" , " Dog" ))
142
+ )
143
+
144
+ class Pet (DjangoObjectType ):
145
+ class Meta :
146
+ model = PetModel
147
+ fields = (" id" , " kind" ,)
130
148
131
149
Results in the following GraphQL schema definition:
132
150
@@ -148,27 +166,35 @@ You can disable this automatic conversion by setting
148
166
149
167
.. code :: python
150
168
151
- class Pet (DjangoObjectType ):
152
- class Meta :
153
- model = PetModel
154
- convert_choices_to_enum = False
169
+ from graphene_django import DjangoObjectType
170
+ from .models import PetModel
171
+
172
+ class Pet (DjangoObjectType ):
173
+ class Meta :
174
+ model = PetModel
175
+ fields = (" id" , " kind" ,)
176
+ convert_choices_to_enum = False
155
177
156
178
.. code ::
157
179
158
- type Pet {
159
- id: ID!
160
- kind: String!
161
- }
180
+ type Pet {
181
+ id: ID!
182
+ kind: String!
183
+ }
162
184
163
185
You can also set ``convert_choices_to_enum `` to a list of fields that should be
164
186
automatically converted into enums:
165
187
166
188
.. code :: python
167
189
168
- class Pet (DjangoObjectType ):
169
- class Meta :
170
- model = PetModel
171
- convert_choices_to_enum = [' kind' ]
190
+ from graphene_django import DjangoObjectType
191
+ from .models import PetModel
192
+
193
+ class Pet (DjangoObjectType ):
194
+ class Meta :
195
+ model = PetModel
196
+ fields = (" id" , " kind" ,)
197
+ convert_choices_to_enum = [" kind" ]
172
198
173
199
**Note: ** Setting ``convert_choices_to_enum = [] `` is the same as setting it to
174
200
``False ``.
@@ -181,6 +207,8 @@ Say you have the following models:
181
207
182
208
.. code :: python
183
209
210
+ from django.db import models
211
+
184
212
class Category (models .Model ):
185
213
foo = models.CharField(max_length = 256 )
186
214
@@ -192,20 +220,27 @@ When ``Question`` is published as a ``DjangoObjectType`` and you want to add ``C
192
220
193
221
.. code :: python
194
222
223
+ from graphene_django import DjangoObjectType
224
+ from .models import Question
225
+
195
226
class QuestionType (DjangoObjectType ):
196
227
class Meta :
197
228
model = Question
198
- fields = (' category' ,)
229
+ fields = (" category" ,)
199
230
200
231
Then all query-able related models must be defined as DjangoObjectType subclass,
201
232
or they will fail to show if you are trying to query those relation fields. You only
202
233
need to create the most basic class for this to work:
203
234
204
235
.. code :: python
205
236
237
+ from graphene_django import DjangoObjectType
238
+ from .models import Category
239
+
206
240
class CategoryType (DjangoObjectType ):
207
241
class Meta :
208
242
model = Category
243
+ fields = (" foo" ,)
209
244
210
245
.. _django-objecttype-get-queryset :
211
246
@@ -220,7 +255,6 @@ Use this to control filtering on the ObjectType level instead of the Query objec
220
255
from graphene_django.types import DjangoObjectType
221
256
from .models import Question
222
257
223
-
224
258
class QuestionType (DjangoObjectType ):
225
259
class Meta :
226
260
model = Question
@@ -240,18 +274,22 @@ This resolve method should follow this format:
240
274
241
275
.. code :: python
242
276
243
- def resolve_foo (self , info , ** kwargs ):
277
+ def resolve_foo (parent , info , ** kwargs ):
244
278
245
279
Where "foo" is the name of the field declared in the ``Query `` object.
246
280
247
281
.. code :: python
248
282
249
- class Query :
283
+ import graphene
284
+ from .models import Question
285
+ from .types import QuestionType
286
+
287
+ class Query (graphene .ObjectType ):
250
288
foo = graphene.List(QuestionType)
251
289
252
- def resolve_foo (self , info , ** kwargs ):
253
- id = kwargs.get(' id ' )
254
- return QuestionModel .objects.get(id )
290
+ def resolve_foo (root , info ):
291
+ id = kwargs.get(" id " )
292
+ return Question .objects.get(id )
255
293
256
294
Arguments
257
295
~~~~~~~~~
@@ -260,10 +298,18 @@ Additionally, Resolvers will receive **any arguments declared in the field defin
260
298
261
299
.. code :: python
262
300
263
- class Query :
264
- question = graphene.Field(Question, foo = graphene.String(), bar = graphene.Int())
301
+ import graphene
302
+ from .models import Question
303
+ from .types import QuestionType
265
304
266
- def resolve_question (self , info , foo , bar ):
305
+ class Query (graphene .ObjectType ):
306
+ question = graphene.Field(
307
+ QuestionType,
308
+ foo = graphene.String(),
309
+ bar = graphene.Int()
310
+ )
311
+
312
+ def resolve_question (root , info , foo , bar ):
267
313
# If `foo` or `bar` are declared in the GraphQL query they will be here, else None.
268
314
return Question.objects.filter(foo = foo, bar = bar).first()
269
315
@@ -278,7 +324,15 @@ of Django's ``HTTPRequest`` in your resolve methods, such as checking for authen
278
324
279
325
.. code :: python
280
326
281
- def resolve_questions (self , info , ** kwargs ):
327
+ import graphene
328
+
329
+ from .models import Question
330
+ from .types import QuestionType
331
+
332
+ class Query (graphene .ObjectType ):
333
+ questions = graphene.List(QuestionType)
334
+
335
+ def resolve_questions (root , info ):
282
336
# See if a user is authenticated
283
337
if info.context.user.is_authenticated():
284
338
return Question.objects.all()
@@ -305,15 +359,13 @@ Django models and your external API.
305
359
import graphene
306
360
from .models import Question
307
361
308
-
309
362
class MyQuestion (graphene .ObjectType ):
310
363
text = graphene.String()
311
364
312
-
313
- class Query :
365
+ class Query (graphene .ObjectType ):
314
366
question = graphene.Field(MyQuestion, question_id = graphene.String())
315
367
316
- def resolve_question (self , info , question_id ):
368
+ def resolve_question (root , info , question_id ):
317
369
question = Question.objects.get(pk = question_id)
318
370
return MyQuestion(
319
371
text = question.question_text
@@ -343,25 +395,22 @@ the core graphene pages for more information on customizing the Relay experience
343
395
from graphene_django import DjangoObjectType
344
396
from .models import Question
345
397
346
-
347
398
class QuestionType (DjangoObjectType ):
348
399
class Meta :
349
400
model = Question
350
- interfaces = (relay.Node,)
351
-
401
+ interfaces = (relay.Node,) # make sure you add this
402
+ fields = " __all__ "
352
403
353
404
class QuestionConnection (relay .Connection ):
354
405
class Meta :
355
406
node = QuestionType
356
407
357
-
358
408
class Query :
359
409
questions = relay.ConnectionField(QuestionConnection)
360
410
361
411
def resolve_questions (root , info , ** kwargs ):
362
412
return Question.objects.all()
363
413
364
-
365
414
You can now execute queries like:
366
415
367
416
0 commit comments