我有三个文件:authors.py
,posts.py
和schema.py
。
每篇文章都有一个作者,并且查询是在模式文件中构建的。
我正在尝试从Post
内部解析 Author
,而不必在Post
中声明一个解析器函数,因为Author
已经有了一个自己声明的解析器函数。下面的代码可以工作,但是我必须从Post
类型中引用resolve_author
,这似乎不正确。我认为Graphene应该直接将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)
作者.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',
}