Spring Boot + GraphQL 才是 API 的未来!
往期热门文章:
1、全员远程办公,半年入 1 亿美元:GitHub 的最大竞争对手上市了!
快速开始
创建spring boot工程
![](https://filescdn.proginn.com/cad2a99a237a340259ddf2ac96e41f30/b73f240c2a4aa09c479ca7b18d5cb2da.webp)
![](https://filescdn.proginn.com/e093c6b617eaff2c240f948dce7269d0/5778b37c438d766bc1989c7cae00ad18.webp)
![](https://filescdn.proginn.com/d7894eb26b9473022e595c242f3c81e0/098f817131adc88000f182132caae4b2.webp)
引入相关依赖
"1.0" encoding="UTF-8"?> "http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.4.6
com.xuxd
graphql.demo
0.0.1-SNAPSHOT
graphql.demo
GraphQL Demo project for Spring Boot
1.8
1.8
1.8
UTF-8
UTF-8
1.18.20
11.0.1
2.8.7
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.projectlombok
lombok
${lombok.version}
provided
com.graphql-java-kickstart
graphql-java-tools
${graphql-java-tools.version}
com.google.code.gson
gson
${gson.version}
org.springframework.boot
spring-boot-maven-plugin
初始化GraphQL实例
@Component
public class GraphQLProvider {
private GraphQL graphQL;
@Autowired
private IItemService itemService;
@Bean
public GraphQL graphQL() {
return graphQL;
}
@PostConstruct
public void init() throws IOException {
GraphQLSchema graphQLSchema = SchemaParser.newParser()
.file("graphql/base.graphqls")
.resolvers(new Query(), new Mutation())
.file("graphql/item.graphqls")
.resolvers(new ItemResolver(itemService))
// .file("book.graphqls")
// .resolvers(new BookResolver()) //其它定义照上面的示例,继续增加
.build().makeExecutableSchema();
this.graphQL = graphQL.newGraphQL(graphQLSchema).build();
}
}
*.graphqls
或者对应的Resolver如ItemResolver,可以参看浅尝GraphQL相关描述,这里只是作了微调整,相关代码如下:schema {
# 查询
query: Query
# 更新
mutation: Mutation
}
type Query {
version: String
}
type Mutation {
version: String
}
# 定义一个查询类型
extend type Query {
queryItemList: ItemList # 定义查询项目列表
queryById(id: ID): Item
}
extend type Mutation {
updateName(param: Param): Item
}
# 定义项目字段
type Item {
id: ID!
code: String!
name: String!
}
type ItemList {
itemList: [Item!]! #获取项目列表
total: Int! # 获取项目总数
}
input Param {
id: ID!
name: String!
}
public class ItemResolver implements GraphQLQueryResolver, GraphQLMutationResolver {
private IItemService itemService;
public ItemResolver(IItemService itemService) {
this.itemService = itemService;
}
// 对应item.graphqls里的queryItemList
public ItemList queryItemList() {
return itemService.queryItemList();
}
public Item queryById(Long id) {
return itemService.queryById(id);
}
public Item updateName(Param param) {
return itemService.updateName(param);
}
}
提供API
@RestController
@RequestMapping("/graphql")
@Log
public class GraphqlController {
@Autowired
private GraphQL graphQL;
@PostMapping
public Object execute(@RequestBody GraphqlRequest request) {
ExecutionInput executionInput = ExecutionInput.newExecutionInput()
.query(request.getQuery())
.variables(request.getVariables())
.build();
Mapresult = new HashMap<>();
ExecutionResult executionResult = graphQL.execute(executionInput);
Listerrors = executionResult.getErrors();
if (errors != null && !errors.isEmpty()) {
result.put("errors", errors);
return result;
}
return executionResult.getData();
}
}
测试
ItemList queryItemList();
Item queryById(Long id);
Item updateName(Param param);
![](https://filescdn.proginn.com/d70f16e77c124e7d168b5b09ff50dfa8/8face419508194d05b0ff82b2a978282.webp)
![](https://filescdn.proginn.com/85c702ecbd0a075dcd362c18a9c4f131/25f0fb3b82a1130dc17843b7c49d902c.webp)
![](https://filescdn.proginn.com/42ce1dcdd781ee730f1ac09a2d0cb386/ae5c945fe794bcffb2e1d82a3f26bca3.webp)
![](https://filescdn.proginn.com/011c1c10ee2dcfecdc086f3b62e5873f/51b54cb092eedbbd9c76e26f9ecbd028.webp)
结束语
最近热文阅读:
1、全员远程办公,半年入 1 亿美元:GitHub 的最大竞争对手上市了! 2、面试官:Spring AOP、AspectJ、CGLIB 都是什么鬼?它们有什么关系? 3、10 个 VSCode 超级摸鱼神器,确定不试一下? 4、JMH + Arthas,性能监控的神器 5、MySQL 8.0 可以操作 JSON 了,牛逼 6、MySQL 用 limit 为什么会影响性能? 7、List中remove()方法的陷阱,被坑惨了! 8、7 种 Spring Boot 注入 Bean 的方式 9、再见MybatisPlus,阿里推出新ORM框架! 10、为什么不推荐使用BeanUtils属性转换工具 关注公众号,你想要的Java都在这里
评论