1
- Graphene- Django Tutorial using Relay
2
- ====================================
1
+ Graphene and Django Tutorial using Relay
2
+ ========================================
3
3
4
4
Graphene has a number of additional features that are designed to make
5
5
working with Django *really simple *.
6
6
7
7
Note: The code in this quickstart is pulled from the `cookbook example
8
8
app <https://github.com/graphql-python/graphene-django/tree/master/examples/cookbook> `__.
9
9
10
+ A good idea is to check the following things first:
11
+
12
+ * `Graphene Relay documentation <http://docs.graphene-python.org/en/latest/relay/ >`__
13
+ * `GraphQL Relay Specification <https://facebook.github.io/relay/docs/graphql-relay-specification.html >`__
14
+
10
15
Setup the Django project
11
16
------------------------
12
17
@@ -43,7 +48,7 @@ Now sync your database for the first time:
43
48
Let's create a few simple models...
44
49
45
50
Defining our models
46
- -------------------
51
+ ^^^^^^^^^^^^^^^^^^^
47
52
48
53
Let's get started with these models:
49
54
@@ -68,6 +73,33 @@ Let's get started with these models:
68
73
def __str__ (self ):
69
74
return self .name
70
75
76
+ Don't forget to create & run migrations:
77
+
78
+ .. code :: bash
79
+
80
+ python manage.py makemigrations
81
+ python manage.py migrate
82
+
83
+ Load some test data
84
+ ^^^^^^^^^^^^^^^^^^^
85
+
86
+ Now is a good time to load up some test data. The easiest option will be
87
+ to `download the
88
+ ingredients.json <https://raw.githubusercontent.com/graphql-python/graphene-django/master/examples/cookbook/cookbook/ingredients/fixtures/ingredients.json> `__
89
+ fixture and place it in
90
+ ``cookbook/ingredients/fixtures/ingredients.json ``. You can then run the
91
+ following:
92
+
93
+ .. code :: bash
94
+
95
+ $ python ./manage.py loaddata ingredients
96
+
97
+ Installed 6 object(s) from 1 fixture(s)
98
+
99
+ Alternatively you can use the Django admin interface to create some data
100
+ yourself. You'll need to run the development server (see below), and
101
+ create a login for yourself too (``./manage.py createsuperuser ``).
102
+
71
103
Schema
72
104
------
73
105
@@ -158,8 +190,11 @@ Create the parent project-level ``cookbook/schema.py``:
158
190
You can think of this as being something like your top-level ``urls.py ``
159
191
file (although it currently lacks any namespacing).
160
192
193
+ Testing everything so far
194
+ -------------------------
195
+
161
196
Update settings
162
- ---------------
197
+ ^^^^^^^^^^^^^^^
163
198
164
199
Next, install your app and GraphiQL in your Django project. GraphiQL is
165
200
a web-based integrated development environment to assist in the writing
@@ -191,7 +226,7 @@ Alternatively, we can specify the schema to be used in the urls definition,
191
226
as explained below.
192
227
193
228
Creating GraphQL and GraphiQL views
194
- -----------------------------------
229
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
195
230
196
231
Unlike a RESTful API, there is only a single URL from which GraphQL is
197
232
accessed. Requests to this URL are handled by Graphene's ``GraphQLView ``
@@ -230,39 +265,9 @@ as explained above, we can do so here using:
230
265
url(r ' ^ graphql' , GraphQLView.as_view(graphiql = True , schema = schema)),
231
266
]
232
267
233
- Apply model changes to database
234
- -------------------------------
235
-
236
- Tell Django that we've added models and update the database schema to
237
- reflect these additions.
238
-
239
- .. code :: bash
240
-
241
- python manage.py makemigrations
242
- python manage.py migrate
243
-
244
- Load some test data
245
- -------------------
246
-
247
- Now is a good time to load up some test data. The easiest option will be
248
- to `download the
249
- ingredients.json <https://raw.githubusercontent.com/graphql-python/graphene-django/master/examples/cookbook/cookbook/ingredients/fixtures/ingredients.json> `__
250
- fixture and place it in
251
- ``cookbook/ingredients/fixtures/ingredients.json ``. You can then run the
252
- following:
253
-
254
- .. code :: bash
255
-
256
- $ python ./manage.py loaddata ingredients
257
-
258
- Installed 6 object(s) from 1 fixture(s)
259
-
260
- Alternatively you can use the Django admin interface to create some data
261
- yourself. You'll need to run the development server (see below), and
262
- create a login for yourself too (``./manage.py createsuperuser ``).
263
268
264
269
Testing our GraphQL schema
265
- --------------------------
270
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^
266
271
267
272
We're now ready to test the API we've built. Let's fire up the server
268
273
from the command line.
0 commit comments