需要向GraphQLView提供架构

时间:2017-05-12 12:31:34

标签: django graphql

我正在关注this教程将Graphql与Django集成,当我在本地计算机上访问graphql URL时,我根据该教程做了一切

  

http://localhost:8000/graphql

我发现了以下错误

  

/ graphql

中的AssertionError      

需要向GraphQLView提供架构。

请求方法:GET 请求网址:http://localhost:8000/graphql Django版本:1.11.1 异常类型:AssertionError 例外价值:
需要向GraphQLView提供架构。 异常位置:/home/psingh/Projects/django_graphql/env/local/lib/python2.7/site-packages/graphene_django/views.py在 init ,第84行 Python可执行文件:/ home / psingh / Projects / django_graphql / env / bin / python Python版本:2.7.6 Python路径:
[' /家/ psingh /项目/ django_graphql /项目&#39 ;,  ' /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')),    

]

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

2 个答案:

答案 0 :(得分:2)

如果您不想在GRAPHENE中添加settings.py变量,则可以在scheme方法调用中传递GraphQLView.as_view()参数

   from onlineshop_project.scheme import schema
   urlpatterns = [
       url(r'^admin/', admin.site.urls),
       url(r'^graphql', GraphQLView.as_view(graphiql=True, schema=schema)),
   ]

您可以选中the documentation

答案 1 :(得分:0)

您是否已在INSTALLED_APPS中的settings.py文件下添加了“ graphene_django”? 检查此处-https://docs.graphene-python.org/projects/django/en/latest/tutorial-plain/#update-settings