随着现代应用程序的复杂性不断提高,构建可扩展的API和查询系统变得越来越重要。在过去,REST API和SOAP都是主流的API构建方案,但现在GraphQL也成为了受欢迎的选项。本文将介绍如何使用Spring Boot和GraphQL构建API和查询系统。
什么是GraphQL?
GraphQL是一种用于API和查询系统的查询语言。与传统的REST API相比,GraphQL有以下优势:
Spring Boot和GraphQL
Spring Boot是一个Java框架,用于构建基于Java的Web应用程序。它提供了许多有用的功能,例如自动配置和快速开发。与传统的Java Web开发相比,Spring Boot可以使开发过程变得更加愉快和高效。
在本文中,我们将使用Spring Boot和GraphQL来构建一个基本的API和查询系统。在开始之前,您需要了解以下几个组件:
构建API和查询系统
首先,我们需要创建一个Spring Boot应用程序。您可以使用Spring Initializr来快速创建一个Spring Boot应用程序。以下是创建一个Spring Boot应用程序的步骤:
创建GraphQL Schema
在创建GraphQL Schema之前,让我们考虑一下我们的API需要执行哪些操作。我们将创建一个具有三个类型的API:作者,书籍和作者-书籍关系。以下是我们的API操作:
下一步是创建GraphQL Schema。Schema定义了可以在API上执行的操作。在本文中,我们将使用GraphQL Java Tools来创建Schema。创建GraphQL Schema的步骤如下:
type Author { id: ID! name: String! } type Book { id: ID! title: String! author: Author! } type Relationship { id: ID! author: Author! book: Book! } type Query { authors: [Author] author(id: ID!): Author books: [Book] book(id: ID!): Book relationships: [Relationship] booksByAuthor(authorId: ID!): [Book] }
这个Schema定义了三个类型:作者,书籍和关系。它还定义了六个操作:获取作者列表,按ID获取作者,获取书籍列表,按ID获取书籍,获取关系列表和按作者ID获取关联书籍。
package com.example.demo; import com.example.demo.entity.*; import com.example.demo.repository.*; import com.example.demo.resolver.*; import java.util.List; import javax.annotation.PostConstruct; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import graphql.schema.DataFetcher; import graphql.schema.DataFetchingEnvironment; import graphql.schema.GraphQLSchema; import graphql.schema.idl.RuntimeWiring; import graphql.schema.idl.SchemaGenerator; import graphql.schema.idl.SchemaParser; import graphql.schema.idl.TypeRuntimeWiring; import graphql.servlet.GraphQLServlet; import graphql.servlet.SimpleGraphQLHttpServlet; @Configuration public class GraphQLProvider { private static final Logger LOGGER = LoggerFactory.getLogger(GraphQLProvider.class); private final AuthorRepository authorRepository; private final BookRepository bookRepository; private final RelationshipRepository relationshipRepository; private List<DataFetcher> fetchDataers; @Autowired public GraphQLProvider( AuthorRepository authorRepository, BookRepository bookRepository, RelationshipRepository relationshipRepository, List<DataFetcher> fetchDataers ) { this.authorRepository = authorRepository; this.bookRepository = bookRepository; this.relationshipRepository = relationshipRepository; this.fetchDataers = fetchDataers; } @PostConstruct public void setup() { fetchDataers.add(new DataFetcher() { @Override public Object get(DataFetchingEnvironment environment) { return authorRepository.findAll(); } }); fetchDataers.add(new DataFetcher() { @Override public Object get(DataFetchingEnvironment environment) { return authorRepository.findById(environment.getArgument("id")).get(); } }); fetchDataers.add(new DataFetcher() { @Override public Object get(DataFetchingEnvironment environment) { return bookRepository.findAll(); } }); fetchDataers.add(new DataFetcher() { @Override public Object get(DataFetchingEnvironment environment) { return bookRepository.findById(environment.getArgument("id")).get(); } }); fetchDataers.add(new DataFetcher() { @Override public Object get(DataFetchingEnvironment environment) { return relationshipRepository.findAll(); } }); fetchDataers.add(new DataFetcher() { @Override public Object get(DataFetchingEnvironment environment) { return bookRepository.findByAuthor_Id(environment.getArgument("authorId")); } }); } @Bean public GraphQLSchema schema() { SchemaParser schemaParser = new SchemaParser(); SchemaGenerator schemaGenerator = new SchemaGenerator(); TypeRuntimeWiring.Builder authorWiring = newTypeWiring("Author").dataFetchers(fetchDataers); return schemaGenerator.makeExecutableSchema(schemaParser.parse(getClass().getResource("/schema.graphqls").getPath()), RuntimeWiring.newRuntimeWiring() .type(authorWiring) .build()); } @Bean public GraphQLServlet graphQLServlet() { return new SimpleGraphQLHttpServlet(new GraphQL.Builder(schema()).build()); } }
该类创建一个GraphQL服务,将schema.graphqls文件加载到该服务中,并定义了Data Fetchers。Data Fetchers负责获取数据并填充GraphQL操作的结果。
创建JPA实体和存储库
现在,我们需要创建实体并将它们映射到数据库中。在本文中,我们将创建Author,Book和Relationship实体,并使用JPA将其映射到H2数据库。
package com.example.demo.repository; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import com.example.demo.entity.Author; @Repository public interface AuthorRepository extends JpaRepository<Author, Long> { }
package com.example.demo.entity; import lombok.Data; import javax.persistence.*; @Data @Entity public class Author { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; protected Author() {} public Author(String name) { this.name = name; } }
在上面的示例中,我们使用Lombok的@Data注释的“id”和“name”字段创建了一个名为Author的Java实体。
填充数据
我们现在可以使用H2控制台或编写Java代码来填充数据。
使用H2控制台填充数据:
INSERT INTO author (id, name) VALUES (1, 'William Shakespeare'); INSERT INTO author (id, name) VALUES (2, 'John Milton'); INSERT INTO author (id, name) VALUES (3, 'Charles Dickens'); INSERT INTO book (id, title, author_id) VALUES (1, 'Hamlet', 1); INSERT INTO book (id, title, author_id) VALUES (2, 'Paradise Lost', 2); INSERT INTO book (id, title, author_id) VALUES (3, 'Oliver Twist', 3); INSERT INTO relationship (id, author_id, book_id) VALUES (1, 1, 1); INSERT INTO relationship (id, author_id, book_id) VALUES (2, 2, 2); INSERT INTO relationship (id, author_id, book_id) VALUES (3, 3, 3);
使用Java代码填充数据:
package com.example.demo.seed; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; import com.example.demo.entity.Author; import com.example.demo.entity.Book; import com.example.demo.entity.Relationship; import com.example.demo.repository.AuthorRepository; import com.example.demo.repository.BookRepository; import com.example.demo.repository.RelationshipRepository; @Component public class DataSeed implements CommandLineRunner { private AuthorRepository authorRepository; private BookRepository bookRepository; private RelationshipRepository relationshipRepository; public DataSeed(AuthorRepository authorRepository, BookRepository bookRepository, RelationshipRepository relationshipRepository) { this.authorRepository = authorRepository; this.bookRepository = bookRepository; this.relationshipRepository = relationshipRepository; } @Override public void run(String... args) throws Exception { Author shakespeare = new Author("William Shakespeare"); Author milton = new Author("John Milton"); Author dickens = new Author("Charles Dickens"); authorRepository.save(shakespeare); authorRepository.save(milton); authorRepository.save(dickens); Book hamlet = new Book("Hamlet", shakespeare); Book paradiseLost = new Book("Paradise Lost", milton); Book oliverTwist = new Book("Oliver Twist", dickens); bookRepository.save(hamlet); bookRepository.save(paradiseLost); bookRepository.save(oliverTwist); relationshipRepository.save(new Relationship(shakespeare, hamlet)); relationshipRepository.save(new Relationship(milton, paradiseLost)); relationshipRepository.save(new Relationship(dickens, oliverTwist)); } }
在上面的示例中,我们创建了一个CommandLineRunner工具类,它在应用程序启动时添加示例数据到数据库中。
测试GraphQL API
我们现在可以使用GraphQL Playground工具查询GraphQL API。
以下是一些示例查询:
获取作者列表:
query { authors { id name } }
按ID获取作者:
query { author(id: 1) { id name } }
获取书籍列表:
query { books { id title author { id name } } }
按ID获取书籍:
query { book(id: 1) { id title author { id name } } }
获取作者-书籍关系列表:
query { relationships { id author { id name } book { id title } } }
按作者ID获取关联书籍:
query { booksByAuthor(authorId: 1) { id title author { id name } } }
结论
本文介绍了如何使用Spring Boot和GraphQL构建API和查询系统,并执行基本的操作。可以使用GraphQL Java Tools和JPA轻松定义Schema和映射实体。GraphQL的灵活性和可扩展性使得它成为构建现代Web应用程序的理想选择。
以上是使用Spring Boot和GraphQL构建API和查询系统的详细内容。更多信息请关注PHP中文网其他相关文章!