简介
GraphQL是一种用于 API 的查询语言。GraphQL 对你的 API 中的数据提供了一套易于理解的完整描述,使得客户端能够准确地获得它需要的数据,而且没有任何冗余,也让 API 更容易地随着时间推移而演进,还能用于构建强大的开发者工具。本文介绍在SpringBoot中使用GraphQL。如果你对GraphQL已经有一定了解,但苦于不知道如何在实际项目中使用它,那么这篇文章应该可以帮到你。
阅读此文要求掌握基本的java开发及springboot项目开发技能。
文件目录结构参考
│ pom.xml
├─src
│ ├─main
│ │ ├─java
│ │ │ └─com
│ │ │ └─example
│ │ │ └─demo
│ │ │ Author.java
│ │ │ Book.java
│ │ │ BookController.java
│ │ │ DemoApplication.java
│ │ │
│ │ └─resources
│ │ │ application.properties
│ │ │
│ │ ├─graphql
│ │ │ schema.graphqls
依赖引入
在 pom.xml 中 添加以下依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-graphql</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
我测试时,是基于spring-boot 2.7.11版本。
定义启动类
我们先创建一个 SpringBoot的启动类 DemoApplication.java
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
定义schema
创建目录 src/main/resources/graphql,在这个目录下新建一个 schema.graphqls 文件,并写入以下内容
type Query {
bookById(id: ID): Book
}
type Book {
id: ID
name: String
pageCount: Int
author: Author
}
type Author {
id: ID
firstName: String
lastName: String
}
创建实体类
Author.java
public class Author {
private String id;
private String firstName;
private String lastName;
public Author(String id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
private static List<Author> authors = Arrays.asList(
new Author("author-1", "Joanne", "Rowling"),
new Author("author-2", "Herman", "Melville"),
new Author("author-3", "Anne", "Rice")
);
public static Author getById(String id) {
return authors.stream().filter(author -> author.getId().equals(id)).findFirst().orElse(null);
}
public String getId() {
return id;
}
}
Book.java
public class Book {
private String id;
private String name;
private int pageCount;
private String authorId;
public Book(String id, String name, int pageCount, String authorId) {
this.id = id;
this.name = name;
this.pageCount = pageCount;
this.authorId = authorId;
}
private static List<Book> books = Arrays.asList(
new Book("book-1", "Harry Potter and the Philosopher's Stone", 223, "author-1"),
new Book("book-2", "Moby Dick", 635, "author-2"),
new Book("book-3", "Interview with the vampire", 371, "author-3")
);
public static Book getById(String id) {
return books.stream().filter(book -> book.getId().equals(id)).findFirst().orElse(null);
}
public String getId() {
return id;
}
public String getAuthorId() {
return authorId;
}
}
创建 Controller
BookController.java
@Controller
public class BookController {
@QueryMapping
public Book bookById(@Argument String id) {
return Book.getById(id);
}
@SchemaMapping
public Author author(Book book) {
return Author.getById(book.getAuthorId());
}
}
补充配置文件
在 application.properties 中添加
spring.graphql.graphiql.enabled=true
spring.graphql.graphiql.path=/graphiql
这个配置是 用来启动调试工具的。
启动调试
然后我们就可以通过 DemoApplication.java 启动 SpringBoot程序了。 正常启动后,在浏览器里打开 http://localhost:8080/graphiql?path=/graphql 就能看到以下测试界面了。
我们可以F12打开浏览器的开发者工作,抓包查看具体的接口请求及返回。