6

I was following this tutorial to integrate Graphql with Django, I did everything according to that tutorial when I'm hitting graphql URL on my local machine

http://localhost:8000/graphql

I'm geting the following error

AssertionError at /graphql

A Schema is required to be provided to GraphQLView.

Request Method: GET Request URL: http://localhost:8000/graphql Django Version: 1.11.1 Exception Type: AssertionError Exception Value:
A Schema is required to be provided to GraphQLView. Exception Location: /home/psingh/Projects/django_graphql/env/local/lib/python2.7/site-packages/graphene_django/views.py in init, line 84 Python Executable: /home/psingh/Projects/django_graphql/env/bin/python Python Version: 2.7.6 Python Path:
['/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'] Server time: Fri, 12 May 2017 12:18:31 +0000

In settings.py

GRAPHENE = {
'SCHEMA': 'project.schema.schema'

}

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

]

Any help would be great.I am new to the coding stuff. Thanks in advance.

0

3 Answers 3

8

You have to add the schema to your settings.py as seen here:

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

You need 2 schema.py files, one at the root level of the project and one in the app folder.

2
  • 2
    I have added that as GRAPHENE = { 'SCHEMA': 'project.schema.schema' } Commented May 12, 2017 at 13:43
  • 1
    I have edited the question with all the required codes. Commented May 12, 2017 at 13:50
5

if you don't want to add GRAPHENE variable in settings.py then you can pass scheme parameter in GraphQLView.as_view() method call

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

You can check the documentation.

0

Have you added "graphene_django" in INSTALLED_APPS under settings.py file ? Check here - https://docs.graphene-python.org/projects/django/en/latest/tutorial-plain/#update-settings

1
  • Your answer doesn't seem to be different than the accepted answer. Also, the question was asked and answered 3 years ago. Be sure to look at the date of the original question when answering. Please read How to Answer. Commented Aug 16, 2020 at 18:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.