mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-25 19:13:57 +03:00
Adding cookbook example app demoing new django functionality
This commit is contained in:
parent
216c4350a1
commit
c70b5f2f0d
5
.gitignore
vendored
5
.gitignore
vendored
|
@ -74,4 +74,7 @@ target/
|
||||||
/docs/static/playground
|
/docs/static/playground
|
||||||
|
|
||||||
# PyCharm
|
# PyCharm
|
||||||
/.idea
|
.idea
|
||||||
|
|
||||||
|
# Databases
|
||||||
|
*.sqlite3
|
||||||
|
|
0
examples/cookbook/cookbook/__init__.py
Normal file
0
examples/cookbook/cookbook/__init__.py
Normal file
0
examples/cookbook/cookbook/ingredients/__init__.py
Normal file
0
examples/cookbook/cookbook/ingredients/__init__.py
Normal file
6
examples/cookbook/cookbook/ingredients/admin.py
Normal file
6
examples/cookbook/cookbook/ingredients/admin.py
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
from django.contrib import admin
|
||||||
|
|
||||||
|
from cookbook.ingredients.models import Ingredient, Category
|
||||||
|
|
||||||
|
admin.site.register(Ingredient)
|
||||||
|
admin.site.register(Category)
|
7
examples/cookbook/cookbook/ingredients/apps.py
Normal file
7
examples/cookbook/cookbook/ingredients/apps.py
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
from django.apps import AppConfig
|
||||||
|
|
||||||
|
|
||||||
|
class IngredientsConfig(AppConfig):
|
||||||
|
name = 'cookbook.ingredients'
|
||||||
|
label = 'ingredients'
|
||||||
|
verbose_name = 'Ingredients'
|
|
@ -0,0 +1 @@
|
||||||
|
[{"model": "ingredients.category", "pk": 1, "fields": {"name": "Dairy"}}, {"model": "ingredients.category", "pk": 2, "fields": {"name": "Meat"}}, {"model": "ingredients.ingredient", "pk": 1, "fields": {"name": "Eggs", "notes": "Good old eggs", "category": 1}}, {"model": "ingredients.ingredient", "pk": 2, "fields": {"name": "Milk", "notes": "Comes from a cow", "category": 1}}, {"model": "ingredients.ingredient", "pk": 3, "fields": {"name": "Beef", "notes": "Much like milk, this comes from a cow", "category": 2}}, {"model": "ingredients.ingredient", "pk": 4, "fields": {"name": "Chicken", "notes": "Definitely doesn't come from a cow", "category": 2}}]
|
|
@ -0,0 +1,33 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# Generated by Django 1.9 on 2015-12-04 18:15
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
import django.db.models.deletion
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
initial = True
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='Category',
|
||||||
|
fields=[
|
||||||
|
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('name', models.CharField(max_length=100)),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='Ingredient',
|
||||||
|
fields=[
|
||||||
|
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('name', models.CharField(max_length=100)),
|
||||||
|
('notes', models.TextField()),
|
||||||
|
('category', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='ingredients', to='ingredients.Category')),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
17
examples/cookbook/cookbook/ingredients/models.py
Normal file
17
examples/cookbook/cookbook/ingredients/models.py
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
from django.db import models
|
||||||
|
|
||||||
|
|
||||||
|
class Category(models.Model):
|
||||||
|
name = models.CharField(max_length=100)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.name
|
||||||
|
|
||||||
|
|
||||||
|
class Ingredient(models.Model):
|
||||||
|
name = models.CharField(max_length=100)
|
||||||
|
notes = models.TextField()
|
||||||
|
category = models.ForeignKey(Category, related_name='ingredients')
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.name
|
38
examples/cookbook/cookbook/ingredients/schema.py
Normal file
38
examples/cookbook/cookbook/ingredients/schema.py
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
from graphene import relay, ObjectType
|
||||||
|
from graphene.contrib.django.filter import DjangoFilterConnectionField
|
||||||
|
from graphene.contrib.django.types import DjangoNode
|
||||||
|
|
||||||
|
from cookbook.ingredients.models import Category, Ingredient
|
||||||
|
|
||||||
|
|
||||||
|
# Graphene will automatically map the User model's fields onto the UserType.
|
||||||
|
# This is configured in the UserType's Meta class (as you can see below)
|
||||||
|
class CategoryNode(DjangoNode):
|
||||||
|
class Meta:
|
||||||
|
model = Category
|
||||||
|
filter_fields = ['name', 'ingredients']
|
||||||
|
filter_order_by = ['name']
|
||||||
|
|
||||||
|
|
||||||
|
class IngredientNode(DjangoNode):
|
||||||
|
class Meta:
|
||||||
|
model = Ingredient
|
||||||
|
# Allow for some more advanced filtering here
|
||||||
|
filter_fields = {
|
||||||
|
'name': ['exact', 'icontains', 'istartswith'],
|
||||||
|
'notes': ['exact', 'icontains'],
|
||||||
|
'category': ['exact'],
|
||||||
|
'category__name': ['exact'],
|
||||||
|
}
|
||||||
|
filter_order_by = ['name', 'category__name']
|
||||||
|
|
||||||
|
|
||||||
|
class Query(ObjectType):
|
||||||
|
category = relay.NodeField(CategoryNode)
|
||||||
|
all_categories = DjangoFilterConnectionField(CategoryNode)
|
||||||
|
|
||||||
|
ingredient = relay.NodeField(IngredientNode)
|
||||||
|
all_ingredients = DjangoFilterConnectionField(IngredientNode)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
abstract = True
|
3
examples/cookbook/cookbook/ingredients/tests.py
Normal file
3
examples/cookbook/cookbook/ingredients/tests.py
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
from django.test import TestCase
|
||||||
|
|
||||||
|
# Create your tests here.
|
3
examples/cookbook/cookbook/ingredients/views.py
Normal file
3
examples/cookbook/cookbook/ingredients/views.py
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
from django.shortcuts import render
|
||||||
|
|
||||||
|
# Create your views here.
|
0
examples/cookbook/cookbook/recipes/__init__.py
Normal file
0
examples/cookbook/cookbook/recipes/__init__.py
Normal file
6
examples/cookbook/cookbook/recipes/admin.py
Normal file
6
examples/cookbook/cookbook/recipes/admin.py
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
from django.contrib import admin
|
||||||
|
|
||||||
|
from cookbook.recipes.models import Recipe, RecipeIngredient
|
||||||
|
|
||||||
|
admin.site.register(Recipe)
|
||||||
|
admin.site.register(RecipeIngredient)
|
7
examples/cookbook/cookbook/recipes/apps.py
Normal file
7
examples/cookbook/cookbook/recipes/apps.py
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
from django.apps import AppConfig
|
||||||
|
|
||||||
|
|
||||||
|
class RecipesConfig(AppConfig):
|
||||||
|
name = 'cookbook.recipes'
|
||||||
|
label = 'recipes'
|
||||||
|
verbose_name = 'Recipes'
|
|
@ -0,0 +1,36 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# Generated by Django 1.9 on 2015-12-04 18:20
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
import django.db.models.deletion
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
initial = True
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('ingredients', '0001_initial'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='Recipe',
|
||||||
|
fields=[
|
||||||
|
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('title', models.CharField(max_length=100)),
|
||||||
|
('instructions', models.TextField()),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='RecipeIngredient',
|
||||||
|
fields=[
|
||||||
|
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('amount', models.FloatField()),
|
||||||
|
('unit', models.CharField(choices=[('kg', 'Kilograms'), ('l', 'Litres'), ('', 'Units')], max_length=20)),
|
||||||
|
('ingredient', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='used_by', to='ingredients.Ingredient')),
|
||||||
|
('recipes', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='amounts', to='recipes.Recipe')),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
19
examples/cookbook/cookbook/recipes/models.py
Normal file
19
examples/cookbook/cookbook/recipes/models.py
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
from django.db import models
|
||||||
|
|
||||||
|
from cookbook.ingredients.models import Ingredient
|
||||||
|
|
||||||
|
|
||||||
|
class Recipe(models.Model):
|
||||||
|
title = models.CharField(max_length=100)
|
||||||
|
instructions = models.TextField()
|
||||||
|
|
||||||
|
|
||||||
|
class RecipeIngredient(models.Model):
|
||||||
|
recipes = models.ForeignKey(Recipe, related_name='amounts')
|
||||||
|
ingredient = models.ForeignKey(Ingredient, related_name='used_by')
|
||||||
|
amount = models.FloatField()
|
||||||
|
unit = models.CharField(max_length=20, choices=(
|
||||||
|
('kg', 'Kilograms'),
|
||||||
|
('l', 'Litres'),
|
||||||
|
('', 'Units'),
|
||||||
|
))
|
3
examples/cookbook/cookbook/recipes/tests.py
Normal file
3
examples/cookbook/cookbook/recipes/tests.py
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
from django.test import TestCase
|
||||||
|
|
||||||
|
# Create your tests here.
|
3
examples/cookbook/cookbook/recipes/views.py
Normal file
3
examples/cookbook/cookbook/recipes/views.py
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
from django.shortcuts import render
|
||||||
|
|
||||||
|
# Create your views here.
|
10
examples/cookbook/cookbook/schema.py
Normal file
10
examples/cookbook/cookbook/schema.py
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
import graphene
|
||||||
|
|
||||||
|
import cookbook.ingredients.schema
|
||||||
|
|
||||||
|
|
||||||
|
class Query(cookbook.ingredients.schema.Query):
|
||||||
|
pass
|
||||||
|
|
||||||
|
schema = graphene.Schema(name='Cookbook Schema')
|
||||||
|
schema.query = Query
|
125
examples/cookbook/cookbook/settings.py
Normal file
125
examples/cookbook/cookbook/settings.py
Normal file
|
@ -0,0 +1,125 @@
|
||||||
|
"""
|
||||||
|
Django settings for cookbook project.
|
||||||
|
|
||||||
|
Generated by 'django-admin startproject' using Django 1.9.
|
||||||
|
|
||||||
|
For more information on this file, see
|
||||||
|
https://docs.djangoproject.com/en/1.9/topics/settings/
|
||||||
|
|
||||||
|
For the full list of settings and their values, see
|
||||||
|
https://docs.djangoproject.com/en/1.9/ref/settings/
|
||||||
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
|
||||||
|
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||||
|
|
||||||
|
|
||||||
|
# Quick-start development settings - unsuitable for production
|
||||||
|
# See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/
|
||||||
|
|
||||||
|
# SECURITY WARNING: keep the secret key used in production secret!
|
||||||
|
SECRET_KEY = '_$=$%eqxk$8ss4n7mtgarw^5$8^d5+c83!vwatr@i_81myb=e4'
|
||||||
|
|
||||||
|
# SECURITY WARNING: don't run with debug turned on in production!
|
||||||
|
DEBUG = True
|
||||||
|
|
||||||
|
ALLOWED_HOSTS = []
|
||||||
|
|
||||||
|
|
||||||
|
# Application definition
|
||||||
|
|
||||||
|
INSTALLED_APPS = [
|
||||||
|
'django.contrib.admin',
|
||||||
|
'django.contrib.auth',
|
||||||
|
'django.contrib.contenttypes',
|
||||||
|
'django.contrib.sessions',
|
||||||
|
'django.contrib.messages',
|
||||||
|
'django.contrib.staticfiles',
|
||||||
|
'django_graphiql',
|
||||||
|
|
||||||
|
'cookbook.ingredients.apps.IngredientsConfig',
|
||||||
|
'cookbook.recipes.apps.RecipesConfig',
|
||||||
|
]
|
||||||
|
|
||||||
|
MIDDLEWARE_CLASSES = [
|
||||||
|
'django.middleware.security.SecurityMiddleware',
|
||||||
|
'django.contrib.sessions.middleware.SessionMiddleware',
|
||||||
|
'django.middleware.common.CommonMiddleware',
|
||||||
|
'django.middleware.csrf.CsrfViewMiddleware',
|
||||||
|
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||||
|
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
|
||||||
|
'django.contrib.messages.middleware.MessageMiddleware',
|
||||||
|
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
||||||
|
]
|
||||||
|
|
||||||
|
ROOT_URLCONF = 'cookbook.urls'
|
||||||
|
|
||||||
|
TEMPLATES = [
|
||||||
|
{
|
||||||
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
||||||
|
'DIRS': [],
|
||||||
|
'APP_DIRS': True,
|
||||||
|
'OPTIONS': {
|
||||||
|
'context_processors': [
|
||||||
|
'django.template.context_processors.debug',
|
||||||
|
'django.template.context_processors.request',
|
||||||
|
'django.contrib.auth.context_processors.auth',
|
||||||
|
'django.contrib.messages.context_processors.messages',
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
WSGI_APPLICATION = 'cookbook.wsgi.application'
|
||||||
|
|
||||||
|
|
||||||
|
# Database
|
||||||
|
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases
|
||||||
|
|
||||||
|
DATABASES = {
|
||||||
|
'default': {
|
||||||
|
'ENGINE': 'django.db.backends.sqlite3',
|
||||||
|
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Password validation
|
||||||
|
# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators
|
||||||
|
|
||||||
|
AUTH_PASSWORD_VALIDATORS = [
|
||||||
|
{
|
||||||
|
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
# Internationalization
|
||||||
|
# https://docs.djangoproject.com/en/1.9/topics/i18n/
|
||||||
|
|
||||||
|
LANGUAGE_CODE = 'en-us'
|
||||||
|
|
||||||
|
TIME_ZONE = 'UTC'
|
||||||
|
|
||||||
|
USE_I18N = True
|
||||||
|
|
||||||
|
USE_L10N = True
|
||||||
|
|
||||||
|
USE_TZ = True
|
||||||
|
|
||||||
|
|
||||||
|
# Static files (CSS, JavaScript, Images)
|
||||||
|
# https://docs.djangoproject.com/en/1.9/howto/static-files/
|
||||||
|
|
||||||
|
STATIC_URL = '/static/'
|
13
examples/cookbook/cookbook/urls.py
Normal file
13
examples/cookbook/cookbook/urls.py
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
from django.conf.urls import url, include
|
||||||
|
from django.contrib import admin
|
||||||
|
from django.views.decorators.csrf import csrf_exempt
|
||||||
|
|
||||||
|
from graphene.contrib.django.views import GraphQLView
|
||||||
|
|
||||||
|
from cookbook.schema import schema
|
||||||
|
|
||||||
|
urlpatterns = [
|
||||||
|
url(r'^admin/', admin.site.urls),
|
||||||
|
url(r'^graphql', csrf_exempt(GraphQLView.as_view(schema=schema))),
|
||||||
|
url(r'^graphiql', include('django_graphiql.urls')),
|
||||||
|
]
|
16
examples/cookbook/cookbook/wsgi.py
Normal file
16
examples/cookbook/cookbook/wsgi.py
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
"""
|
||||||
|
WSGI config for cookbook project.
|
||||||
|
|
||||||
|
It exposes the WSGI callable as a module-level variable named ``application``.
|
||||||
|
|
||||||
|
For more information on this file, see
|
||||||
|
https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/
|
||||||
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
from django.core.wsgi import get_wsgi_application
|
||||||
|
|
||||||
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cookbook.settings")
|
||||||
|
|
||||||
|
application = get_wsgi_application()
|
10
examples/cookbook/manage.py
Executable file
10
examples/cookbook/manage.py
Executable file
|
@ -0,0 +1,10 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cookbook.settings")
|
||||||
|
|
||||||
|
from django.core.management import execute_from_command_line
|
||||||
|
|
||||||
|
execute_from_command_line(sys.argv)
|
5
examples/cookbook/requirements.txt
Normal file
5
examples/cookbook/requirements.txt
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
-e git+https://github.com/adamcharnock/graphene.git@feature/django#egg=graphene
|
||||||
|
graphql-core==0.4.12
|
||||||
|
django==1.9
|
||||||
|
django_graphiql==0.4.2
|
||||||
|
django-filter==0.11.0
|
Loading…
Reference in New Issue
Block a user