mirror of
https://github.com/graphql-python/graphene-django.git
synced 2024-11-13 05:07:02 +03:00
Making the example working for Django 2.1.2
This commit is contained in:
parent
f76f38ef30
commit
19ef9a094a
|
@ -2,6 +2,8 @@ from django.db import models
|
||||||
|
|
||||||
|
|
||||||
class Category(models.Model):
|
class Category(models.Model):
|
||||||
|
class Meta:
|
||||||
|
verbose_name_plural = 'Categories'
|
||||||
name = models.CharField(max_length=100)
|
name = models.CharField(max_length=100)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
@ -11,7 +13,7 @@ class Category(models.Model):
|
||||||
class Ingredient(models.Model):
|
class Ingredient(models.Model):
|
||||||
name = models.CharField(max_length=100)
|
name = models.CharField(max_length=100)
|
||||||
notes = models.TextField(null=True, blank=True)
|
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):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import graphene
|
import graphene
|
||||||
from graphene_django.types import DjangoObjectType
|
from graphene_django.types import DjangoObjectType
|
||||||
|
|
||||||
from cookbook.ingredients.models import Category, Ingredient
|
from .models import Category, Ingredient
|
||||||
|
|
||||||
|
|
||||||
class CategoryType(DjangoObjectType):
|
class CategoryType(DjangoObjectType):
|
||||||
|
@ -25,16 +25,16 @@ class Query(object):
|
||||||
name=graphene.String())
|
name=graphene.String())
|
||||||
all_ingredients = graphene.List(IngredientType)
|
all_ingredients = graphene.List(IngredientType)
|
||||||
|
|
||||||
def resolve_all_categories(self, args, context, info):
|
def resolve_all_categories(self, context, **kwargs):
|
||||||
return Category.objects.all()
|
return Category.objects.all()
|
||||||
|
|
||||||
def resolve_all_ingredients(self, args, context, info):
|
def resolve_all_ingredients(self, context, **kwargs):
|
||||||
# We can easily optimize query count in the resolve method
|
# We can easily optimize query count in the resolve method
|
||||||
return Ingredient.objects.select_related('category').all()
|
return Ingredient.objects.select_related('category').all()
|
||||||
|
|
||||||
def resolve_category(self, args, context, info):
|
def resolve_category(self, context, **kwargs):
|
||||||
id = args.get('id')
|
id = kwargs.get('id')
|
||||||
name = args.get('name')
|
name = kwargs.get('name')
|
||||||
|
|
||||||
if id is not None:
|
if id is not None:
|
||||||
return Category.objects.get(pk=id)
|
return Category.objects.get(pk=id)
|
||||||
|
@ -44,9 +44,9 @@ class Query(object):
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def resolve_ingredient(self, args, context, info):
|
def resolve_ingredient(self, context, **kwargs):
|
||||||
id = args.get('id')
|
id = kwargs.get('id')
|
||||||
name = args.get('name')
|
name = kwargs.get('name')
|
||||||
|
|
||||||
if id is not None:
|
if id is not None:
|
||||||
return Ingredient.objects.get(pk=id)
|
return Ingredient.objects.get(pk=id)
|
||||||
|
|
|
@ -1,17 +1,18 @@
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
from cookbook.ingredients.models import Ingredient
|
from ..ingredients.models import Ingredient
|
||||||
|
|
||||||
|
|
||||||
class Recipe(models.Model):
|
class Recipe(models.Model):
|
||||||
title = models.CharField(max_length=100)
|
title = models.CharField(max_length=100)
|
||||||
instructions = models.TextField()
|
instructions = models.TextField()
|
||||||
__unicode__ = lambda self: self.title
|
def __str__(self):
|
||||||
|
return self.title
|
||||||
|
|
||||||
|
|
||||||
class RecipeIngredient(models.Model):
|
class RecipeIngredient(models.Model):
|
||||||
recipe = models.ForeignKey(Recipe, related_name='amounts')
|
recipe = models.ForeignKey(Recipe, related_name='amounts', on_delete=models.CASCADE)
|
||||||
ingredient = models.ForeignKey(Ingredient, related_name='used_by')
|
ingredient = models.ForeignKey(Ingredient, related_name='used_by', on_delete=models.CASCADE)
|
||||||
amount = models.FloatField()
|
amount = models.FloatField()
|
||||||
unit = models.CharField(max_length=20, choices=(
|
unit = models.CharField(max_length=20, choices=(
|
||||||
('unit', 'Units'),
|
('unit', 'Units'),
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import graphene
|
import graphene
|
||||||
from graphene_django.types import DjangoObjectType
|
from graphene_django.types import DjangoObjectType
|
||||||
|
|
||||||
from cookbook.recipes.models import Recipe, RecipeIngredient
|
from .models import Recipe, RecipeIngredient
|
||||||
|
|
||||||
|
|
||||||
class RecipeType(DjangoObjectType):
|
class RecipeType(DjangoObjectType):
|
||||||
|
@ -24,9 +24,9 @@ class Query(object):
|
||||||
id=graphene.Int())
|
id=graphene.Int())
|
||||||
all_recipeingredients = graphene.List(RecipeIngredientType)
|
all_recipeingredients = graphene.List(RecipeIngredientType)
|
||||||
|
|
||||||
def resolve_recipe(self, args, context, info):
|
def resolve_recipe(self, context, **kwargs):
|
||||||
id = args.get('id')
|
id = kwargs.get('id')
|
||||||
title = args.get('title')
|
title = kwargs.get('title')
|
||||||
|
|
||||||
if id is not None:
|
if id is not None:
|
||||||
return Recipe.objects.get(pk=id)
|
return Recipe.objects.get(pk=id)
|
||||||
|
@ -36,17 +36,17 @@ class Query(object):
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def resolve_recipeingredient(self, args, context, info):
|
def resolve_recipeingredient(self, context, **kwargs):
|
||||||
id = args.get('id')
|
id = kwargs.get('id')
|
||||||
|
|
||||||
if id is not None:
|
if id is not None:
|
||||||
return RecipeIngredient.objects.get(pk=id)
|
return RecipeIngredient.objects.get(pk=id)
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def resolve_all_recipes(self, args, context, info):
|
def resolve_all_recipes(self, context, **kwargs):
|
||||||
return Recipe.objects.all()
|
return Recipe.objects.all()
|
||||||
|
|
||||||
def resolve_all_recipeingredients(self, args, context, info):
|
def resolve_all_recipeingredients(self, context, **kwargs):
|
||||||
related = ['recipe', 'ingredient']
|
related = ['recipe', 'ingredient']
|
||||||
return RecipeIngredient.objects.select_related(*related).all()
|
return RecipeIngredient.objects.select_related(*related).all()
|
||||||
|
|
|
@ -44,13 +44,12 @@ INSTALLED_APPS = [
|
||||||
'cookbook.recipes.apps.RecipesConfig',
|
'cookbook.recipes.apps.RecipesConfig',
|
||||||
]
|
]
|
||||||
|
|
||||||
MIDDLEWARE_CLASSES = [
|
MIDDLEWARE = [
|
||||||
'django.middleware.security.SecurityMiddleware',
|
'django.middleware.security.SecurityMiddleware',
|
||||||
'django.contrib.sessions.middleware.SessionMiddleware',
|
'django.contrib.sessions.middleware.SessionMiddleware',
|
||||||
'django.middleware.common.CommonMiddleware',
|
'django.middleware.common.CommonMiddleware',
|
||||||
'django.middleware.csrf.CsrfViewMiddleware',
|
'django.middleware.csrf.CsrfViewMiddleware',
|
||||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||||
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
|
|
||||||
'django.contrib.messages.middleware.MessageMiddleware',
|
'django.contrib.messages.middleware.MessageMiddleware',
|
||||||
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
from django.conf.urls import url
|
from django.urls import path
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
|
|
||||||
from graphene_django.views import GraphQLView
|
from graphene_django.views import GraphQLView
|
||||||
|
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
url(r'^admin/', admin.site.urls),
|
path('admin/', admin.site.urls),
|
||||||
url(r'^graphql', GraphQLView.as_view(graphiql=True)),
|
path('graphql/', GraphQLView.as_view(graphiql=True)),
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
graphene
|
graphene
|
||||||
graphene-django
|
graphene-django
|
||||||
graphql-core>=2.1rc1
|
graphql-core>=2.1rc1
|
||||||
django==1.9
|
django==2.1.2
|
||||||
|
|
Loading…
Reference in New Issue
Block a user