Apollo GraphQL conflicting types example

Posted by Christian Noack on 2020-11-15
Words 213 and Reading Time 1 Minutes
Viewed Times

Imagine we have a book store service whose only functionality is to provide a list of books with their attributes depending on a query. We have a very simple Book model here just containing:

  • an ID
  • a name
  • a review (which represents the last review given to the book)

With Apollo GraphQL you can query for books:

query { books { name review { value } } }

Now we want to change the functionality: a review can either be a

  • Poll Review that contains a float value, or a
  • Web Review_that contains a string value and the name of the user who wrote the review.

We can imagine other types also. This should be just a simple example of return values that might have different types in the same slot/attribute. This might happen when you want to visualize generic data coming from a backend service. Here we have a clash between dynamic types from the backend and static types in the Apollo API.

If you query for books you then should either get a PollReview or a WebReview inside the review slot of the Book. Both should contain a value attribute inside the review.

There are three working solutions for this situation. You’ll find a detailed explanation of the problem and all solutions on my Github.