Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A Schema is required to be provided to GraphQLView

Tags:

django

graphql

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.

like image 638
piyush singh Avatar asked Dec 13 '22 23:12

piyush singh


1 Answers

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.

like image 119
Timo Avatar answered Jan 08 '23 15:01

Timo