【问题标题】:how to use a list of objects as query in graphene?如何在石墨烯中使用对象列表作为查询?
【发布时间】:2020-11-24 15:29:18
【问题描述】:

我有一个从第三方 api 获得的对象列表:

heroes : [
    {
        "id": 1,
        "name": "James"
    },
    {
        "id": 2,
        "name": "Monk"
    }
]

这是我觉得我很难过的地方 我可以创建一个字典来加载一个数据但是如果我有多个对象怎么办?

def get_hero(heroes):
   hero = {}
   hero['hero_id'] = heroes[0]['id']
   hero['hero_name'] = heroes[0]['name']
   return hero

石墨烯相关:

   class Hero(ObjectType):
       hero_id = Int()
       hero_name = String()

   
   class Query(ObjectType):
       hero_search = Field(Hero, hero_id=String())

       def resolve_hero_search(self, info, **kwargs):
          return get_hero(heroes=kwargs) # this is a just a placeholder cause I think there is no issue here

石墨烯中的查询工作正常,但如果只包含一个数据,但我不确定是否有多个对象。

【问题讨论】:

  • 不清楚您到底想做什么?你能解释清楚吗?

标签: python-3.x graphene-python


【解决方案1】:

您需要从输入中获取 ID/字符串列表:

hero_search = Field(Hero, hero_id=graphene.List(graphene.String))

【讨论】:

    猜你喜欢
    • 2020-07-27
    • 2018-07-30
    • 2020-09-03
    • 1970-01-01
    • 2019-12-20
    • 2020-11-30
    • 2021-07-08
    • 2020-11-25
    • 2018-11-27
    相关资源
    最近更新 更多