GraphQL 在找不到数值时返回 None。

huangapple go评论56阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年2月24日 02:26:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548870.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定