@@ -28,7 +28,6 @@ pip install django-graphiql
28
28
29
29
# Set up a new project with a single application
30
30
django-admin.py startproject tutorial . # Note the trailing '.' character
31
- cd tutorial
32
31
django-admin.py startapp quickstart
33
32
```
34
33
@@ -50,14 +49,15 @@ Once you've set up a database and initial user created and ready to go, open up
50
49
51
50
## Schema
52
51
53
- Right, we'd better write some types then. Open ` tutorial/ quickstart/schema.py` and get typing.
52
+ Right, we'd better write some types then. Open ` quickstart/schema.py ` and get typing.
54
53
55
54
``` python
56
55
import graphene
57
56
from graphene.contrib.django import DjangoObjectType
58
57
59
58
from django.contrib.auth.models import User, Group
60
59
60
+
61
61
# Graphene will map automatically the User model to UserType with
62
62
# the specified fields
63
63
class UserType (DjangoObjectType ):
@@ -68,25 +68,41 @@ class UserType(DjangoObjectType):
68
68
69
69
class GroupType (DjangoObjectType ):
70
70
class Meta :
71
- model = User
71
+ model = Group
72
72
only_fields = (' name' , )
73
73
74
74
75
75
class Query (graphene .ObjectType ):
76
76
all_users = graphene.List(UserType)
77
- get_user = graphene.Field(UserType
78
- id = graphene.String(required = True ))
77
+ get_user = graphene.Field(UserType,
78
+ id = graphene.String().NonNull)
79
+ get_group = graphene.Field(GroupType,
80
+ id = graphene.String().NonNull)
79
81
80
82
def resolve_all_users (self , args , info ):
81
83
return User.objects.all()
82
84
83
85
def resolve_get_user (self , args , info ):
84
86
return User.objects.get(id = args.get(' id' ))
85
87
88
+ def resolve_get_group (self , args , info ):
89
+ return Group.objects.get(id = args.get(' id' ))
90
+
86
91
schema = graphene.Schema(query = Query)
87
92
```
88
93
89
94
95
+ ## Adding GraphiQL
96
+
97
+ For having the GraphiQL static assets we need to append ` django_graphiql ` in ` INSTALLED_APPS ` in ` tutorial/settings.py ` :
98
+
99
+ ``` python
100
+ INSTALLED_APPS = [
101
+ # The other installed apps
102
+ ' django_graphiql' ,
103
+ ]
104
+ ```
105
+
90
106
## Creating GraphQL and GraphiQL views
91
107
92
108
Okay, now let's wire up the GraphQL and GraphiQL urls. On to ` tutorial/urls.py ` ...
@@ -96,7 +112,7 @@ Okay, now let's wire up the GraphQL and GraphiQL urls. On to `tutorial/urls.py`.
96
112
from django.conf.urls import url, include
97
113
from django.views.decorators.csrf import csrf_exempt
98
114
from graphene.contrib.django.views import GraphQLView
99
- from tutorial. quickstart.schema import schema
115
+ from quickstart.schema import schema
100
116
101
117
102
118
# Wire up our GraphQL schema in /graphql.
0 commit comments