Skip to content

Commit 97de26b

Browse files
authored
Update tutorial docs (#994)
1 parent b552dca commit 97de26b

File tree

4 files changed

+177
-214
lines changed

4 files changed

+177
-214
lines changed

docs/installation.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ Add ``graphene_django`` to the ``INSTALLED_APPS`` in the ``settings.py`` file of
2525
2626
INSTALLED_APPS = [
2727
...
28-
'django.contrib.staticfiles', # Required for GraphiQL
29-
'graphene_django'
28+
"django.contrib.staticfiles", # Required for GraphiQL
29+
"graphene_django"
3030
]
3131
3232
@@ -63,7 +63,7 @@ Finally, define the schema location for Graphene in the ``settings.py`` file of
6363
.. code:: python
6464
6565
GRAPHENE = {
66-
'SCHEMA': 'django_root.schema.schema'
66+
"SCHEMA": "django_root.schema.schema"
6767
}
6868
6969
Where ``path.schema.schema`` is the location of the ``Schema`` object in your Django project.
@@ -75,7 +75,7 @@ The most basic ``schema.py`` looks like this:
7575
import graphene
7676
7777
class Query(graphene.ObjectType):
78-
pass
78+
hello = graphene.String(default_value="Hi!")
7979
8080
schema = graphene.Schema(query=Query)
8181

docs/queries.rst

Lines changed: 100 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +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
30+
fields = ("id", "question_text")
3131
32-
33-
class Query:
32+
class Query(graphene.ObjectType):
3433
questions = graphene.List(QuestionType)
35-
question = graphene.Field(QuestionType, question_id=graphene.String())
34+
question_by_id = graphene.Field(QuestionType, id=graphene.String())
3635
37-
def resolve_questions(self, info, **kwargs):
36+
def resolve_questions(root, info, **kwargs):
3837
# Querying a list
3938
return Question.objects.all()
4039
41-
def resolve_question(self, info, question_id):
40+
def resolve_question_by_id(root, info, id):
4241
# Querying a single question
43-
return Question.objects.get(pk=question_id)
42+
return Question.objects.get(pk=id)
4443
4544
4645
Specifying which fields to include
@@ -60,21 +59,27 @@ Show **only** these fields on the model:
6059

6160
.. code:: python
6261
62+
from graphene_django import DjangoObjectType
63+
from .models import Question
64+
6365
class QuestionType(DjangoObjectType):
6466
class Meta:
6567
model = Question
66-
fields = ('id', 'question_text')
68+
fields = ("id", "question_text")
6769
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.
6971

7072
For example:
7173

7274
.. code:: python
7375
76+
from graphene_django import DjangoObjectType
77+
from .models import Question
78+
7479
class QuestionType(DjangoObjectType):
7580
class Meta:
7681
model = Question
77-
fields = '__all__'
82+
fields = "__all__"
7883
7984
8085
``exclude``
@@ -84,10 +89,13 @@ Show all fields **except** those in ``exclude``:
8489

8590
.. code:: python
8691
92+
from graphene_django import DjangoObjectType
93+
from .models import Question
94+
8795
class QuestionType(DjangoObjectType):
8896
class Meta:
8997
model = Question
90-
exclude = ('question_text',)
98+
exclude = ("question_text",)
9199
92100
93101
Customising fields
@@ -97,16 +105,19 @@ You can completely overwrite a field, or add new fields, to a ``DjangoObjectType
97105

98106
.. code:: python
99107
108+
from graphene_django import DjangoObjectType
109+
from .models import Question
110+
100111
class QuestionType(DjangoObjectType):
101112
102113
class Meta:
103114
model = Question
104-
fields = ('id', 'question_text')
115+
fields = ("id", "question_text")
105116
106117
extra_field = graphene.String()
107118
108119
def resolve_extra_field(self, info):
109-
return 'hello!'
120+
return "hello!"
110121
111122
112123
Choices to Enum conversion
@@ -121,12 +132,19 @@ For example the following ``Model`` and ``DjangoObjectType``:
121132

122133
.. code:: python
123134
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
126137
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",)
130148
131149
Results in the following GraphQL schema definition:
132150

@@ -148,27 +166,35 @@ You can disable this automatic conversion by setting
148166

149167
.. code:: python
150168
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
155177
156178
.. code::
157179
158-
type Pet {
159-
id: ID!
160-
kind: String!
161-
}
180+
type Pet {
181+
id: ID!
182+
kind: String!
183+
}
162184
163185
You can also set ``convert_choices_to_enum`` to a list of fields that should be
164186
automatically converted into enums:
165187

166188
.. code:: python
167189
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"]
172198
173199
**Note:** Setting ``convert_choices_to_enum = []`` is the same as setting it to
174200
``False``.
@@ -181,6 +207,8 @@ Say you have the following models:
181207

182208
.. code:: python
183209
210+
from django.db import models
211+
184212
class Category(models.Model):
185213
foo = models.CharField(max_length=256)
186214
@@ -192,20 +220,27 @@ When ``Question`` is published as a ``DjangoObjectType`` and you want to add ``C
192220

193221
.. code:: python
194222
223+
from graphene_django import DjangoObjectType
224+
from .models import Question
225+
195226
class QuestionType(DjangoObjectType):
196227
class Meta:
197228
model = Question
198-
fields = ('category',)
229+
fields = ("category",)
199230
200231
Then all query-able related models must be defined as DjangoObjectType subclass,
201232
or they will fail to show if you are trying to query those relation fields. You only
202233
need to create the most basic class for this to work:
203234

204235
.. code:: python
205236
237+
from graphene_django import DjangoObjectType
238+
from .models import Category
239+
206240
class CategoryType(DjangoObjectType):
207241
class Meta:
208242
model = Category
243+
fields = ("foo",)
209244
210245
.. _django-objecttype-get-queryset:
211246

@@ -220,7 +255,6 @@ Use this to control filtering on the ObjectType level instead of the Query objec
220255
from graphene_django.types import DjangoObjectType
221256
from .models import Question
222257
223-
224258
class QuestionType(DjangoObjectType):
225259
class Meta:
226260
model = Question
@@ -240,18 +274,22 @@ This resolve method should follow this format:
240274

241275
.. code:: python
242276
243-
def resolve_foo(self, info, **kwargs):
277+
def resolve_foo(parent, info, **kwargs):
244278
245279
Where "foo" is the name of the field declared in the ``Query`` object.
246280

247281
.. code:: python
248282
249-
class Query:
283+
import graphene
284+
from .models import Question
285+
from .types import QuestionType
286+
287+
class Query(graphene.ObjectType):
250288
foo = graphene.List(QuestionType)
251289
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)
255293
256294
Arguments
257295
~~~~~~~~~
@@ -260,10 +298,18 @@ Additionally, Resolvers will receive **any arguments declared in the field defin
260298

