Setting up a plain graphene nested query

主宰稳场 提交于 2021-01-27 23:03:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!