mirror of
https://github.com/graphql-python/graphene.git
synced 2025-02-02 12:44:15 +03:00
Improved Django documentation
This commit is contained in:
parent
33ff3c5299
commit
bbf1392ffd
|
@ -5,7 +5,6 @@ ga = "UA-12613282-7"
|
|||
name = "Quickstart"
|
||||
pages = [
|
||||
"/docs/quickstart/",
|
||||
"/docs/quickstart-django/",
|
||||
]
|
||||
|
||||
[docs.walkthrough]
|
||||
|
@ -16,5 +15,11 @@ ga = "UA-12613282-7"
|
|||
"/docs/mutations/",
|
||||
"/docs/basic-types/",
|
||||
"/docs/relay/",
|
||||
"/docs/filtering/",
|
||||
]
|
||||
|
||||
[docs.django]
|
||||
name = "Django"
|
||||
pages = [
|
||||
"/docs/django/tutorial/",
|
||||
"/docs/django/filtering/",
|
||||
]
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
---
|
||||
title: Filtering (Django)
|
||||
description: Details of how to perform filtering
|
||||
title: Filtering
|
||||
description: Details of how to perform filtering in Graphene Django
|
||||
---
|
||||
|
||||
# Filtering (Django)
|
||||
# Filtering
|
||||
|
||||
Graphene integrates with [django-filter](https://django-filter.readthedocs.org)
|
||||
to provide filtering of results. See the
|
||||
|
@ -21,7 +21,7 @@ pip install django-filter
|
|||
```
|
||||
|
||||
**Note: The techniques below are demoed in the
|
||||
[cookbook example app](https://github.com/graphql-python/graphene/tree/feature/django/examples/cookbook).**
|
||||
[cookbook example app](https://github.com/graphql-python/graphene/tree/master/examples/cookbook_django).**
|
||||
|
||||
## Filterable fields
|
||||
|
||||
|
@ -54,7 +54,10 @@ query {
|
|||
node {
|
||||
id,
|
||||
name
|
||||
}}}}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
You can also make more complex lookup types available:
|
||||
|
@ -76,12 +79,15 @@ Which you could query as follows:
|
|||
```graphql
|
||||
query {
|
||||
# Note that fields names become camelcased
|
||||
allAnimals(nameIcontains: "lion") {
|
||||
allAnimals(name_Icontains: "lion") {
|
||||
edges {
|
||||
node {
|
||||
id,
|
||||
name
|
||||
}}}}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Orderable fields
|
||||
|
@ -112,7 +118,10 @@ query {
|
|||
node {
|
||||
id,
|
||||
name
|
||||
}}}}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Custom Filtersets
|
|
@ -1,28 +1,54 @@
|
|||
---
|
||||
title: Django Quickstart
|
||||
title: Quickstart
|
||||
description: A Quick guide to Graphene in Django
|
||||
---
|
||||
|
||||
# Django Tutorial
|
||||
|
||||
Graphene has a number of additional features that are designed to make
|
||||
working with Django simple.
|
||||
|
||||
If you need help getting started with django then head over to
|
||||
Django's getting started page.
|
||||
|
||||
First let's create a few simple models...
|
||||
working with Django *really simple*.
|
||||
|
||||
**Note: The code in this quickstart is pulled from the
|
||||
[cookbook example app](https://github.com/graphql-python/graphene/tree/feature/django/examples/cookbook)**.
|
||||
[cookbook example app](https://github.com/graphql-python/graphene/tree/master/examples/cookbook_django)**.
|
||||
|
||||
## Defining our models
|
||||
|
||||
Before continuing, create the following:
|
||||
## Setup the Django project
|
||||
|
||||
We will setup the project, create the following:
|
||||
|
||||
* A Django project called `cookbook`
|
||||
* An app within `cookbook` called `ingredients`
|
||||
|
||||
```bash
|
||||
# Create the project directory
|
||||
mkdir cookbook
|
||||
cd cookbook
|
||||
|
||||
# Create a virtualenv to isolate our package dependencies locally
|
||||
virtualenv env
|
||||
source env/bin/activate # On Windows use `env\Scripts\activate`
|
||||
|
||||
# Install Django and Graphene with Django support
|
||||
pip install django
|
||||
pip install graphene[django]
|
||||
pip install django-graphiql
|
||||
|
||||
# Set up a new project with a single application
|
||||
django-admin.py startproject cookbook . # Note the trailing '.' character
|
||||
django-admin.py startapp ingredients
|
||||
```
|
||||
|
||||
Now sync your database for the first time:
|
||||
|
||||
```bash
|
||||
python manage.py migrate
|
||||
```
|
||||
|
||||
Let's create a few simple models...
|
||||
|
||||
|
||||
## Defining our models
|
||||
|
||||
Let's get started with these models:
|
||||
|
||||
```python
|
||||
|
@ -51,9 +77,9 @@ class Ingredient(models.Model):
|
|||
GraphQL presents your objects to the world as a graph structure rather than a more
|
||||
hierarchical structure to which you may be accustomed. In order to create this
|
||||
representation, Graphene needs to know about each *type* of object which will appear in
|
||||
the graph. Below we define these as the `UserType` and `GroupType` classes.
|
||||
the graph.
|
||||
|
||||
This graph also has a 'root' through which all access begins. This is the `Query` class below.
|
||||
This graph also has a *root type* through which all access begins. This is the `Query` class below.
|
||||
In this example, we provide the ability to list all users via `all_users`, and the
|
||||
ability to obtain a specific user via `get_user`.
|
||||
|
||||
|
@ -183,7 +209,7 @@ Installed 6 object(s) from 1 fixture(s)
|
|||
```
|
||||
|
||||
Alternatively you can use the Django admin interface to create some data youself.
|
||||
You'll need to run the development server (see below), and probably create a login
|
||||
You'll need to run the development server (see below), and create a login
|
||||
for yourself too (`./manage.py createsuperuser`).
|
||||
|
||||
## Testing our GraphQL schema
|
||||
|
@ -235,12 +261,17 @@ query {
|
|||
edges {
|
||||
node {
|
||||
name,
|
||||
|
||||
ingredients {
|
||||
edges {
|
||||
node {
|
||||
name
|
||||
}}}}}}}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Or you can get only 'meat' ingredients containing the letter 'e':
|
||||
|
@ -248,9 +279,12 @@ Or you can get only 'meat' ingredients containing the letter 'e':
|
|||
```graphql
|
||||
query {
|
||||
# You can also use `category: "CATEGORY GLOBAL ID"`
|
||||
allIngredients(nameIcontains: "e", categoryName: "Meat") {
|
||||
allIngredients(name_Icontains: "e", categoryName: "Meat") {
|
||||
edges {
|
||||
node {
|
||||
name
|
||||
}}}}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
Loading…
Reference in New Issue
Block a user