Skip to content

Cookbook plain 2.1.2 compatible #538

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 2.0 on 2018-10-18 17:46

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('ingredients', '0002_auto_20161104_0050'),
]

operations = [
migrations.AlterModelOptions(
name='category',
options={'verbose_name_plural': 'Categories'},
),
]
4 changes: 3 additions & 1 deletion examples/cookbook-plain/cookbook/ingredients/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@


class Category(models.Model):
class Meta:
verbose_name_plural = 'Categories'
name = models.CharField(max_length=100)

def __str__(self):
Expand All @@ -11,7 +13,7 @@ def __str__(self):
class Ingredient(models.Model):
name = models.CharField(max_length=100)
notes = models.TextField(null=True, blank=True)
category = models.ForeignKey(Category, related_name='ingredients')
category = models.ForeignKey(Category, related_name='ingredients', on_delete=models.CASCADE)

def __str__(self):
return self.name
16 changes: 5 additions & 11 deletions examples/cookbook-plain/cookbook/ingredients/schema.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import graphene
from graphene_django.types import DjangoObjectType

from cookbook.ingredients.models import Category, Ingredient
from .models import Category, Ingredient


class CategoryType(DjangoObjectType):
Expand All @@ -25,17 +25,14 @@ class Query(object):
name=graphene.String())
all_ingredients = graphene.List(IngredientType)

def resolve_all_categories(self, args, context, info):
def resolve_all_categories(self, context):
return Category.objects.all()

def resolve_all_ingredients(self, args, context, info):
def resolve_all_ingredients(self, context):
# We can easily optimize query count in the resolve method
return Ingredient.objects.select_related('category').all()

def resolve_category(self, args, context, info):
id = args.get('id')
name = args.get('name')

def resolve_category(self, context, id=None, name=None):
if id is not None:
return Category.objects.get(pk=id)

Expand All @@ -44,10 +41,7 @@ def resolve_category(self, args, context, info):

return None

def resolve_ingredient(self, args, context, info):
id = args.get('id')
name = args.get('name')

def resolve_ingredient(self, context, id=None, name=None):
if id is not None:
return Ingredient.objects.get(pk=id)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.0 on 2018-10-18 17:28

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('recipes', '0002_auto_20161104_0106'),
]

operations = [
migrations.AlterField(
model_name='recipeingredient',
name='unit',
field=models.CharField(choices=[('unit', 'Units'), ('kg', 'Kilograms'), ('l', 'Litres'), ('st', 'Shots')], max_length=20),
),
]
9 changes: 5 additions & 4 deletions examples/cookbook-plain/cookbook/recipes/models.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
from django.db import models

from cookbook.ingredients.models import Ingredient
from ..ingredients.models import Ingredient


class Recipe(models.Model):
title = models.CharField(max_length=100)
instructions = models.TextField()
__unicode__ = lambda self: self.title
def __str__(self):
return self.title


class RecipeIngredient(models.Model):
recipe = models.ForeignKey(Recipe, related_name='amounts')
ingredient = models.ForeignKey(Ingredient, related_name='used_by')
recipe = models.ForeignKey(Recipe, related_name='amounts', on_delete=models.CASCADE)
ingredient = models.ForeignKey(Ingredient, related_name='used_by', on_delete=models.CASCADE)
amount = models.FloatField()
unit = models.CharField(max_length=20, choices=(
('unit', 'Units'),
Expand Down
15 changes: 5 additions & 10 deletions examples/cookbook-plain/cookbook/recipes/schema.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import graphene
from graphene_django.types import DjangoObjectType

from cookbook.recipes.models import Recipe, RecipeIngredient
from .models import Recipe, RecipeIngredient


class RecipeType(DjangoObjectType):
Expand All @@ -24,10 +24,7 @@ class Query(object):
id=graphene.Int())
all_recipeingredients = graphene.List(RecipeIngredientType)

def resolve_recipe(self, args, context, info):
id = args.get('id')
title = args.get('title')

def resolve_recipe(self, context, id=None, title=None):
if id is not None:
return Recipe.objects.get(pk=id)

Expand All @@ -36,17 +33,15 @@ def resolve_recipe(self, args, context, info):

return None

def resolve_recipeingredient(self, args, context, info):
id = args.get('id')

def resolve_recipeingredient(self, context, id=None):
if id is not None:
return RecipeIngredient.objects.get(pk=id)

return None

def resolve_all_recipes(self, args, context, info):
def resolve_all_recipes(self, context):
return Recipe.objects.all()

def resolve_all_recipeingredients(self, args, context, info):
def resolve_all_recipeingredients(self, context):
related = ['recipe', 'ingredient']
return RecipeIngredient.objects.select_related(*related).all()
3 changes: 1 addition & 2 deletions examples/cookbook-plain/cookbook/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,12 @@
'cookbook.recipes.apps.RecipesConfig',
]

MIDDLEWARE_CLASSES = [
MIDDLEWARE = [
'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',
]
Expand Down
6 changes: 3 additions & 3 deletions examples/cookbook-plain/cookbook/urls.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from django.conf.urls import url
from django.urls import path
from django.contrib import admin

from graphene_django.views import GraphQLView


urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^graphql', GraphQLView.as_view(graphiql=True)),
path('admin/', admin.site.urls),
path('graphql/', GraphQLView.as_view(graphiql=True)),
]
2 changes: 1 addition & 1 deletion examples/cookbook-plain/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
graphene
graphene-django
graphql-core>=2.1rc1
django==1.9
django==2.1.2