GraphQL Using Netflix’s DGS Framework & Spring-Boot (Schema-First Approach)
Tags: spring, graphql, springboot, netflix
Published: 2022-06-16 17:10:07

---
title: GraphQL Using Netflix’s DGS Framework & Spring-Boot (Schema-First Approach)
published: true
date: 2022-04-26 17:18:48 UTC
tags: spring,graphql,springboot,netflix
canonical_url: 
---

### Introduction

DGS stands for **D** omain **G** raph **S** ervice. 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 -

[spring-boot/graphql-netflix-dgs-example at main · shethapurv/spring-boot](https://github.com/shethapurv/spring-boot/tree/main/graphql-netflix-dgs-example)

### Maven Dependency

![](https://cdn-images-1.medium.com/max/596/1*47YaxQ1InujUfarRPabTnw.png)
_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](http://twitter.com/Data)  
> [@Builder](http://twitter.com/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](https://github.com/shethapurv/spring-boot/tree/main/graphql-netflix-dgs-example) & 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.

![](https://cdn-images-1.medium.com/max/1024/1*El-4CfomgppN0ZQUYPHOjg.png)
_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”.

![](https://cdn-images-1.medium.com/max/1024/1*SzGG_tTLxxeJ5UnRIJRzuw.png)
_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.

![](https://cdn-images-1.medium.com/max/1024/1*UZGbP2m2iSODrzbsdFetgQ.png)
_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/](https://netflix.github.io/dgs/)