Log in to GraphQL Editor
Open-source GraphQL framework for Spring Boot by Netflix
Tomek

Tomek Poniatowicz

3/30/2021

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

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
}

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());
    }
}

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.

Check out our other blogposts

What can GraphQL do for your API?
Carl Matte
Carl Matte
What can GraphQL do for your API?
3 min read
almost 4 years ago
GraphQL Mesh - a new library from the Guild
Tomek Poniatowicz
Tomek Poniatowicz
GraphQL Mesh - a new library from the Guild
2 min read
almost 4 years ago
Software licensing cheat sheet
Michał Tyszkiewicz
Michał Tyszkiewicz
Software licensing cheat sheet
4 min read
almost 4 years ago

Ready for take-off?

Elevate your work with our editor that combines world-class visual graph, documentation and API console

Get Started with GraphQL Editor