Log in to GraphQL EditorGet started
Open-source GraphQL framework for Spring Boot by Netflix

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

Unlocking the Power of React 19
Tomasz Gajda
Tomasz Gajda
Unlocking the Power of React 19
1 min read
about 1 month ago
Zeus update - GraphQL spread operator
Michał Tyszkiewicz
Michał Tyszkiewicz
Zeus update - GraphQL spread operator
1 min read
3 months ago
What is GraphQL Zeus?
Michał Tyszkiewicz
Michał Tyszkiewicz
What is GraphQL Zeus?
1 min read
3 months 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