@@ -20,28 +20,26 @@ Full example
2020 # my_app/schema.py
2121
2222 import graphene
23+ from graphene_django import DjangoObjectType
2324
24- from graphene_django.types import DjangoObjectType
2525 from .models import Question
2626
27-
2827 class QuestionType (DjangoObjectType ):
2928 class Meta :
3029 model = Question
31- fields = ' __all__'
32-
30+ fields = (" id" , " question_text" )
3331
34- class Query :
32+ class Query ( graphene . ObjectType ) :
3533 questions = graphene.List(QuestionType)
36- question = graphene.Field(QuestionType, question_id = graphene.String())
34+ question_by_id = graphene.Field(QuestionType, id = graphene.String())
3735
38- def resolve_questions (self , info , ** kwargs ):
36+ def resolve_questions (root , info , ** kwargs ):
3937 # Querying a list
4038 return Question.objects.all()
4139
42- def resolve_question ( self , info , question_id ):
40+ def resolve_question_by_id ( root , info , id ):
4341 # Querying a single question
44- return Question.objects.get(pk = question_id )
42+ return Question.objects.get(pk = id )
4543
4644
4745 Specifying which fields to include
@@ -64,21 +62,27 @@ Show **only** these fields on the model:
6462
6563.. code :: python
6664
65+ from graphene_django import DjangoObjectType
66+ from .models import Question
67+
6768 class QuestionType (DjangoObjectType ):
6869 class Meta :
6970 model = Question
70- fields = (' id ' , ' question_text' )
71+ fields = (" id " , " question_text" )
7172
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.
7374
7475For example:
7576
7677.. code :: python
7778
79+ from graphene_django import DjangoObjectType
80+ from .models import Question
81+
7882 class QuestionType (DjangoObjectType ):
7983 class Meta :
8084 model = Question
81- fields = ' __all__'
85+ fields = " __all__"
8286
8387
8488 ``exclude ``
@@ -88,10 +92,13 @@ Show all fields **except** those in ``exclude``:
8892
8993.. code :: python
9094
95+ from graphene_django import DjangoObjectType
96+ from .models import Question
97+
9198 class QuestionType (DjangoObjectType ):
9299 class Meta :
93100 model = Question
94- exclude = (' question_text' ,)
101+ exclude = (" question_text" ,)
95102
96103
97104 Customising fields
@@ -101,16 +108,19 @@ You can completely overwrite a field, or add new fields, to a ``DjangoObjectType
101108
102109.. code :: python
103110
111+ from graphene_django import DjangoObjectType
112+ from .models import Question
113+
104114 class QuestionType (DjangoObjectType ):
105115
106116 class Meta :
107117 model = Question
108- fields = (' id ' , ' question_text' )
118+ fields = (" id " , " question_text" )
109119
110120 extra_field = graphene.String()
111121
112122 def resolve_extra_field (self , info ):
113- return ' hello!'
123+ return " hello!"
114124
115125
116126 Choices to Enum conversion
@@ -125,13 +135,19 @@ For example the following ``Model`` and ``DjangoObjectType``:
125135
126136.. code :: python
127137
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
130140
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" ,)
135151
136152 Results in the following GraphQL schema definition:
137153
@@ -153,29 +169,35 @@ You can disable this automatic conversion by setting
153169
154170.. code :: python
155171
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
161180
162181 .. code ::
163182
164- type Pet {
165- id: ID!
166- kind: String!
167- }
183+ type Pet {
184+ id: ID!
185+ kind: String!
186+ }
168187
169188 You can also set ``convert_choices_to_enum `` to a list of fields that should be
170189automatically converted into enums:
171190
172191.. code :: python
173192
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" ]
179201
180202 **Note: ** Setting ``convert_choices_to_enum = [] `` is the same as setting it to
181203``False ``.
@@ -188,6 +210,8 @@ Say you have the following models:
188210
189211.. code :: python
190212
213+ from django.db import models
214+
191215 class Category (models .Model ):
192216 foo = models.CharField(max_length = 256 )
193217
@@ -199,21 +223,27 @@ When ``Question`` is published as a ``DjangoObjectType`` and you want to add ``C
199223
200224.. code :: python
201225
226+ from graphene_django import DjangoObjectType
227+ from .models import Question
228+
202229 class QuestionType (DjangoObjectType ):
203230 class Meta :
204231 model = Question
205- fields = (' category' ,)
232+ fields = (" category" ,)
206233
207234 Then all query-able related models must be defined as DjangoObjectType subclass,
208235or they will fail to show if you are trying to query those relation fields. You only
209236need to create the most basic class for this to work:
210237
211238.. code :: python
212239
240+ from graphene_django import DjangoObjectType
241+ from .models import Category
242+
213243 class CategoryType (DjangoObjectType ):
214244 class Meta :
215245 model = Category
216- fields = ' __all__ '
246+ fields = ( " foo " ,)
217247
218248 .. _django-objecttype-get-queryset :
219249
@@ -228,11 +258,10 @@ Use this to control filtering on the ObjectType level instead of the Query objec
228258 from graphene_django.types import DjangoObjectType
229259 from .models import Question
230260
231-
232261 class QuestionType (DjangoObjectType ):
233262 class Meta :
234263 model = Question
235- fields = ' __all__'
264+ fields = " __all__"
236265
237266 @ classmethod
238267 def get_queryset (cls , queryset , info ):
@@ -249,18 +278,22 @@ This resolve method should follow this format:
249278
250279.. code :: python
251280
252- def resolve_foo (self , info , ** kwargs ):
281+ def resolve_foo (parent , info , ** kwargs ):
253282
254283 Where "foo" is the name of the field declared in the ``Query `` object.
255284
256285.. code :: python
257286
258- class Query :
287+ import graphene
288+ from .models import Question
289+ from .types import QuestionType
290+
291+ class Query (graphene .ObjectType ):
259292 foo = graphene.List(QuestionType)
260293
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 )
264297
265298 Arguments
266299~~~~~~~~~
@@ -269,10 +302,18 @@ Additionally, Resolvers will receive **any arguments declared in the field defin
269302
270303.. code :: python
271304
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
274308
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 ):
276317 # If `foo` or `bar` are declared in the GraphQL query they will be here, else None.
277318 return Question.objects.filter(foo = foo, bar = bar).first()
278319
@@ -287,7 +328,15 @@ of Django's ``HTTPRequest`` in your resolve methods, such as checking for authen
287328
288329.. code :: python
289330
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 ):
291340 # See if a user is authenticated
292341 if info.context.user.is_authenticated():
293342 return Question.objects.all()
@@ -314,15 +363,13 @@ Django models and your external API.
314363 import graphene
315364 from .models import Question
316365
317-
318366 class MyQuestion (graphene .ObjectType ):
319367 text = graphene.String()
320368
321-
322- class Query :
369+ class Query (graphene .ObjectType ):
323370 question = graphene.Field(MyQuestion, question_id = graphene.String())
324371
325- def resolve_question (self , info , question_id ):
372+ def resolve_question (root , info , question_id ):
326373 question = Question.objects.get(pk = question_id)
327374 return MyQuestion(
328375 text = question.question_text
@@ -352,26 +399,22 @@ the core graphene pages for more information on customizing the Relay experience
352399 from graphene_django import DjangoObjectType
353400 from .models import Question
354401
355-
356402 class QuestionType (DjangoObjectType ):
357403 class Meta :
358404 model = Question
359- fields = ' __all__'
360- interfaces = (relay.Node,)
361-
405+ interfaces = (relay.Node,) # make sure you add this
406+ fields = " __all__"
362407
363408 class QuestionConnection (relay .Connection ):
364409 class Meta :
365410 node = QuestionType
366411
367-
368412 class Query :
369413 questions = relay.ConnectionField(QuestionConnection)
370414
371415 def resolve_questions (root , info , ** kwargs ):
372416 return Question.objects.all()
373417
374-
375418 You can now execute queries like:
376419
377420
0 commit comments