@@ -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
@@ -50,16 +50,25 @@ Once you've set up a database and initial user created and ready to go, open up
50
50
51
51
## Schema
52
52
53
- Right, we'd better write some types then. Open ` tutorial/quickstart/schema.py ` and get typing.
53
+ GraphQL presents your objects to the world as a graph structure rather than a more
54
+ heiricarcal structure to which you may be acustomed. In order to create this
55
+ representation, Graphene needs to know about each * type* of object which will appear in
56
+ the graph. Below we define these as the ` UserType ` and ` GroupType ` classes.
57
+
58
+ This graph also has a 'root' through which all access begins. This is the ` Query ` class below.
59
+ In this example, we provide the ability to list all users via ` all_users ` , and the
60
+ ability to obtain a single user via ` get_user ` .
61
+
62
+ Open ` tutorial/quickstart/schema.py ` and type the following:
54
63
55
64
``` python
56
65
import graphene
57
66
from graphene.contrib.django import DjangoObjectType
58
67
59
68
from django.contrib.auth.models import User, Group
60
69
61
- # Graphene will map automatically the User model to UserType with
62
- # the specified fields
70
+ # Graphene will automatically map the User model's fields onto the UserType.
71
+ # This is configured in the UserType's Meta class
63
72
class UserType (DjangoObjectType ):
64
73
class Meta :
65
74
model = User
@@ -68,13 +77,13 @@ class UserType(DjangoObjectType):
68
77
69
78
class GroupType (DjangoObjectType ):
70
79
class Meta :
71
- model = User
80
+ model = Group
72
81
only_fields = (' name' , )
73
82
74
83
75
84
class Query (graphene .ObjectType ):
76
85
all_users = graphene.List(UserType)
77
- get_user = graphene.Field(UserType
86
+ get_user = graphene.Field(UserType,
78
87
id = graphene.String(required = True ))
79
88
80
89
def resolve_all_users (self , args , info ):
@@ -89,8 +98,12 @@ schema = graphene.Schema(query=Query)
89
98
90
99
## Creating GraphQL and GraphiQL views
91
100
92
- Okay, now let's wire up the GraphQL and GraphiQL urls. On to ` tutorial/urls.py ` ...
101
+ Unlike a RESTful API, there is only a single URL from which a GraphQL is accessed.
102
+ Requests to this URL are handled by Graphene's ` GraphQLView ` view.
93
103
104
+ Additionally, and interface for navigating this API will be very useful. Graphene
105
+ includes the [ graphiql] ( https://github.com/graphql/graphiql ) in-browser IDE
106
+ which assits and exploring and querying your new API. We'll add a URL for this too.
94
107
95
108
``` python
96
109
from django.conf.urls import url, include
@@ -99,7 +112,7 @@ from graphene.contrib.django.views import GraphQLView
99
112
from tutorial.quickstart.schema import schema
100
113
101
114
102
- # Wire up our GraphQL schema in /graphql.
115
+ # Wire up our GraphQL schema to /graphql.
103
116
# Additionally, we include GraphiQL view for querying easily our schema.
104
117
urlpatterns = [
105
118
url(r ' ^ graphql' , csrf_exempt(GraphQLView.as_view(schema = schema))),
0 commit comments