简体   繁体   English

/graphql/ 处的 AssertionError 查询字段必须是一个以字段名称作为键的映射(dict / OrderedDict)或返回此类映射的 function

[英]AssertionError at /graphql/ Query fields must be a mapping (dict / OrderedDict) with field names as keys or a function which returns such a mapping

I am new to django and graphql, I am trying use graphql in django using the the instructions given in this site.我是 django 和 graphql 的新手,我正在尝试按照本网站提供的说明在 django 中使用 graphql。

https://docs.graphene-python.org/projects/django/en/latest/installation/ https://docs.graphene-python.org/projects/django/en/latest/installation/

I followed the instructions carefully but when I enter the url http://127.0.0.1:8000/graphql/我仔细按照说明进行操作,但是当我输入 url http://127.0.0.1:8000/graphql/

I am getting the error following error.我在错误之后收到错误。

AssertionError at /graphql/
Query fields must be a mapping (dict / OrderedDict) with field names 
as keys or a function which returns such a mapping.

I just have a single app called demo.我只有一个名为 demo 的应用程序。 The code in the models.py of demo app is as follows... demo app的models.py中的代码如下...

from django.db import models

# Create your models here.
class UserProfile(models.Model):
    userId = models.IntegerField(primary_key=True)
    userName = models.CharField(max_length=15)
    password = models.CharField(max_length=15)
    address = models.CharField(max_length=30)
    phoneNumber = models.IntegerField()

    class Meta:
        db_table = 'UserProfile'

Here are the relevant parts of the settings.py file in the project folder.以下是项目文件夹中 settings.py 文件的相关部分。

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'graphene_django',
    'corsheaders',
    'demo',
]

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',
]

CORS_ORIGIN_ALLOW_ALL = True

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

abc is the name of the main project folder which contains manage.py . abc 是包含manage.py的主项目文件夹的名称。

Here is the urls.py file这是 urls.py 文件

from django.contrib import admin
from django.urls import path
from graphene_django.views import GraphQLView

urlpatterns = [
        path('admin/', admin.site.urls),
        path('graphql/', GraphQLView.as_view(graphiql=True)),
    ]

Please help,thanks in advance.请帮助,在此先感谢。

My problem was solved by inserting DjangoObjectType instead of ObjectType in schema.py you can import it: from graphene_django.types import DjangoObjectType我的问题是通过在 schema.py 中插入DjangoObjectType而不是 ObjectType 来解决的,你可以导入它: from graphene_django.types import DjangoObjectType

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM