spring-boot集成graphql入门教程

绝地灬酷狼 2021-07-04 23:05 928阅读 0赞

https://my.oschina.net/genghz/blog/1789240

摘要: 这是一个spring-boot 集成 graphql 的入门教程, 仅供参考学习, 初次写, 请多见谅

本文介绍一个spring-boot + graphql, 是一个 graphql java 入门项目

graphql 到底是什么

  1. graphql 是一种 API 查询语言, 用于服务器端执行按已定义类型系统的查询. GraphQL 不与任何特定的数据库或存储引擎进行绑定, 而是由您的代码和数据支持.(官方描述)
  2. 说白了 就是想要什么, 就传入什么字段, 也就会返回什么字段, 具体字段处理是服务器所提供, graphql 并不会关心怎么服务器怎么处理

例如:

  1. 传统的rest api: /test/user/\{id\} return \{ id, name, age \} 是一成不变的,
  2. graphql: findOneUser(id: xx) return \{ id, name \} (注: 传输参数id, 指定返回字段 id, name, 当然也可以写\{ name, age \},完全取决于前端需求 )

graphql 的优势

  1. graphql 大大减少了沟通成本, 避免每次了定义api字段的多少问题, 前端自己选择、组合想要的字段, 生成数据, graphql 提供了GUI 可以很方便的测试, 书写graphql语句, 查看服务提供的 doc, 详细信息请看 [https://graphql.cn/][https_graphql.cn]

项目构建

spring-boot 是基于 2.0.0版本, 想了解spring-boot https://gitee.com/geng_hz/Spring-Boot-Reference-Guide

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.0.0.RELEASE</version>
  5. </parent>

大神们封装的 spring-boot 依赖

  1. <!-- graphql -->
  2. <dependency>
  3. <groupId>com.graphql-java</groupId>
  4. <artifactId>graphql-spring-boot-starter</artifactId>
  5. <version>4.0.0</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>com.graphql-java</groupId>
  9. <artifactId>graphql-java-tools</artifactId>
  10. <version>4.3.0</version>
  11. </dependency>

项目的目录( 随便分的包最后会附上链接代码):

150407_uhV0_2725939.png

root.graphqls 是graphql 服务入口定义:

  1. type Query {
  2. findAllAuthors: [Author]!
  3. countAuthors: Long!
  4. findOneAuthor(id: Long!): Author
  5. findAllBooks: [Book]!
  6. countBooks: Long!
  7. }
  8. type Mutation {
  9. newAuthor(firstName: String!, lastName: String!) : Author!
  10. newBook(title: String!, isbn: String!, pageCount: Int, authorId: Long!) : Book!
  11. saveBook(input: BookInput!): Book!
  12. deleteBook(id: ID!) : Boolean
  13. updateBookPageCount(pageCount: Int!, id: Long!) : Book!
  14. }

scheme.graphqls 则是 query/mutation 具体的 scheme 定义字段、类型

  1. type Author {
  2. id: Long!
  3. createdTime: String
  4. firstName: String
  5. lastName: String
  6. books: [Book]
  7. }
  8. input BookInput {
  9. title: String!
  10. isbn: String!
  11. pageCount: Int
  12. authorId: Long
  13. }
  14. type Book {
  15. id: Long!
  16. title: String!
  17. isbn: String!
  18. pageCount: Int
  19. author: Author
  20. }

Query 是查询入口, Mutation则是修改入口

例: findOneAuthor 传入一个long id, 返回一个 Author schema,

graphql 入口定义了, 但这只是一个描述, 我们需要实现 query/mutation中的描述

例如:

  1. public class Query implements GraphQLQueryResolver {
  2. private AuthorRepository authorRepository;
  3. private BookRepository bookRepository;
  4. public Author findOneAuthor(Long id) {
  5. Optional<Author> opt = authorRepository.findById(id);
  6. return opt.isPresent() ? opt.get() : null;
  7. }
  8. public List<Author> findAllAuthors() {
  9. return authorRepository.findAll();
  10. }
  11. public Long countAuthors() {
  12. return authorRepository.count();
  13. }
  14. public List<Book> findAllBooks() {
  15. return bookRepository.findAll();
  16. }
  17. public Long countBooks() {
  18. return bookRepository.count();
  19. }
  20. }

实现了所有的 Query中的描述(必须全部实现)

schema 一样像我的目录中的一样 AuthorResolver 这是对schema中的描述的实现

注: query/mutation和普通的schema 一样, 只是它们是 graphql服务的入口, resolver实现描述遵循:

  1. method (*args)

2.method is(*args) 仅支持 return boolean

3.method get(*args)

4.method getField(*args)

这是种实现, 当提供了resolver时优先使用, 其次是 class this.get方法

Author.class 中的createdTime 是 Date, 然而 schema Author { createdTime: String }, 所以单独提供AuthorResovler 生成createdTime String, 而其他参数因为与schema Author类型一致,使用Author中的Get方法足够了

  1. @Component
  2. @AllArgsConstructor
  3. public class AuthorResolver implements GraphQLResolver<Author> {
  4. private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  5. private BookRepository bookRepository;
  6. public String getCreatedTime(Author author) {
  7. return sdf.format(author.getCreatedTime());
  8. }
  9. public List<Book> getBooks(Author author) {
  10. return bookRepository.findByAuthorId(author.getId());
  11. }
  12. }

执行脚本: ./run.sh 启动项目, graphql 的默认endpoint: /graphql

141435_UjJo_2725939.png
graphql GUI, 方便的用来编写测试 graphql 和 查看当前服务提供了那些可用的方法, 像这样:

141719_1Cij_2725939.png

demo 代码、GUI奉上:

  1. [https://gitee.com/geng\_hz/spring-boot-graphql][https_gitee.com_geng_hz_spring-boot-graphql]

发表评论

表情:
评论列表 (有 0 条评论,928人围观)

还没有评论,来说两句吧...

相关阅读

    相关 GraphQL入门

    GraphQL 是什么 GraphQL 是一种描述请求数据方法的语法,通常用于客户端从服务端加载数据。GraphQL 有以下三个主要特征: 它允许客户端指定具体所