使用Spring Boot集成GraphQL

大家好,我是微赚淘客系统3.0的小编,是个冬天不穿秋裤,天冷也要风度的程序猿!

GraphQL是一种用于API的查询语言,提供了一个强大的工具来实现灵活和高效的数据查询。在这篇文章中,我们将介绍如何在Spring Boot应用程序中集成GraphQL。我们将一步一步地展示如何设置项目、定义GraphQL模式、编写查询和变更以及处理GraphQL请求。

1. 设置Spring Boot项目

首先,我们需要创建一个Spring Boot项目。可以使用Spring Initializr来生成项目,并添加以下依赖:

<dependency>
    <groupId>com.graphql-java-kickstart</groupId>
    <artifactId>graphql-spring-boot-starter</artifactId>
    <version>11.1.0</version>
</dependency>
<dependency>
    <groupId>com.graphql-java-kickstart</groupId>
    <artifactId>graphiql-spring-boot-starter</artifactId>
    <version>11.1.0</version>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>com.graphql-java-kickstart</groupId>
    <artifactId>altair-spring-boot-starter</artifactId>
    <version>11.1.0</version>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>com.graphql-java-kickstart</groupId>
    <artifactId>graphql-java-tools</artifactId>
    <version>11.1.0</version>
</dependency>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.

2. 定义GraphQL模式

GraphQL模式定义了API的类型和结构。我们可以在资源文件夹中创建一个名为schema.graphqls的文件,并在其中定义模式。

type Query {
    getBookById(id: ID!): Book
    getAllBooks: [Book]
}

type Mutation {
    addBook(title: String!, author: String!): Book
}

type Book {
    id: ID!
    title: String!
    author: String!
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

3. 创建实体类

接下来,我们需要为GraphQL模式中的类型创建Java类。在这个例子中,我们创建一个Book类。

package cn.juwatech.model;

public class Book {
    private String id;
    private String title;
    private String author;

    // Getters and Setters
    public String getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.

4. 创建GraphQL查询和变更的Resolver

Resolvers是用于处理GraphQL查询和变更的组件。我们需要创建一个QueryResolver和一个MutationResolver。

package cn.juwatech.resolver;

import cn.juwatech.model.Book;
import cn.juwatech.service.BookService;
import com.coxautodev.graphql.tools.GraphQLQueryResolver;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class Query implements GraphQLQueryResolver {
    private final BookService bookService;

    public Query(BookService bookService) {
        this.bookService = bookService;
    }

    public Book getBookById(String id) {
        return bookService.getBookById(id);
    }

    public List<Book> getAllBooks() {
        return bookService.getAllBooks();
    }
}

package cn.juwatech.resolver;

import cn.juwatech.model.Book;
import cn.juwatech.service.BookService;
import com.coxautodev.graphql.tools.GraphQLMutationResolver;
import org.springframework.stereotype.Component;

@Component
public class Mutation implements GraphQLMutationResolver {
    private final BookService bookService;

    public Mutation(BookService bookService) {
        this.bookService = bookService;
    }

    public Book addBook(String title, String author) {
        return bookService.addBook(title, author);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.

5. 创建服务类

服务类用于处理业务逻辑。我们在这个例子中创建一个BookService类来处理图书的CRUD操作。

package cn.juwatech.service;

import cn.juwatech.model.Book;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class BookService {
    private List<Book> books = new ArrayList<>();

    public Book getBookById(String id) {
        return books.stream().filter(book -> book.getId().equals(id)).findFirst().orElse(null);
    }

    public List<Book> getAllBooks() {
        return books;
    }

    public Book addBook(String title, String author) {
        Book book = new Book();
        book.setId(UUID.randomUUID().toString());
        book.setTitle(title);
        book.setAuthor(author);
        books.add(book);
        return book;
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.

6. 配置GraphQL

application.yml文件中添加GraphQL的配置。

graphql:
  servlet:
    mapping: /graphql
    enabled: true
  tools:
    schema-location-pattern: "**/*.graphqls"
  graphiql:
    enabled: true
    endpoint: /graphql
  altair:
    enabled: true
    endpoint: /graphql
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

7. 测试GraphQL API

启动Spring Boot应用程序后,可以通过访问http://localhost:8080/graphiqlhttp://localhost:8080/altair来测试GraphQL API。

查询所有图书的示例查询:

query {
  getAllBooks {
    id
    title
    author
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

添加新图书的示例变更:

mutation {
  addBook(title: "Spring Boot in Action", author: "Craig Walls") {
    id
    title
    author
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

通过这些步骤,我们已经成功地在Spring Boot应用程序中集成了GraphQL。GraphQL的引入使得API查询变得更加灵活和高效,并且为前端开发者提供了更好的数据获取体验。

著作权归聚娃科技微赚淘客系统开发者团队,转载请注明出处!