Implement GraphQL with Spring Boot

What is GraphQL?

Please refer to REST Issues and GraphQL.

environment

Add dependent libraries

Add the following:

pom.xml


    <!-- https://mvnrepository.com/artifact/com.graphql-java/graphql-java -->
    <dependency>
      <groupId>com.graphql-java</groupId>
      <artifactId>graphql-java</artifactId>
      <version>11.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.graphql-java/graphql-spring-boot-starter -->
    <dependency>
      <groupId>com.graphql-java</groupId>
      <artifactId>graphql-spring-boot-starter</artifactId>
      <version>5.0.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.graphql-java/graphql-java-tools -->
    <dependency>
      <groupId>com.graphql-java</groupId>
      <artifactId>graphql-java-tools</artifactId>
      <version>5.2.4</version>
    </dependency>

Schema definition

For the schema and types, please refer to GraphQL Schema and Type Definition.

schema.graphqls


type Book {
    id: ID
    name: String
    pageCount: Int
    author: Author
}

type Author {
    id: ID
    firstName: String
    lastName: String
}

type Query {
    bookById(id: ID): Book
}

Creating a Type class

Create a Java class (a class that holds data obtained from some data source) that corresponds to the type defined in the schema definition.

Book.java



public class Book {

    private String id;
    private String name;
    private int pageCount;
    private Author author;

    public void setId(String id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }

    public void setAuthor(Author author) {
        this.author = author;
    }
}

Author.java


public class Author {

    private String id;
    private String firstName;
    private String lastName;

    public void setId(String id) {
        this.id = id;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

Creating a Resolver

As mentioned in GraphQL Schema and Type Definition, the schema definition defines the queries and various types that the client can operate. However, the schema is just a definition and does not actually manipulate the data. The resolver is the one that actually manipulates the data.

BookResolver.java


@Component
public class BookResolver implements GraphQLQueryResolver {
	
	public Book bookById(String bookId) {
		//Actually, in most cases, data is read from some data store and returned, but here a dummy value is returned.
		Book book = new Book();
		book.setId(bookId);
		book.setName("bookName");
		book.setPageCount(900);
		Author author = new Author();
		author.setId("0001");
		author.setFirstName("fName");
		author.setLastName("lName");
		book.setAuthor(author);
		return book;
	}
}


Run

To execute GraphQL, use tools such as GraphQL and GraphQL Playground, as introduced in Useful GraphQL Tools. This time, we will use the desktop version of GraphQL Playground.

query {
  bookById(id:1) {
    id
    name
  }
}

If you get the following response, you are successful.

{
  "data": {
    "bookById": {
      "id": "1",
      "name": "bookName"
    }
  }
}

Let's increase the acquisition items. Now write the following query and press the play button.

query {
  bookById(id:1) {
    id
    name
    author {
      id
      firstName
      lastName
    }
  }
}

If you get the following response, you are successful. It is possible to dynamically change the items you want to get without changing the source.

{
  "data": {
    "bookById": {
      "id": "1",
      "name": "bookName",
      "author": {
        "id": "0001",
        "firstName": "fName",
        "lastName": "lName"
      }
    }
  }
}

that's all.

reference

Recommended Posts

Implement GraphQL with Spring Boot
I tried GraphQL with Spring Boot
Implement CRUD with Spring Boot + Thymeleaf + MySQL
Implement paging function with Spring Boot + Thymeleaf
Download with Spring Boot
Try to implement login function with Spring Boot
Generate barcode with Spring Boot
Hello World with Spring Boot
Get started with Spring boot
Hello World with Spring Boot!
Run LIFF with Spring Boot
SNS login with Spring Boot
File upload with Spring Boot
Spring Boot starting with copy
Spring Boot starting with Docker
Hello World with Spring Boot
Set cookies with Spring Boot
Use Spring JDBC with Spring Boot
Add module with Spring Boot
Getting Started with Spring Boot
Create microservices with Spring Boot
Send email with spring boot
Implement a simple Rest API with Spring Security with Spring Boot 2.0
Use Basic Authentication with Spring Boot
gRPC on Spring Boot with grpc-spring-boot-starter
Create an app with Spring Boot 2
Hot deploy with Spring Boot development
Database linkage with doma2 (Spring boot)
Spring Boot programming with VS Code
Until "Hello World" with Spring Boot
Inquiry application creation with Spring Boot
Get validation results with Spring Boot
Implement file download with Spring MVC
(Intellij) Hello World with Spring Boot
Create an app with Spring Boot
Implement REST API in Spring Boot
Google Cloud Platform with Spring Boot 2.0.0
Check date correlation with Spring Boot
Implement Spring Boot application in Gradle
[Java] LINE integration with Spring Boot
Beginning with Spring Boot 0. Use Spring CLI
I tried Flyway with Spring Boot
Message cooperation started with Spring Boot
Spring Boot gradle build with Docker
Implement REST API with Spring Boot and JPA (Application Layer)
Implement REST API with Spring Boot and JPA (Infrastructure layer)
Implement REST API with Spring Boot and JPA (domain layer)
Implement a simple Rest API with Spring Security & JWT with Spring Boot 2.0
Implement a simple Web REST API server with Spring Boot + MySQL
Processing at application startup with Spring Boot
Hello World with Eclipse + Spring Boot + Maven
Send regular notifications with LineNotify + Spring Boot
Perform transaction confirmation test with Spring Boot
HTTPS with Spring Boot and Let's Encrypt
Challenge Spring Boot
Try using Spring Boot with VS Code
Start web application development with Spring Boot
Launch Nginx + Spring Boot application with docker-compose
I tried Lazy Initialization with Spring Boot 2.2.0
Spring Boot Form
Asynchronous processing with Spring Boot using @Async