Please refer to REST Issues and GraphQL.
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>
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
}
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;
}
}
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;
}
}
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.
Recommended Posts