0

根本无法理解出了什么问题:无法为石墨烯设置“SCHEMA”导入“ss.schema.schema”。AttributeError:类型对象“查询”没有属性“_meta”。

这是我在应用程序文件“pages.schema.py”中的查询类:

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

这是根模式文件“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)

这是完整的回溯:

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'.

任何建议如何解决它?

4

2 回答 2

2

Graphene(以及类似的框架,如 Django 和 SQLAlchemy)让我们无需编写大量样板就可以轻松地构建我们的模式并合并程序逻辑,但它有一个阴暗面:通常它们提供的错误消息和回溯几乎没有什么作用指出我们的实际错误。

这里就是这种情况。你ss/schema.py没什么问题,问题一定出在pages/schema.py中。既然你给我们的东西这么少,我不知道是什么,但这里有一个稍微充实的例子,根据你的代码,它适用于 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)

页面/models.py:

from django.db import models

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

页面/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)

使用以下查询进行测试:

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

希望那里的某些东西会为您指明正确的方向。如果没有,请发布更多您的页面/schema.py,我们将从那里开始。

于 2017-11-15T04:51:42.733 回答
0

这可能与您的项目/文件查询与突变导入结构的整体结构有关。 https://github.com/graphql-python/graphene-django/issues/569#issuecomment-883172665

以前我有

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

在我的 schema.py 中。并得到了错误

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

之后我将其更改为

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

问题就解决了。

于 2021-10-10T20:33:45.787 回答