GraphQL Using Netflix’s DGS Framework & Spring-Boot (Schema-First Approach)

Introduction

DGS stands for Domain Graph Service. In this article, we will learn how to implement GraphQL APIs using Netflix’s DGS & spring-boot framework.

Tool(s)/Framework(s)

Editor — IntelliJ Idea/Eclipse
Language — Java 8 or above
Framework — Spring boot
GraphQL Framework — Netflix’s DGS
Communication medium — GraphQL
Image Build Tool — Docker

GIT Repo -

Maven Dependency

Netflix DGS Framework — Maven Dependency

Configuration

No specific configuration is required as far as application.yml is concerned.

By default, DGS framework is looking for the schema under “resources/schema” directory. So, we will have to make sure that all our schemas are under “resources/schema” directory.

However, if we want to change the defaults, then the good news is, we can :)

dgs.graphql.schema-json.path — Path to the schema-json endpoint without trailing slash, by default, it will scan schema under “resources/schema” directory

dgs.graphql.graphiql.path — Path to the GraphiQL UI endpoint without trailing slash, by default, it’s “/graphiql”

Model

Now, let’s define a simple “User” model & we will implement retrieve/filter operations using the Netflix DGS framework’s GraphQL schema-first approach.

@Data
@Builder
public class User {
private String name;
private Integer id;
}

GraphQL Schema

Now, as we have defined the model, let’s create a GraphQL schema for the “User” model.

Let’s create a file named “user.graphqls” under “resources/schema” directory & define the schema of type “User”.

Here, we can see that type “User” has two attributes “name” & “id” which we have already defined in our “User” model.

type User {
name: String
id: Int
}

Now, let’s define query type in the same schema file.

type Query {
users(nameFilter: String): [User]
}

Here, we have defined the “users” query along with the optional “nameFilter” of type “String”.

if “nameFilter” criteria is provided — It means that if we will provide “nameFilter” then our query result would return only those records which contain the filter criteria

if “nameFilter” criteria is not provided — As per our implementation, if there is no criteria provided then it will return all users.

Implementation

Here, in this section, we will try to understand a few important annotations which will help us to provide GraphQL API endpoints.

@DgsQuery — This signifies that the given data fetcher is of type query which we can use to retrieve records.

For Ex:

@DgsQuery
public List<User> users(@InputArgument String nameFilter) {
if (nameFilter == null) {
return users;
}
return users.stream().filter(s -> s.getName().contains(nameFilter)).collect(Collectors.toList());
}

@DgsMutation— This signifies that the given data fetcher is of type mutation which we can use for insert/update/delete records.

For Ex:

@DgsMutation
public boolean deleteUser(@InputArgument Integer id) {
boolean isUserRemoved = Boolean.FALSE;
if(users.stream().filter(s -> s.getId() == id ).count() == 0)
return isUserRemoved;
isUserRemoved = users.remove(id);
return isUserRemoved;
}

@DgsComponent — This signifies that the data fetcher is a spring component & will contain the DGS queries & mutations.

Here in the below example, we have defined the spring DGS component using @DgsComponent.

For Ex:

@DgsComponent
public class UserDataFetcher {

The most important point to note here is that, @DgsQuery & @DgsMutation must be a part of @DgsComponent.

Testing

Yey, as we have gone through the required important concepts regarding the Netflix DGS framework, please check out the code from the git repo & run the spring boot application.

Once the application is up, please hit the below URL which will open the GraphQL UI to run queries/mutations.

GraphQL UI

Once the UI is up, please run the query to get user records as given below. This query will return all users since we have not used “nameFilter”.

GraphQL Query Without Using Filter

Now, let’s run the same query using “nameFilter” as given below.

Here, we have used “nameFilter” for which value is “A” hence it will return all the user’s data that contains “A” in the user’s name.

GraphQL Query Using Filter

Key Takeaways

There are many advantages of using Netflix’s DGS framework but to point out a few,

→ It is annotation driven & easy to use

→ It supports file upload as well.

→ It provides the code generation plugin which helps to generate java/kotlin types from schema.

→ very easy to integrate with GraphQL Federation

→ It also provides support for spring reactive programing using flux.

References

https://netflix.github.io/dgs/

--

--

--

AWS Certified Solutions Architect | Cloud Enabler/Enthusiast | Spring Boot | Docker | Kubernetes | Microservices | Java/J2EE | Terraform | AML | Banking

Love podcasts or audiobooks? Learn on the go with our new app.

Hammer Throw Area Installation in Norfolk #Hammer #Throwing #Area #Construction #Norfolk https://t.c

Considering the sheer number of people who lost their lives to the Spanish Flu, it’s amazing that…

Android Activity Lifecycle for dummies

All Activity states

Python Basics for Data Science — I

Why code reviews should be top of mind for all engineering teams?

Avoiding Coding Pitfalls with Performance Signatures

Transcend the “Feature Factory” Mindset Using Modern Agile and OKR

Flutter Firebase tutorial for beginners #5 - Stream & Provider in Flutter

Flutter & Firebase App Tutorial — Lumei Digital

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Apurv Sheth

Apurv Sheth

AWS Certified Solutions Architect | Cloud Enabler/Enthusiast | Spring Boot | Docker | Kubernetes | Microservices | Java/J2EE | Terraform | AML | Banking

More from Medium

Building Microservices with Spring Boot, Spring Cloud Netflix and Spring Cloud Sleuth

SpringBoot: Generate OpenAPI Document During Test Phase

Here’s the code to use Spring Cloud Config and centralize your Spring applications

Spring Cloud Config

Validate Request Body and Parameter in Spring Boot