gpt4 book ai didi

django - 需要向 GraphQLView 提供 Schema

转载 作者:行者123 更新时间:2023-12-01 23:07:10 28 4
gpt4 key购买 nike

我正在关注 this 教程以将 Graphql 与 Django 集成,当我在本地机器上点击 graphql URL 时,我根据该教程完成了所有操作

http://localhost:8000/graphql



我收到以下错误

AssertionError at /graphql

A Schema is required to be provided to GraphQLView.



请求方式:GET
请求网址: http://localhost:8000/graphql
Django 版本:1.11.1
异常类型:断言错误
异常值:
需要向 GraphQLView 提供 Schema。
异常位置:/home/psingh/Projects/django_graphql/env/local/lib/python2.7/site-packages/graphene_django/views.py in init , line 84
Python 可执行文件:/home/psingh/Projects/django_graphql/env/bin/python
Python 版本:2.7.6
python 路径:
['/home/psingh/Projects/django_graphql/project',
'/home/psingh/Projects/django_graphql/env/lib/python2.7',
'/home/psingh/Projects/django_graphql/env/lib/python2.7/plat-x86_64-linux-gnu',
'/home/psingh/Projects/django_graphql/env/lib/python2.7/lib-tk',
'/home/psingh/Projects/django_graphql/env/lib/python2.7/lib-old',
'/home/psingh/Projects/django_graphql/env/lib/python2.7/lib-dynload',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/home/psingh/Projects/django_graphql/env/local/lib/python2.7/site-packages',
'/home/psingh/Projects/django_graphql/env/lib/python2.7/site-packages']
服务器时间:2017年5月12日星期五12:18:31 +0000

在settings.py中
GRAPHENE = {
'SCHEMA': 'project.schema.schema'

}

项目> schema.py
import graphene
import mainapp.schema
class Query(mainapp.schema.Query, graphene.ObjectType):
# This class will inherit from multiple Queries
# as we begin to add more apps to our project
pass

schema = graphene.Schema(query=Query)

应用程序>schema.py
import graphene
from graphene_django.types import DjangoObjectType
from cookbook.ingredients.models import Category, Ingredient

class CategoryType(DjangoObjectType):
class Meta:
model = Category


class IngredientType(DjangoObjectType):
class Meta:
model = Ingredient


class Query(graphene.AbstractType):
all_categories = graphene.List(CategoryType)
all_ingredients = graphene.List(IngredientType)

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

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

project_urls.py
from django.conf.urls import include, url
from django.contrib import admin

from graphene_django.views import GraphQLView
import schema

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^graphql', GraphQLView.as_view(graphiql=True)),
url(r'^', include('mainapp.urls')),

]

任何帮助都会很棒。我是编码方面的新手。
提前致谢。

最佳答案

您必须将架构添加到您的 settings.py 中,如 here 所示:
GRAPHENE = {
'SCHEMA': 'cookbook.schema.schema'
}

您需要 2 个 schema.py 文件,一个在项目的根级别,一个在 app 文件夹中。

关于django - 需要向 GraphQLView 提供 Schema,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43937859/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com