如何在GraphQL中查询嵌套对象?

时间:2020-06-10 10:22:04

标签: graphql mongoengine graphene-python

我是GraphQL和MongoDB的新手,并尝试同时使用两者构建后端。

我遵循了https://github.com/graphql-python/graphene-mongo/tree/master/examples/flask_mongoengine上的示例,并以某种方式能够构建一个示例。但是我无法查询嵌套对象,不确定是代码问题还是查询问题?

models.py

from mongoengine import connect, Document, EmbeddedDocument
from mongoengine.fields import (
    StringField,
    IntField,
    ReferenceField,
    ListField,
    EmbeddedDocumentField,
    ObjectIdField
)

class author(Document):
    meta = {'db_alias': 'db', 'collection': 'author'}
    acronym = StringField()
    name = StringField()
    notes = StringField()

class author_list(EmbeddedDocument):
    sequence = IntField()
    author = ReferenceField(author)

class book(Document):
    meta = {'db_alias': 'db', 'collection': 'book'}
    publish_date = StringField()
    title = StringField()
    author_list = ListField(EmbeddedDocumentField(author_list))
    category = StringField()

schema.py

import graphene

from graphene.relay import Node
from graphene_mongo import MongoengineConnectionField, MongoengineObjectType
from models import author as AuthorModel
from models import author_list as AuthorListModel
from models import book as BookModel

class AuthorType(MongoengineObjectType):
    class Meta:
        model = AuthorModel
        interfaces = (Node,)


class AuthorListType(MongoengineObjectType):
    class Meta:
        model = AuthorListModel
        interfaces = (Node,)

class BookType(MongoengineObjectType):
    class Meta:
        model = BookModel
        interfaces = (Node,)


class Query(graphene.ObjectType):
    node = Node.Field()

    books = MongoengineConnectionField(BookType)

    authors = MongoengineConnectionField(AuthorType)

schema = graphene.Schema(query=Query, types=[BookType, AuthorListType, AuthorType])

查询

{
  books {
    edges {
      node {
        authorList{
          edges {
            node {
              sequence
              author(acronym: "CC") {
                name
              }
            }
          }
        }
      }
    }
  }
}

我收到以下错误:

{
  "errors": [
    {
      "message": "Unknown argument \"acronym\" on field \"author\" of type \"AuthorListType\".",
      "locations": [
        {
          "line": 10,
          "column": 20
        }
      ]
    }
  ]
}

0 个答案:

没有答案