261299
.. code:: python
262300
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
265304
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):
267313
# If `foo` or `bar` are declared in the GraphQL query they will be here, else None.
268314
return Question.objects.filter(foo=foo, bar=bar).first()
269315
@@ -278,7 +324,15 @@ of Django's ``HTTPRequest`` in your resolve methods, such as checking for authen
278324

279325
.. code:: python
280326
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):
282336
# See if a user is authenticated
283337
if info.context.user.is_authenticated():
284338
return Question.objects.all()
@@ -305,15 +359,13 @@ Django models and your external API.
305359
import graphene
306360
from .models import Question
307361
308-
309362
class MyQuestion(graphene.ObjectType):
310363
text = graphene.String()
311364
312-
313-
class Query:
365+
class Query(graphene.ObjectType):
314366
question = graphene.Field(MyQuestion, question_id=graphene.String())
315367
316-
def resolve_question(self, info, question_id):
368+
def resolve_question(root, info, question_id):
317369
question = Question.objects.get(pk=question_id)
318370
return MyQuestion(
319371
text=question.question_text
@@ -343,25 +395,22 @@ the core graphene pages for more information on customizing the Relay experience
343395
from graphene_django import DjangoObjectType
344396
from .models import Question
345397
346-
347398
class QuestionType(DjangoObjectType):
348399
class Meta:
349400
model = Question
350-
interfaces = (relay.Node,)
351-
401+
interfaces = (relay.Node,) # make sure you add this
402+
fields = "__all__"
352403
353404
class QuestionConnection(relay.Connection):
354405
class Meta:
355406
node = QuestionType
356407
357-
358408
class Query:
359409
questions = relay.ConnectionField(QuestionConnection)
360410
361411
def resolve_questions(root, info, **kwargs):
362412
return Question.objects.all()
363413
364-
365414
You can now execute queries like:
366415

367416

0 commit comments

Comments
 (0)