Build a GraphQL API with Netflix DGS and Spring Boot

Learn how to quickly set up a GraphQL API using Netflix's open‑source DGS framework in a Spring Boot project, covering Maven dependencies, schema definition, data fetcher implementation, UI debugging with GraphiQL, and example curl and Java test calls.

Java Architecture Diary
Java Architecture Diary
Java Architecture Diary
Build a GraphQL API with Netflix DGS and Spring Boot

Introduction

Netflix has open‑sourced its Domain Graph Service (DGS) framework, which simplifies the integration of GraphQL in Java applications.

Why use GraphQL?

GraphQL serves as a flexible data interface between front‑end and back‑end, allowing clients to request only the fields they need without modifying existing controller or service code.

Component Dependency

<dependency>
    <groupId>com.netflix.graphql.dgs</groupId>
    <artifactId>graphql-dgs-spring-boot-starter</artifactId>
    <version>3.5.1</version>
</dependency>

Note: DGS artifacts are hosted on JCenter; you may need to add the repository configuration.

<profiles>
  <profile>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
    <repositories>
      <repository>
        <snapshots><enabled>false</enabled></snapshots>
        <id>central</id>
        <name>bintray</name>
        <url>https://jcenter.bintray.com</url>
      </repository>
    </repositories>
    <pluginRepositories>
      <pluginRepository>
        <snapshots><enabled>false</enabled></snapshots>
        <id>central</id>
        <name>bintray-plugins</name>
        <url>https://jcenter.bintray.com</url>
      </pluginRepository>
    </pluginRepositories>
    <id>bintray</id>
  </profile>
</profiles>

Define the GraphQL Schema

Create src/main/resources/schema/schema.graphqls with the following content:

type Query {
    shows(title: String, releaseYear: Int): [Show]
}

type Show {
    title: String
    releaseYear: Int
}

Data Fetcher Implementation

@DgsComponent
public class ShowsDatafetcher {

    @DgsData(parentType = "Query", field = "shows")
    public List<Show> shows(@InputArgument("title") String title,
                             @InputArgument("releaseYear") Integer releaseYear) {
        if (title == null) {
            return shows;
        }
        return shows.stream()
                    .filter(s -> s.getTitle().contains(title))
                    .collect(Collectors.toList());
    }

    // Simulated DB
    private final List<Show> shows = List.of(
        new Show("java", 1995),
        new Show("php", 1995),
        new Show("python", 1990),
        new Show("golang", 2009),
        new Show("rust", 2015)
    );
}

UI Debugging with GraphiQL

Open http://localhost:8080/graphiql in a browser to explore the API.

Example of a conditional query:

Calling the API

curl --location --request POST 'http://localhost:8080/graphql' \
--header 'Content-Type: application/json' \
--data-raw '{"query":"{
  shows(title: \"java\", releaseYear: 1995) {
    title
    releaseYear
  }
}
","variables":null}'

Java Test Example

@SpringBootTest(classes = {DgsAutoConfiguration.class, ShowsDatafetcher.class})
class ShowsDatafetcherTests {

    @Autowired
    DgsQueryExecutor dgsQueryExecutor;

    @Test
    void shows() {
        List<String> titles = dgsQueryExecutor.executeAndExtractJsonPath(
            " { shows { title releaseYear }}",
            "data.shows[*].title");
        assertThat(titles).contains("java");
    }
}

Source Code

Repository: https://github.com/lltx/dgs-demo

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

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaSpring BootAPITutorialGraphQLNetflix DGS
Java Architecture Diary
Written by

Java Architecture Diary

Committed to sharing original, high‑quality technical articles; no fluff or promotional content.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.