The website content provides a technical guide on using GraphQL interfaces and unions within a Spring Boot application, implemented with Kotlin.
Abstract
The article titled "Using GraphQL With Spring Boot: Interfaces and Unions" delves into the advanced concepts of GraphQL, specifically interfaces and unions, and their practical implementation in a Spring Boot application using Kotlin. It begins with an explanation of GraphQL interfaces and unions, then demonstrates how to model these in a GraphQL schema. The author guides readers through setting up a Spring Boot project, integrating necessary GraphQL libraries, and creating a schema with interfaces or unions. The article also covers the creation of resolvers and services to handle queries and provides insights into when to use interfaces versus unions, emphasizing that the choice depends on the specific use case and desired outcomes. The tutorial concludes with troubleshooting common issues and directs readers to additional resources for further learning.
Opinions
The author suggests that interfaces are beneficial when types share a commonality and should be used in a way that aligns with their fundamental purpose.
Unions are recommended for documenting and enforcing distinct behaviors for different types, ensuring clients understand how to handle each type.
The choice between interfaces and unions is not one-size-fits-all; it should be based on the specific requirements and goals of the implementation.
The author provides a personal experience and insights into using GraphQL, indicating a preference for its flexibility and efficiency in handling multiple types within a single query.
There is an acknowledgment of a limitation in the GraphQL library concerning the autodiscovery of interface types, along with a practical workaround to address the issue.
Using GraphQL With Spring Boot: Interfaces and Unions
GraphQL’s interfaces and unions provide a good way to handle multiple field types in queries
Illustration of GraphQL with Spring Boot
First and Foremost
I’ll start with the explanation of the concepts, and then we’ll look into its implementation with Spring Boot and Kotlin.
Consider the following data classes and interface.
Now we’re going to write a GraphQL schema that models the same interfaces and classes. We’ll have a query to fetch the Profile, which will give us a User or Company based on what the client needs in a single query.
In our GraphQL schema, when using interfaces, we’ll add the following.
We can also use Union instead of Interface.
Which one should we use?
It can vary based on the use case and your end goal. The following points can be used to finalize the option.
There isn’t always an advantage to grouping shared fields into interfaces — it depends on the use case
Interfaces are good for when the types have a fundamental commonality in how they should be used
Unions are good for documenting and forcing the client to understand how different types should be treated
Implementation With Spring Boot
1. The setup
You can skip to the third section if you know how to set up a Spring Boot project with GraphQL. Also, the code to this is available here.
I’m not adding the implementation for Union here, as it’s pretty similar to the implementation of interfaces. Don’t worry: If you’re interested, you can find the implementation here.
I’m glad you made it to the end. It means you’re interested in implementing GraphQL. Here is another article in which I share my experience of using GraphQL for six months.