GraphQL with Netflix dgs-framework

Starting the year with some back-to-basics hands-on coding using small building blocks in areas that I randomly get interested in. This time using the Netflix DGS GraphQL framework. DGS (Domain Graph Service) is a GraphQL server framework for Spring Boot.

There is a ton of material out there to learn GraphQL. The best description I find (IMO) is from https://graphql.org/

GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data.

It provides an alternative to the standard RESTful API design option. It is an option optimized for mobile dev, but of course, it can be used in any type of architecture. Rather than calling multiple RESTful APIs to serve your UI, you can call one GraphQL endpoint and specify precisely the objects you are interested in and which attributes you would like back. It is left to the GraphQL server runtime to orchestrate the attributes’ retrieval. While this is no excuse to create crappy RESTful APIs and put GraphQL on top, it is an option that helps optimize the delivery of data that each consumer needs.

There are multiple frameworks in the Java space to expose GraphQL servers and serve up such content. We will look at (with code) is DGS from Netflix on Spring Boot.

Clone the code repo is at https://github.com/thomasma/dgs-boot-gql

Run the server (and service) using

Once the app starts successfully, you should get to the query at http://localhost:8080/graphiql . This will give you access to the GraphQL IDE where you can explore the types and fields that are available to retrieve vs a list of REST API endpoints. You can explore the type model exposed, read the API documentation and execute queries & mutations. In GraphQL world, you use a Query to retrieve data and Mutation to change data.

The code in the Git repo is based on a simple API exposed by the Crypto exchange Binance to retrieve the last 24hr stats on 100s of Cryptos. Our service will allow you to retrieve all crypto data (only fields I am interested in), add certain cryptos to my watch list, retrieve the watch list, retrieve crypto stats for my watch list or just ask for specific crypto stats.

The DGS framework allows for all of this with a simple developer framework and a runtime that exposes a GraphQL server on Spring Boot. The IDE experience should be something like this.

Code blocks to review…(see github, cause I am not repeating them here) in src/ folder.

  1. resources/schema/schema.graphql for the types, mutations, and queries we exposed in this simple getting started example.
  2. CryptoTickerDataFetcher.java where you can see all the queries we expose implemented and calling run-of-the-mill Spring components. Note the use of @DgsComponent and @DgsQuery annotations. By default, the name of the method should map to the Query name in the schema. If the names differ then you need to send in a field attribute to the @DgsQuery
  3. CryptoWatchListMutation.java to review Mutation code. Note the use of @DgsComponent and @DgsMutation annotations.

Hope this gives you a quick way to get started with DGS and the rest you should pick from DGS website.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.