Skip to content

Commit abc4c91

Browse files
committed
Merge pull request #54 from adamcharnock/docs-adam
Expanding on the django quickstart to make it a little more verbose
2 parents aed366a + 93b8a59 commit abc4c91

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

docs/pages/docs/quickstart-django.md

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ description: A Quick guide to Graphene in Django
77

88
In our previous quickstart page we created a very simple schema.
99

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,
1111
and expose this schema in a `/graphql` API endpoint.
1212

1313
## Project setup
@@ -50,16 +50,25 @@ Once you've set up a database and initial user created and ready to go, open up
5050

5151
## Schema
5252

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:
5463

5564
```python
5665
import graphene
5766
from graphene.contrib.django import DjangoObjectType
5867

5968
from django.contrib.auth.models import User, Group
6069

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
6372
class UserType(DjangoObjectType):
6473
class Meta:
6574
model = User
@@ -68,13 +77,13 @@ class UserType(DjangoObjectType):
6877

6978
class GroupType(DjangoObjectType):
7079
class Meta:
71-
model = User
80+
model = Group
7281
only_fields = ('name', )
7382

7483

7584
class Query(graphene.ObjectType):
7685
all_users = graphene.List(UserType)
77-
get_user = graphene.Field(UserType
86+
get_user = graphene.Field(UserType,
7887
id=graphene.String(required=True))
7988

8089
def resolve_all_users(self, args, info):
@@ -89,8 +98,12 @@ schema = graphene.Schema(query=Query)
8998

9099
## Creating GraphQL and GraphiQL views
91100

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.
93103

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.
94107

95108
```python
96109
from django.conf.urls import url, include
@@ -99,7 +112,7 @@ from graphene.contrib.django.views import GraphQLView
99112
from tutorial.quickstart.schema import schema
100113

101114

102-
# Wire up our GraphQL schema in /graphql.
115+
# Wire up our GraphQL schema to /graphql.
103116
# Additionally, we include GraphiQL view for querying easily our schema.
104117
urlpatterns = [
105118
url(r'^graphql', csrf_exempt(GraphQLView.as_view(schema=schema))),

0 commit comments

Comments
 (0)