@@ -7,7 +7,7 @@ description: A Quick guide to Graphene in Django
7
7
8
8
In our previous quickstart page we created a very simple schema.
9
9
10
- Now we will adapt the schema to map automatically some Django models,
10
+ Now we will adapt the schema to automatically map some Django models,
11
11
and expose this schema in a ` /graphql ` API endpoint.
12
12
13
13
## Project setup
@@ -49,17 +49,25 @@ Once you've set up a database and initial user created and ready to go, open up
49
49
50
50
## Schema
51
51
52
- Right, we'd better write some types then. Open ` quickstart/schema.py ` and get typing.
52
+ GraphQL presents your objects to the world as a graph structure rather than a more
53
+ heiricarcal structure to which you may be acustomed. In order to create this
54
+ representation, Graphene needs to know about each * type* of object which will appear in
55
+ the graph. Below we define these as the ` UserType ` and ` GroupType ` classes.
56
+
57
+ This graph also has a 'root' through which all access begins. This is the ` Query ` class below.
58
+ In this example, we provide the ability to list all users via ` all_users ` , and the
59
+ ability to obtain a single user via ` get_user ` .
60
+
61
+ Open ` tutorial/quickstart/schema.py ` and type the following:
53
62
54
63
``` python
55
64
import graphene
56
65
from graphene.contrib.django import DjangoObjectType
57
66
58
67
from django.contrib.auth.models import User, Group
59
68
60
-
61
- # Graphene will map automatically the User model to UserType with
62
- # the specified fields
69
+ # Graphene will automatically map the User model's fields onto the UserType.
70
+ # This is configured in the UserType's Meta class
63
71
class UserType (DjangoObjectType ):
64
72
class Meta :
65
73
model = User
@@ -105,8 +113,12 @@ INSTALLED_APPS = [
105
113
106
114
## Creating GraphQL and GraphiQL views
107
115
108
- Okay, now let's wire up the GraphQL and GraphiQL urls. On to ` tutorial/urls.py ` ...
116
+ Unlike a RESTful API, there is only a single URL from which a GraphQL is accessed.
117
+ Requests to this URL are handled by Graphene's ` GraphQLView ` view.
109
118
119
+ Additionally, and interface for navigating this API will be very useful. Graphene
120
+ includes the [ graphiql] ( https://github.com/graphql/graphiql ) in-browser IDE
121
+ which assits and exploring and querying your new API. We'll add a URL for this too.
110
122
111
123
``` python
112
124
from django.conf.urls import url, include
@@ -115,7 +127,7 @@ from graphene.contrib.django.views import GraphQLView
115
127
from quickstart.schema import schema
116
128
117
129
118
- # Wire up our GraphQL schema in /graphql.
130
+ # Wire up our GraphQL schema to /graphql.
119
131
# Additionally, we include GraphiQL view for querying easily our schema.
120
132
urlpatterns = [
121
133
url(r ' ^ graphql' , csrf_exempt(GraphQLView.as_view(schema = schema))),
0 commit comments