Skip to content

Commit 0f7844b

Browse files
author
Paul Hallett
committed
Start on mutations
1 parent bdcc0f2 commit 0f7844b

5 files changed

Lines changed: 146 additions & 22 deletions

File tree

docs/index.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ Graphene-Django
33

44
Welcome to the Graphene-Django docs.
55

6-
First time? We recommend you start with the installation guide and then read the basic tutorial.
6+
Graphene-Django is built on top of `Graphene <https://docs.graphene-python.org/en/latest/>`__.
7+
Graphene-Django provides some additional abstractions that make it easy to add GraphQL functionality to your Django project.
8+
9+
First time? We recommend you start with the installation guide to get set up and the basic tutorial.
10+
It is worth reading the `core graphene docs <https://docs.graphene-python.org/en/latest/>`__ to familiarize yourself with the basic utilities.
711

812
Core tenants
913
------------
@@ -22,7 +26,6 @@ For more advanced use, check out the Relay tutorial.
2226
schema
2327
queries
2428
mutations
25-
relay
2629
filtering
2730
authorization
2831
debug

docs/installation.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Installation
1717
1818
pip install graphene-django
1919
20-
**We strongly recommend pinning against a specific version of graphene-django as new versions could introduce breaking changes to your project.**
20+
**We strongly recommend pinning against a specific version of Graphene-Django as new versions could introduce breaking changes to your project.**
2121

2222
Add ``graphene_django`` to the ``INSTALLED_APPS`` in the ``settings.py`` file of your Django project:
2323

@@ -42,6 +42,8 @@ We need to add a graphql URL to the ``urls.py`` of your Django project:
4242
url(r'^graphql', GraphQLView.as_view(graphiql=True)),
4343
]
4444
45+
(Changed ``graphiql=True`` to ``graphiql=False`` if you do not want to use the graphiQL API browser.)
46+
4547
Finally, define the schema location for Graphene in the ``settings.py`` file of your Django project:
4648

4749
.. code:: python

docs/mutations.rst

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,61 @@
11
Mutations
2-
=========
2+
=========
3+
4+
Introduction
5+
------------
6+
7+
Mutations allow you to `mutate` data.
8+
9+
With Graphene-Django we can take advantage of pre-existing Django features to
10+
quickly build CRUD functionality, while still using the core `graphene mutation <https://docs.graphene-python.org/en/latest/types/mutations/>`__
11+
features to add custom mutations to a Django project.
12+
13+
Simple example
14+
--------------
15+
16+
.. code:: python
17+
18+
import graphene
19+
20+
from graphene_django import DjangoObjectType
21+
22+
from .models import Question
23+
24+
25+
class QuestionType(DjangoObjectType):
26+
class Meta:
27+
model = Question
28+
29+
30+
class QuestionMutation(graphene.Mutation):
31+
class Input:
32+
# The input arguments for this mutation
33+
text = graphene.String(required=True)
34+
id = graphene.ID()
35+
36+
# The class attributes define the response of the mutation
37+
question = graphene.Field(QuestionType)
38+
39+
def mutate(self info, text, id):
40+
question = Question.objects.get(pk=id)
41+
question.text = text
42+
question.save()
43+
# Notice we return an instance of this mutation
44+
return QuestionMutation(question=question)
45+
46+
47+
class Mutation:
48+
update_question = QuestionMutation.Field()
49+
50+
51+
52+
Relay
53+
-----
54+
55+
56+
Django Forms
57+
------------
58+
59+
60+
Django REST Framework
61+
---------------------

docs/queries.rst

Lines changed: 69 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
1-
Queries
2-
=======
1+
Queries & ObjectTypes
2+
=====================
33

44
Introduction
55
------------
66

7-
The core functionality of GraphQL is to make queries and get data back.
7+
The core functionality of GraphQL is to make queries in order to get data.
88

99
Graphene-Django ships with a special ``DjangoObjectType`` that automagically transforms a Django Model
10-
into a Query object for you.
11-
12-
In the world of Django; a query object can be considered a Django Model, and a query field a
13-
Django Model field. However you are not limited to just Django Models with Graphene Django - you can use the standard
14-
``ObjectType`` to create custom fields or to provide an abstraction between your internal
15-
Django models and your external API.
10+
into a ``ObjectType`` for you.
1611

1712

1813
Full example
19-
------------
14+
~~~~~~~~~~~~
2015

2116
.. code:: python
2217
@@ -49,7 +44,7 @@ Full example
4944
Fields
5045
------
5146

