Domain Graph Service (DGS) - open-source GraphQL framework for Spring Boot by Netflix

tomekponiat

Tomek Poniatowicz

Posted on March 30, 2021

Domain Graph Service (DGS) - open-source GraphQL framework for Spring Boot by Netflix

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.

How it started

In 2019 Netflix made a decision to implement a federated GraphQL architecture aiming to:

  • provide a unified API for consumers,
  • giving their backend developers flexibility,
  • preserving service isolation.

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

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:

  • Annotation-based Spring Boot programming model,
  • Integration with Spring Security,
  • Gradle Code Generation plugin to create Java/Kotlin types from a GraphQL schema,
  • A GraphQL client for Java,
  • Error handling,
  • Easy integration with GraphQL Federation,
  • GraphQL subscriptions (WebSockets and SSE),
  • Automatic support for interface/union types.

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!).

DGS Framework with Netflix and OSS modules

Source: netflixtechblog.com

Getting started

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
}
Enter fullscreen mode Exit fullscreen mode

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());
    }
}
Enter fullscreen mode Exit fullscreen mode
Source: netflixtechblog.com

Try it out

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.


Speed up your GraphQL API development

GraphQL Editor is a supportive tool for both advanced GraphQL users as well as those taking their first steps with GraphQL APIs. Our all-in-one development environment for GraphQL will help you build, manage & deploy your GraphQL API much faster. Try GraphQL Editor for free!

New features of GraphQL Editor gif

💖 💪 🙅 🚩
tomekponiat
Tomek Poniatowicz

Posted on March 30, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related