Reputation: 1
i am a beginner with Django & GraphQL, i had a problem the at first step, i can not reach to GraphiQL, i had an error
Could not import 'traindjango.schema.schema' for Graphene setting 'SCHEMA'.
AttributeError: module 'graphene' has no attribute 'Heroes'.
traindjango/heroes/schema.py
import graphene
from graphene_django import DjangoObjectType
from .models import Heroes
class HeroesType(DjangoObjectType):
class Meta:
model = Heroes
class Query(graphene.ObjectType):
heroes = graphene.Heroes(HeroesType)
def resolve_links(self, info, **kwargs):
return Heroes.objects.all()
traindjango/traindjango/schema.py
import graphene
import heroes.schema
class Query(heroes.schema.Query, graphene.ObjectType):
pass
schema = graphene.Schema(query=Query)
traindjango/traindjango/settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'heroes',
'graphene_django',
]
GRAPHENE = {
'SCHEMA' : 'traindjango.schema.schema',
}
traindjango/heroes/models.py
from django.db import models
class Heroes(models.Model):
name = models.CharField(max_length=100, verbose_name='Name')
power = models.IntegerField(default=0)
city = models.TextField(max_length=100, verbose_name='City' ,null=True, blank=True)
Could you please help me what i can do it?
Thanx a lot
Upvotes: 0
Views: 2206
Reputation: 3385
Instead of
heroes = graphene.Heroes(HeroesType)
You need
heroes = graphene.List(HeroesType)
Heroes
is your model, and does not belong to Graphene.
Then you need to rename resolve_links
as resolve_heroes
P.S. Good practice is to name your django modules in the singular, i.e. Hero, not Heroes. You can set the verbose name for the plural in Meta
if it's not just adding an 's'.
Upvotes: 1