Harnessing the power of interfaces
In the GraphQL language, one of the most interesting features is interfaces. Interfaces in GraphQL are designed to help in two main situations:
- They serve as a blueprint for a set of fields that multiple types can implement. By defining an interface, we can ensure that certain fields and their types are present across different object types.
- If you use an interface as a field type, it means that it can return all the types that implement that interface. Moreover, when a type implements an interface, it means that it provides the fields specified by that interface. This allows us to query for objects of different types using a single field.
Let’s look at an example. Assuming our library has books and board games available to be borrowed, we will create an interface that brings them together into one collection. Here, you can see how you can return objects of different types in one query:
type Book implements LibraryItem...