I have 3 files: authors.py
, posts.py
and schema.py
.
Posts have one Author and the Query is built in the schema file.
I'm trying to resolve Author
from inside Post
without declaring a resolver function in Post
, since Author
already has a resolver function for itself declared. The following code works, but I have to reference resolve_author
from inside the Post
type and it doesn't seem right. I think Graphene should pass the parent
param directly to Author
, no?
If I don't set a resolver for author
in the Post
type, it simply returns 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',
}