Alexander Shpindler
Alexander Shpindler

Reputation: 951

Django ImportError at /graphql

Just can't understand what's wrong at all: Could not import 'ss.schema.schema' for Graphene setting 'SCHEMA'. AttributeError: type object 'Query' has no attribute '_meta'.

This is my Query class in application file "pages.schema.py":

class Query(graphene.AbstractType):
    user = graphene.relay.Node.Field(UserNode)
    users = DjangoFilterConnectionField(UserNode, filterset_class=UserFilter)

This is the full content of root schema file "ss.schema.py::

import graphene

import pages.schema


class Query(pages.schema.Query, graphene.ObjectType):
    pass


class Mutation(pages.schema.Mutation, graphene.ObjectType):
    pass


schema = graphene.Schema(query=Query, mutation=Mutation)

And here is full traceback:

Environment:


Request Method: GET
Request URL: http://95.213.203.11/graphql

Django Version: 1.11.5
Python Version: 3.5.2
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'corsheaders',
 'ws4redis',
 'graphene_django',
 'pages']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'corsheaders.middleware.CorsMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/root/ss/lib/python3.5/site-packages/graphene_django/settings.py" in import_from_string
  74.         module = importlib.import_module(module_path)

File "/root/ss/lib/python3.5/importlib/__init__.py" in import_module
  126.     return _bootstrap._gcd_import(name[level:], package, level)

File "./ss/schema.py" in <module>
  3. import pages.schema

File "./pages/schema.py" in <module>
  130. schema = graphene.Schema(query=Query, mutation=Mutation)

File "/root/ss/lib/python3.5/site-packages/graphene/types/schema.py" in __init__
  27.         ).format(query)

File "/root/ss/lib/python3.5/site-packages/graphene/utils/subclass_with_meta.py" in __repr__
  11.         return cls._meta.name

During handling of the above exception (type object 'Query' has no attribute '_meta'), another exception occurred:

File "/root/ss/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

File "/root/ss/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/root/ss/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/root/ss/lib/python3.5/site-packages/django/views/decorators/csrf.py" in wrapped_view
  58.         return view_func(*args, **kwargs)

File "/root/ss/lib/python3.5/site-packages/django/views/generic/base.py" in view
  62.             self = cls(**initkwargs)

File "/root/ss/lib/python3.5/site-packages/graphene_django/views.py" in __init__
  70.             schema = graphene_settings.SCHEMA

File "/root/ss/lib/python3.5/site-packages/graphene_django/settings.py" in __getattr__
  116.             val = perform_import(val, attr)

File "/root/ss/lib/python3.5/site-packages/graphene_django/settings.py" in perform_import
  60.         return import_from_string(val, setting_name)

File "/root/ss/lib/python3.5/site-packages/graphene_django/settings.py" in import_from_string
  78.         raise ImportError(msg)

Exception Type: ImportError at /graphql
Exception Value: Could not import 'ss.schema.schema' for Graphene setting 'SCHEMA'. AttributeError: type object 'Query' has no attribute '_meta'.

Any suggestions how to fix it?

Upvotes: 0

Views: 1872

Answers (2)

KingNonso
KingNonso

Reputation: 712

This may just have to do with the overall structure of your project/ file Query vs Mutation import structure. https://github.com/graphql-python/graphene-django/issues/569#issuecomment-883172665

Previously I had

from orders.graphql.mutation.customer import CMutation
from orders.graphql.mutation.store import SMutation
from orders.graphql.query import CQuery, SQuery

in my schema.py. and Got the error

ImportError: Could not import 'p.schema' for Graphene setting 'SCHEMA'. AttributeError: 'NoneType' object has no attribute '_meta'.

after which I changed it to

from orders.graphql.query import CQuery, SQuery
from orders.graphql.mutation.customer import CMutation
from orders.graphql.mutation.store import SMutation

and problem was solved.

Upvotes: 0

Sean Bolton
Sean Bolton

Reputation: 186

The ease with which Graphene (and similar frameworks like Django and SQLAlchemy) let us build our schema and incorporate program logic, without having to write a lot of boilerplate, has a dark side: quite often the error messages and backtraces that they give do little to point us toward the actual error.

Such is the case here. There's nothing wrong with you ss/schema.py, the problem has to be in pages/schema.py. Since you've given us so little of that, I can't tell what, but here is slightly fleshed-out example, based on your code, that works in Graphene 2.0:

ss/schema.py:

import graphene

import pages.schema

class Query(pages.schema.Query, graphene.ObjectType):
    pass

#class Mutation(pages.schema.Mutation, graphene.ObjectType):
#    pass

#schema = graphene.Schema(query=Query, mutation=Mutation)
schema = graphene.Schema(query=Query)

pages/models.py:

from django.db import models

class User(models.Model):
    name = models.CharField(max_length=100)

pages/schema.py:

from django_filters import FilterSet
from graphene import relay
from graphene_django import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField

from .models import User

class UserNode(DjangoObjectType):
    class Meta:
        model = User
        interfaces = (relay.Node, )

class UserFilter(FilterSet):
    class Meta:
        model = User
        fields = ['name']

class Query(object):
    user = relay.Node.Field(UserNode)
    users = DjangoFilterConnectionField(UserNode, filterset_class=UserFilter)

Tested with the following query:

{
  users {
    edges {
      node {
        id
        ... on UserNode {
          name
        }
      }
    }
  }
}

Hopefully something in there will point you in the right direction. If not, please post more of your pages/schema.py, and we'll go from there.

Upvotes: 2

Related Questions