52-
By default, ``DjangoObjectType`` will present all fields on a Model through graphQL.
47+
By default, ``DjangoObjectType`` will present all fields on a Model through GraphQL.
5348
If you don't want to do this you can change this by setting either ``only_fields`` and ``exclude_fields``.
5449

5550
only_fields
@@ -101,13 +96,14 @@ Default QuerySet
10196
-----------------
10297

10398
If you are using ``DjangoObjectType`` you can define a custom `get_queryset` method.
104-
Use this to control filtering on the object level instead of the **resolve method** level.
99+
Use this to control filtering on the ObjectType level instead of the Query object level.
105100

106101
.. code:: python
107102
108103
from graphene_django.types import DjangoObjectType
109104
from .models import Question
110105
106+
111107
class QuestionType(DjangoObjectType):
112108
class Meta:
113109
model = Question
@@ -159,9 +155,9 @@ Info
159155
~~~~
160156

161157
The ``info`` argument passed to all resolve methods holds some useful information.
162-
For graphene-django, the ``info.context`` object is the ``HTTPRequest`` object
158+
For Graphene-Django, the ``info.context`` object is the ``HTTPRequest`` object
163159
that would be familiar to any Django developer. This gives you the full functionality
164-
of Django's `HTTPRequest` in your resolve methods, such as checking for authenticated users:
160+
of Django's ``HTTPRequest`` in your resolve methods, such as checking for authenticated users:
165161

166162
.. code:: python
167163
@@ -173,6 +169,64 @@ of Django's `HTTPRequest` in your resolve methods, such as checking for authenti
173169
return Question.objects.none()
174170
175171
172+
Plain ObjectTypes
173+
-----------------
174+
175+
With Graphene-Django you are not limited to just Django Models - you can use the standard
176+
``ObjectType`` to create custom fields or to provide an abstraction between your internal
177+
Django models and your external API.
178+
179+
.. code:: python
180+
181+
import graphene
182+
from .models import Question
183+
184+
185+
class MyQuestion(graphene.ObjectType):
186+
text = graphene.String()
187+
188+
189+
class Query:
190+
question = graphene.Field(MyQuestion, question_id=graphene.String())
191+
192+
def resolve_question(self, info, question_id):
193+
question = Question.objects.get(pk=question_id)
194+
return MyQuestion(
195+
text=question.question_text
196+
)
197+
176198
For more information and more examples, please see the `core object type documentation <https://docs.graphene-python.org/en/latest/types/objecttypes/>`__.
177199

178-
For additional features such as automatic filtering and pagination, please see the Relay tutorial.
200+
201+
Relay
202+
-----
203+
204+
`Relay <http://docs.graphene-python.org/en/latest/relay/>`__ with Graphene-Django gives us some additional features:
205+
206+
- Pagination and slicing.
207+
- An abstract ``id`` value which contains enough info for the server to know its type and its id.
208+
209+
Full example
210+
~~~~~~~~~~~~
211+
212+
.. code:: python
213+
214+
from graphene import relay
215+
from graphene_django import DjangoObjectType
216+
from .models import Question
217+
218+
219+
class QuestionType(DjangoObjectType):
220+
class Meta:
221+
model = Question
222+
interaces = (relay.Node,)
223+
224+
225+
class QuestionConnection(relay.Connection):
226+
class Meta:
227+
node = QuestionType
228+
229+
230+
class Query:
231+
question = graphene.Field(QuestionType)
232+
questions = relay.ConnectionField(QuestionConnection)

docs/schema.rst

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The ``graphene.Schema`` object describes your data model, and provides a GraphQL
1616
schema = graphene.Schema(query=Query, mutation=Mutation)
1717
1818
19-
This schema on it's own does nothing, but it is ready to accept new Query or Mutation fields that you create.
19+
This schema does nothing like this, but it is ready to accept new Query or Mutation fields that you create.
2020

2121

2222
Adding to the schema
@@ -31,10 +31,16 @@ If you have defined a ``Query`` or ``Mutation`` you can register them with the s
3131
import my_app.schema.Query
3232
import my_app.schema.Mutation
3333
34-
class Query(my_app.schema.Query, graphene.ObjectType):
34+
class Query(
35+
my_app.schema.Query, # Add your Query objects here
36+
graphene.ObjectType
37+
):
3538
pass
3639
37-
class Mutation(my_app.schema.Mutation, graphene.ObjectType):
40+
class Mutation(
41+
my_app.schema.Mutation, # Add your Mutation objects here
42+
graphene.ObjectType
43+
):
3844
pass
3945
4046
schema = graphene.Schema(query=Query, mutation=Mutation)

0 commit comments

Comments
 (0)