mirror of
https://github.com/graphql-python/graphene.git
synced 2025-02-02 12:44:15 +03:00
Improved docs
This commit is contained in:
parent
aed366a3c7
commit
944d361fd7
|
@ -28,7 +28,6 @@ pip install django-graphiql
|
||||||
|
|
||||||
# Set up a new project with a single application
|
# Set up a new project with a single application
|
||||||
django-admin.py startproject tutorial . # Note the trailing '.' character
|
django-admin.py startproject tutorial . # Note the trailing '.' character
|
||||||
cd tutorial
|
|
||||||
django-admin.py startapp quickstart
|
django-admin.py startapp quickstart
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -50,7 +49,7 @@ Once you've set up a database and initial user created and ready to go, open up
|
||||||
|
|
||||||
## Schema
|
## Schema
|
||||||
|
|
||||||
Right, we'd better write some types then. Open `tutorial/quickstart/schema.py` and get typing.
|
Right, we'd better write some types then. Open `quickstart/schema.py` and get typing.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
import graphene
|
import graphene
|
||||||
|
@ -58,6 +57,7 @@ from graphene.contrib.django import DjangoObjectType
|
||||||
|
|
||||||
from django.contrib.auth.models import User, Group
|
from django.contrib.auth.models import User, Group
|
||||||
|
|
||||||
|
|
||||||
# Graphene will map automatically the User model to UserType with
|
# Graphene will map automatically the User model to UserType with
|
||||||
# the specified fields
|
# the specified fields
|
||||||
class UserType(DjangoObjectType):
|
class UserType(DjangoObjectType):
|
||||||
|
@ -68,14 +68,16 @@ class UserType(DjangoObjectType):
|
||||||
|
|
||||||
class GroupType(DjangoObjectType):
|
class GroupType(DjangoObjectType):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = User
|
model = Group
|
||||||
only_fields = ('name', )
|
only_fields = ('name', )
|
||||||
|
|
||||||
|
|
||||||
class Query(graphene.ObjectType):
|
class Query(graphene.ObjectType):
|
||||||
all_users = graphene.List(UserType)
|
all_users = graphene.List(UserType)
|
||||||
get_user = graphene.Field(UserType
|
get_user = graphene.Field(UserType,
|
||||||
id=graphene.String(required=True))
|
id=graphene.String().NonNull)
|
||||||
|
get_group = graphene.Field(GroupType,
|
||||||
|
id=graphene.String().NonNull)
|
||||||
|
|
||||||
def resolve_all_users(self, args, info):
|
def resolve_all_users(self, args, info):
|
||||||
return User.objects.all()
|
return User.objects.all()
|
||||||
|
@ -83,10 +85,24 @@ class Query(graphene.ObjectType):
|
||||||
def resolve_get_user(self, args, info):
|
def resolve_get_user(self, args, info):
|
||||||
return User.objects.get(id=args.get('id'))
|
return User.objects.get(id=args.get('id'))
|
||||||
|
|
||||||
|
def resolve_get_group(self, args, info):
|
||||||
|
return Group.objects.get(id=args.get('id'))
|
||||||
|
|
||||||
schema = graphene.Schema(query=Query)
|
schema = graphene.Schema(query=Query)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
## Adding GraphiQL
|
||||||
|
|
||||||
|
For having the GraphiQL static assets we need to append `django_graphiql` in `INSTALLED_APPS` in `tutorial/settings.py`:
|
||||||
|
|
||||||
|
```python
|
||||||
|
INSTALLED_APPS = [
|
||||||
|
# The other installed apps
|
||||||
|
'django_graphiql',
|
||||||
|
]
|
||||||
|
```
|
||||||
|
|
||||||
## Creating GraphQL and GraphiQL views
|
## Creating GraphQL and GraphiQL views
|
||||||
|
|
||||||
Okay, now let's wire up the GraphQL and GraphiQL urls. On to `tutorial/urls.py`...
|
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`.
|
||||||
from django.conf.urls import url, include
|
from django.conf.urls import url, include
|
||||||
from django.views.decorators.csrf import csrf_exempt
|
from django.views.decorators.csrf import csrf_exempt
|
||||||
from graphene.contrib.django.views import GraphQLView
|
from graphene.contrib.django.views import GraphQLView
|
||||||
from tutorial.quickstart.schema import schema
|
from quickstart.schema import schema
|
||||||
|
|
||||||
|
|
||||||
# Wire up our GraphQL schema in /graphql.
|
# Wire up our GraphQL schema in /graphql.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user