英文:
Graphql return None when no values are found
问题
查询赔率($id: ID!) {
赔率(id: $id) {
赔率市场 {
赔率选项 {
... 在赔率获胜时 {
赔率
}
... 在赔率完成时 {
赔率
}
... 在赔率削减球员时 {
赔率
}
... 在赔率领先球员时 {
赔率
}
}
}
}
}
英文:
I have the following query:
""" query Odds($id: ID!) {
odds(id: $id) {
oddsMarkets {
oddsOptions {
... on OddsToWin {
odds
}
... on OddsFinishes {
odds
}
... on OddsCutsPlayers {
odds
}
... on OddsLeadersPlayers {
odds
}
}
}
}
}
"""
I was wondering if its possible to return all the desired values even if they are non-existent (return them as None
). At the moment I am getting the following dictionary returned:
{
'odds': {
'oddsMarkets': [
{
'oddsOptions': [
{
'odds': '+1200'
}
]
}
]
}
}
As you can see +1200
is located in a list. By doing research I know that this value corresponds to OddsToWin
. But if I don't know this in advance, how do I know if odds
represents the odds of winning, finishing, cutting or becoming the leader?
Let's say the server only returns the odds
from OddsLeadersPlayers
. It will also show up
in oddsOptions[0]
right?
Is it possible to retrieve all odds values even if they are null / None or is there another way to make it more clear?
答案1
得分: 1
更好的方法是使用别名:
query Odds($id: ID!) {
odds(id: $id) {
oddsMarkets {
oddsOptions {
... on OddsToWin {
odds2w: odds
}
... on OddsFinishes {
oddsF: odds
}
... on OddsCutsPlayers {
oddsCP: odds
}
... on OddsLeadersPlayers {
oddsLP: odds
}
}
}
}
}
请注意,与JS不同,别名的名称位于原始名称之前。
英文:
A better way is to use aliases:
query Odds($id: ID!) {
odds(id: $id) {
oddsMarkets {
oddsOptions {
... on OddsToWin {
odds2w:odds
}
... on OddsFinishes {
oddsF:odds
}
... on OddsCutsPlayers {
oddsCP:odds
}
... on OddsLeadersPlayers {
oddsLP:odds
}
}
}
}
}
Note that unlike in JS, the alias name comes before the original name.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论