java graphql_spring-boot集成graphql入门教程

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

graphql 到底是什么

graphql 是一种 API 查询语言, 用于服务器端执行按已定义类型系统的查询. GraphQL 不与任何特定的数据库或存储引擎进行绑定, 而是由您的代码和数据支持.(官方描述)

说白了 就是想要什么, 就传入什么字段, 也就会返回什么字段, 具体字段处理是服务器所提供, 而 graphql 并不会关心怎么服务器怎么处理

例如:

传统的rest api:  /test/user/{id}  return { id, name, age } 是一成不变的,

graphql:  findOneUser(id: xx)  return { id, name }  (注: 传输参数id, 指定返回字段 id, name, 当然也可以写{ name, age },完全取决于前端需求 )

graphql 的优势

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

项目构建

org.springframework.boot

spring-boot-starter-parent

2.0.0.RELEASE

大神们封装的 spring-boot 依赖

com.graphql-java

graphql-spring-boot-starter

4.0.0

com.graphql-java

graphql-java-tools

4.3.0

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

b6181095408a83b1914e9de4ac76c316.png

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

type Query {

findAllAuthors: [Author]!

countAuthors: Long!

findOneAuthor(id: Long!): Author

findAllBooks: [Book]!

countBooks: Long!

}

type Mutation {

newAuthor(firstName: String!, lastName: String!) : Author!

newBook(title: String!, isbn: String!, pageCount: Int, authorId: Long!) : Book!

saveBook(input: BookInput!): Book!

deleteBook(id: ID!) : Boolean

updateBookPageCount(pageCount: Int!, id: Long!) : Book!

}

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

type Author {

id: Long!

createdTime: String

firstName: String

lastName: String

books: [Book]

}

input BookInput {

title: String!

isbn: String!

pageCount: Int

authorId: Long

}

type Book {

id: Long!

title: String!

isbn: String!

pageCount: Int

author: Author

}

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

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

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

例如:

public class Query implements GraphQLQueryResolver {

private AuthorRepository authorRepository;

private BookRepository bookRepository;

public Author findOneAuthor(Long id) {

Optional opt = authorRepository.findById(id);

return opt.isPresent() ? opt.get() : null;

}

public List findAllAuthors() {

return authorRepository.findAll();

}

public Long countAuthors() {

return authorRepository.count();

}

public List findAllBooks() {

return bookRepository.findAll();

}

public Long countBooks() {

return bookRepository.count();

}

}

实现了所有的 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方法足够了

@Component

@AllArgsConstructor

public class AuthorResolver implements GraphQLResolver {

private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

private BookRepository bookRepository;

public String getCreatedTime(Author author) {

return sdf.format(author.getCreatedTime());

}

public List getBooks(Author author) {

return bookRepository.findByAuthorId(author.getId());

}

}

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

837a0c57df10961d47a629a49a44451e.png

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

712feb0322836335e1b9ff9a5340f294.png

demo 代码、GUI奉上:

java-graphql 还有一些其他的, 订阅/发布、校验、缓存等 只能去看文档了

希望大家一起学习, 交流

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值