需要向 GraphQLView 提供 Schema

Posted

技术标签:

【中文标题】需要向 GraphQLView 提供 Schema【英文标题】:A Schema is required to be provided to GraphQLView 【发布时间】:2017-10-11 18:44:49 【问题描述】:

我正在关注this 教程将 Graphql 与 Django 集成,当我在本地机器上点击 graphql URL 时,我按照该教程做了所有事情

http://localhost:8000/graphql

我收到以下错误

/graphql 处的断言错误

请求方法:GET 请求网址:http://localhost:8000/graphql Django 版本:1.11.1 异常类型:AssertionError 异常值: 。 异常位置:/home/psingh/Projects/django_graphql/env/local/lib/python2.7/site-packages/graphene_django/views.py in init,第 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)

app>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')),    

]

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

【问题讨论】:

【参考方案1】:

您必须将架构添加到您的 settings.py,如 here 所示:

GRAPHENE = 'SCHEMA': 'cookbook.schema.schema'

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

【讨论】:

我已将其添加为 GRAPHENE = 'SCHEMA': 'project.schema.schema' 我已经用所有必需的代码编辑了问题。【参考方案2】:

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

   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。

【讨论】:

【参考方案3】:

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

【讨论】:

您的答案似乎与接受的答案没有什么不同。此外,这个问题是在 3 年前提出并回答的。回答时请务必查看原始问题的日期。请阅读How to Answer。

以上是关于需要向 GraphQLView 提供 Schema的主要内容,如果未能解决你的问题,请参考以下文章

向以下查询报告的 information_schema 是啥意思?

在C++中如何用schema校验xml文件

schema 文件约束

MySQL 5.7 SYS系统SCHEMA

Spring Cloud Schema Registry

向 Solr 核心添加字段时,为啥会出现“ManagedIndexSchema Error persisting managed schema => FileNotFoundException: