Netflix's GraphQL for Spring Boot
Written by Nikos Vaggalis   
Wednesday, 10 March 2021

Netflix has open sourced its DGS Framework (Domain Graph Service) GraphQL server framework for Spring Boot. Starting out as a tool internal to the corporation, it has been generously open sourced for the rest of us to enjoy.

Netflix is one of those organizations that have gone beyond REST, embracing GraphQL instead. Rather than exposing a myriad of microservices to UI developers, Netflix opted for a unified API aggregation layer at the edge, powered by GraphQL. Since they also use Spring Boot for their infrastructure, merging was bound to happen.

As such the DGS framework is built on top of graphql-java and in essence it offers a layer of abstraction over the library's low-level details. While exclusively written in Kotlin (requires Kotlin 1.4), the framework mainly targets Java as Java is most closely associated with Spring Boot.That said, you are free to write your code in Kotlin too.

Integrating the library is easy since it too utilizes Spring Boot’s annotation-based model. For example in implementing a Data Fetcher (constructs which return the data for a query) you use the following annotation based snippet :

@DgsComponent
public class ShowsDatafetcher {

private final List<Show> shows = List.of(
  new Show("Stranger Things", 2016),
  new Show("Ozark", 2017),
  new Show("The Crown", 2016),
  new Show("Dead to Me", 2019),
  new Show("Orange is the New Black", 2013)
);

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

 

Apart from that, it comes with a host of other features: 

  • Annotation based Spring Boot programming model
  • Test framework for writing query tests as unit tests
  • Gradle Code Generation plugin to create types from schema
  • Easy integration with GraphQL Federation
  • Integration with Spring Security
  • GraphQL subscriptions (WebSockets and SSE)
  • File uploads
  • Error handling
  • Many extension points 

It's pretty easy to setup. Just add the reference to the library com.netflix.graphql.dgs:graphql-dgs-spring-boot-starter and let it consume your GraphQL schema file as DGS is designed for schema-first development.

To generate each GraphQL type for each type described in the schema, as well as to generate the Data Fetchers, the DSG codegen plugin must be included in the project :

plugins {
  id("com.netflix.dgs.codegen") version "4.0.12"
}

This works according to the mapping rules so, for example, the basic scalar types are mapped to corresponding Java/Kotlin types (String, Integer etc.), whereas the date and time types  are mapped to corresponding java.time classes.

The code generator can also create the client API classes which you can use to query data from a GraphQL endpoints using Java.

This is another testament to Spring Boot's versatility for your backend development; there are just so many integration options, something I found out first hand when graduating from the Java Web Developer Nanodegree. (See my Insider's Guide here). Now it can do GraphQL with ease too.

More Information

DGS Github
DGS Main 

Related Articles

The Insider's Guide to the Java Web Developer Nanodegree

Learn How To Do Java On Azure

Foojay - All About Java and the OpenJDK

Introducing The Android Kotlin Developer Nanodegree

 

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner


Microsoft Introduces .NET Smart Components
01/04/2024

Microsoft has provided a set of .NET Smart Components, described as a set of genuinely useful AI-powered UI components that you can quickly and easily add to .NET apps. The components are prebuilt end [ ... ]



Redis Changes License, Rival Fork Launched
03/04/2024

The developers of Redis have announced that they are changing the licensing model for the database. From now on, all future versions of Redis will be released with source-available licenses rather tha [ ... ]


More News

raspberry pi books

 

Comments




or email your comment to: comments@i-programmer.info

Last Updated ( Wednesday, 10 March 2021 )