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