【发布时间】:2019-12-14 23:02:36
【问题描述】:
我有 3 个文件:authors.py、posts.py 和 schema.py。
帖子有一个作者,查询构建在架构文件中。
我正在尝试从Post 内部解析Author,而不在Post 中声明解析器函数,因为Author 已经声明了自己的解析器函数。以下代码有效,但我必须从 Post 类型内部引用 resolve_author ,这似乎不正确。我认为石墨烯应该将parent 参数直接传递给Author,不是吗?
如果我没有在Post 类型中为author 设置解析器,它只会返回null。
schema.py
import graphene
from graphql_api import posts, authors
class Query(posts.Query, authors.Query):
pass
schema = graphene.Schema(query=Query)
authors.py
from graphene import ObjectType, String, Field
class Author(ObjectType):
id = ID()
name = String()
class Query(ObjectType):
author = Field(Author)
def resolve_author(parent, info):
return {
'id': '123',
'name': 'Grizzly Bear',
'avatar': '#984321'
}
posts.py
from graphene import ObjectType, String, Field
from graphql_api import authors
class Post(ObjectType):
content = String()
author = Field(authors.Author)
def resolve_author(parent, info):
# I'm doing like this and it works, but it seems wrong.
# I think Graphene should be able to use my resolver
# from the Author automatically...
return authors.Query.resolve_author(parent,
info, id=parent['authorId'])
class Query(ObjectType):
post = Field(Post)
def resolve_post(parent, info):
return {
'content': 'A title',
'authorId': '123',
}
【问题讨论】:
-
我也有同样的问题。它在本地工作正常,但在实时服务器中显示在解析器错误中找不到帖子。 .架构类型为 User{ id:Int firstname:string lastname:string post:Post }
标签: python graphql graphene-python