Tomek Poniatowicz
3/30/2021
GraphQL community is full of great stories about how GraphQL implementation changed a product, made it better and more powerful. Sometimes GraphQL adoption generates a need for innovation. Just like GraphQL itself, which was created inside Facebook and later publicly released, many companies decide to opensource their GraphQL implementations and make them available to the public, seeing how much it can give to the developer's community. This is one of those stories.
In 2019 Netflix made a decision to implement a federated GraphQL architecture aiming to:
For a company that has standardized on Spring Boot for backend development, the transition to the new architecture meant a challenge of adopting GraphQL for many different backend teams across Netflix's Java ecosystem. To call this operation successful it was obvious that teams that were forced to implement GraphQL must have been provided a great developer experience for GraphQL in Spring Boot. That's the reason why Domain Graph Service was brought to life.
Domain Graph Service (DGS) is a framework created internally at Netlfix that simplifies the implementation of standalone and federated GraphQL services for Spring Boot. The framework is built on top of graphql-java. Despite most of the DGS's code is written in Kotlin it's primarily designed to be used with Java and its key features include things like:
Realizing how much of a deal it was for their developers, Netflix decided to open-source the framework and build a community around it in 2020 (Netflix is using the same OSS components!).
Using the DGS is very simple. The foundation of the framework is structured around the annotation-based programming model well-known to Spring Boot developers. Let's take a look at an example provided by the team working on this project:
First of all, you need to define a GraphQL schema, a simple one like this would work for an example:
type Query {
shows(titleFilter: String): [Show]
}
type Show {
title: String
releaseYear: Int
}
Once your schema is defined the next step is to implement a fetcher and ... that's it. This is enough to get your GraphQL endpoint running!
@DgsComponent
public class ShowsDatafetcher {
private final List<Show> shows =
List.of(
new Show("Stranger Things", 2016),
new Show("Ozark", 2017)
);
@DgsData(parentType = "Query", field = "shows")
public List<Show> shows(@InputArgument("titleFilter") String titleFilter) {
if(titleFilter == null) {
return shows;
}
return shows.stream()
.filter(s -> s.getTitle().contains(titleFilter))
.collect(Collectors.toList());
}
}
If you would like to try the DGS framework make sure to check out its official documentation and guides or jump straight into the DGS repo on GitHub.