简体   繁体   中英

AssertionError at /graphql/ A Schema is required to be provided to GraphQLView

I have a Django API with a GraphQL route.

Here is my 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:

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:

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. AssertionError at /graphql/ A Schema is required to be provided to 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:

urlpatterns = [
    path("graphql/", GraphQLView.as_view(graphiql=True)),
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM