Dealing with Null Values in GraphQL API Response

38 views Asked by At

I recently got an interview task where I was required to develop a React application using a GraphQL API. The API in question is the SpaceX GraphQL API. As part of the task, I needed to retrieve specific fields such as mission name, mission patch image, launch date, and success status (boolean) for each launch.

But upon querying the API, I encountered a challenge where certain fields, notably 'launch_success' and 'mission_patch', were returning null values. After further exploration, I realized that this issue extended to other fields as well.

I've provided an example query below:

query Launches {
  launches {
    mission_name
    rocket {
      rocket_name
    }
    mission_id
    launch_success
  }
}

In this query, the launch_success field always returns null values, but this is not true as when I used REST API there were 80% successful launches. As I'm relatively new to GraphQL, I'm uncertain about the causes of this issue and how to fix it.

Could someone offer any insights into why certain fields return null values and suggest potential troubleshooting steps or fixes? I would appreciate any guidance!

P.S I've already tried the same with REST API, and was able to fetch all values with no issues (interviewer provided both solutions, but I need to accomplish it with GraphQL)

Used GraphQL Playground: https://studio.apollographql.com/public/SpaceX-pxxbxen/variant/current/explorer

I attempted several strategies to retrieve the required data from the GraphQL API, including querying different fields and utilizing various query structures. Specifically, I tried querying launch_success from different endpoints and explored alternative fields that might provide similar information. However, in each attempt, the launch_success field always returned null values.

1

There are 1 answers

3
adsy On BEST ANSWER

There is nothing wrong at all with your GraphQL query. In GQL, it's not the case that selecting any other field can affect another. If it's null, it's null from the server's understanding.

I'm fairly confident you have found a bug in the SpaceX GQL API. The GQL schema does not declare this field (or many, if any at all actually) as non-nullable, meaning your application code does need to handle if those fields are null. However, why it is null when you have verified the data exists by other means can only be an issue on the SpaceX side.

This is also easily demonstrated, even without the smoking gun of having the data there in the REST API, if you grab the GQL details field. You then get results like:

{
    "details": "Engine failure at 33 seconds and loss of vehicle",
    "launch_date_utc": "2006-03-24T22:30:00.000Z",
    "launch_success": null
},

Which clearly shows missing data. That launch failed. This is consistent so it's reasonable to assume that there is an API issue. Coupled with the evidence about the REST API it is pretty much conclusive.

I would still build the front end code to handle all three possible values: null ("Unknown"), true ("Success") and false ("Failed"). Even if it never triggers the latter two with the current data sets. That would ensure you've done your job and the code would work and handle all cases if the bug was fixed on their end.