Spring Boot + GraphQL 才是 API 的未來!
往期熱門文章:
快速開始
創(chuàng)建spring boot工程



引入相關(guān)依賴
"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實(shí)例
@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())??//其它定義照上面的示例,繼續(xù)增加
????????????.build().makeExecutableSchema();
????????this.graphQL?=?graphQL.newGraphQL(graphQLSchema).build();
????}
}
*.graphqls或者對(duì)應(yīng)的Resolver如ItemResolver,可以參看淺嘗GraphQL相關(guān)描述,這里只是作了微調(diào)整,相關(guān)代碼如下:schema?{
????#?查詢
????query:?Query
????#?更新
????mutation:?Mutation
}
type?Query?{
????version:?String
}
type?Mutation?{
????version:?String
}
#?定義一個(gè)查詢類型
extend?type?Query?{
????queryItemList:?ItemList??#?定義查詢項(xiàng)目列表
????queryById(id:?ID):?Item
}
extend?type?Mutation?{
????updateName(param:?Param):?Item
}
#?定義項(xiàng)目字段
type?Item?{
????id:?ID!
????code:?String!
????name:?String!
}
type?ItemList?{
????itemList:?[Item!]!??#獲取項(xiàng)目列表
????total:?Int!??????#?獲取項(xiàng)目總數(shù)
}
input?Param?{
????id:?ID!
????name:?String!
}
public?class?ItemResolver?implements?GraphQLQueryResolver,?GraphQLMutationResolver?{
????private?IItemService?itemService;
????public?ItemResolver(IItemService?itemService)?{
????????this.itemService?=?itemService;
????}
????//?對(duì)應(yīng)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();
????????Map?result?=?new?HashMap<>();
????????ExecutionResult?executionResult?=?graphQL.execute(executionInput);
????????List?errors?=?executionResult.getErrors();
????????if?(errors?!=?null?&&?!errors.isEmpty())?{
????????????result.put("errors",?errors);
????????????return?result;
????????}
????????return?executionResult.getData();
????}
}
測(cè)試
ItemList?queryItemList();
Item?queryById(Long?id);
Item?updateName(Param?param);




結(jié)束語
往期熱門文章:
1、《歷史文章分類導(dǎo)讀列表!精選優(yōu)秀博文都在這里了!》 2、用 Java 爬小姐姐圖片,這個(gè)厲害了。。。 3、消息冪等(去重)通用解決方案,真頂! 4、從MySQL 5.6升級(jí)到8.0,F(xiàn)acebook付出了慘痛代價(jià)…… 5、當(dāng) Transactional 碰到鎖,有個(gè)大坑! 6、橫空出世,比Visio快10倍的畫圖工具來了。 7、驚呆了,Spring中竟然有12種定義bean的方法 8、代碼寫的垃圾被嫌棄? 9、牛逼!SpringBoot+Vue企業(yè)級(jí)支付系統(tǒng)!附源碼! 10、你真的會(huì)寫for循環(huán)嗎?來看看這些常見的for循環(huán)優(yōu)化方式
評(píng)論
圖片
表情
