[英]how to return a list of dictionary in graphene?
I am new on graphql and working with some datasets that is returned as a list of dictionaries.我是 graphql 的新手,并且正在使用一些作为字典列表返回的数据集。
snippet code:片段代码:
class Player(ObjectType):
username = String()
role = String()
class Game(ObjectType):
players = List(Player)
I'm wondering why below code doesn't work?我想知道为什么下面的代码不起作用?
class Query(ObjectType):
game_info = Field(Game, username=String(), role=String())
def resolve_game_info(self, info):
results = [{
"username":"Malphite",
"role":"tank"
},
{
"username":"Teemo",
"role":"support"
}]
output = []
for res in results:
output.append(
Player(
username=res['username'],
role=res['role']
)
)
return output
How I query in graphql:我如何在 graphql 中查询:
query {
game_info(username:"Teemo") {
players {
username
role
}
}
}
Results to like this:结果如下:
{
"data": {
"gameInfo": null
}
}
Any help would this would be greatly appreciated!任何帮助将不胜感激!
The problem seems in format of returned data.问题似乎在于返回数据的格式。 Suppose, you have more fields in your
Game
, not only players
.假设,您的
Game
中有更多字段,而不仅仅是players
。 There is no way of including those fields in your return format.无法将这些字段包含在您的返回格式中。
Instead of return output
.而不是
return output
。
Try: return {'players':output}
尝试:
return {'players':output}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.