Graphql return None when no values are found

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?

I think the easiest way for you to distinguish the type each is relevant for is to query for __typename as well

    """  query Odds($id: ID!) {
            odds(id: $id) {
                oddsMarkets {
                    oddsOptions {
                       __typename
                        ... on OddsToWin {
                            odds
                        }
                        ... on OddsFinishes {
                            odds
                        }
                        ... on OddsCutsPlayers {
                            odds
                        }
                        ... on OddsLeadersPlayers {
                            odds
                        }
                    }
                }
            }
        } 
   """

This will return to you

{
  'odds': {
    'oddsMarkets': [
      {
        'oddsOptions': [
          {
            '__typename': 'OddsToWin"
            'odds': '+1200'
          }
        ]
      }
    ]
  }
}

If you want the array to always return all options, you should be able to do that simply in your resolver with your own logic. But I don’t think there is a graphql schema way to force it to do that.

1 Like