问题
I have successfully created an all graphene query that responds to
query {
person (id: "Mary") {
id
name
}
}
I now want to extend this to be able to loop through all people and return similar data for each.
query {
people {
count
allPersons {
name
}
}
}
How do I get the resolve_allPersons
resolver in people
to call the person
resolver for each person?
回答1:
Second query you've described can be done with custom type, for example:
class AllPeopleType(graphene.ObjectType):
count = graphene.Int()
all_persons = graphene.List(YourPersonType)
def resolve_count(self, info, **kwargs):
# assumed that django used on backend
return Person.objects.count()
def resolve_all_persons(self, info, **kwargs):
return Person.objects.all()
and query:
class YourQuery(object):
# person = ...
people = graphene.Field(AllPeopleType)
def resolve_people(self, info):
return AllPeopleType()
来源:https://stackoverflow.com/questions/48748236/setting-up-a-plain-graphene-nested-query