[英]AssertionError at /graphql/ A Schema is required to be provided to GraphQLView
I have a Django API with a GraphQL route.我有一个带有 GraphQL 路由的 Django API。
Here is my models.py:这是我的models.py:
class MyBook(models.Model):
id = models.BigAutoField(primary_key= True)
book_name = models.CharField(max_length= 200)
title = models.CharField(max_length= 200)
In my Schema.py:在我的 Schema.py 中:
import graphene
from graphene_django.views import GraphQLSchema
from graphene_django import DjangoObjectType
from publishing_house_app.models import *
class BookType(DjangoObjectType):
class Meta:
model= MyBook
fields= "__all__"
class RootQuery(graphene.ObjectType):
books = graphene.List(BookType)
def resolve_books(self, root, info, **kwargs):
MyBook.objects.all()
MyBookSchema = graphene.Schema(query= RootQuery)
in urls.py:在 urls.py 中:
from .views import *
from rest_framework.urlpatterns import path
from graphene_django.views import GraphQLView
from .GraphQL import Schema
urlpatterns = [
path("graphql/", GraphQLView.as_view(graphiql= True, schema= Schema)),
]
I am getting the following error:我收到以下错误:
File "C:\\Python39\\lib\\site-packages\\graphene_django\\views.py", line 127, in init assert isinstance.
文件“C:\\Python39\\lib\\site-packages\\graphene_django\\views.py”,第 127 行,在init assert isinstance 中。 AssertionError at /graphql/ A Schema is required to be provided to GraphQLView.
AssertionError at /graphql/ 需要向 GraphQLView 提供架构。
You need to pass a valid graphene schema.您需要传递有效的石墨烯架构。 You can either:
您可以:
urlpatterns = [
path("graphql/", GraphQLView.as_view(graphiql=True, schema=MyBookSchema)),
]
or define it in your settings like so或像这样在您的设置中定义它
GRAPHENE = {
...
"SCHEMA": "appname.schema.MyBookSchema",
...
}
and after this you can then create a url without having to pass the schema:在此之后,您可以创建一个 url,而无需传递架构:
urlpatterns = [
path("graphql/", GraphQLView.as_view(graphiql=True)),
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.