Python 官方文档:入门教程 => 点击学习
目录前言Spring Boot中GraphQL的实现方案前言 传统的Restful api 存在诸多的问题,首先它无法控制返回的字段,前端也无法预判后端的返回结果,另外不同的返回结果
传统的Restful api 存在诸多的问题,首先它无法控制返回的字段,前端也无法预判后端的返回结果,另外不同的返回结果对应不同的请求地址,这就导致了多次请求的问题。而GraphQL正是基于这样的背景而构建出来的API查询语言,相对于传统Restful API 它具有以下几个优点:
如果后端语言为Java,那么GraphQL Java则是实现GraphQL的基础库。另外Spring已经整合了GraphQL,如果项目中使用了Spring,那么更加推荐Spring GraphQL。
Spring GraphQL的开发总体分为如下几个步骤
添加 Spring GraphQL 依赖项
在您的项目中添加 Spring GraphQL 依赖项。您可以通过 Maven 或 Gradle 等构建工具来添加依赖项。例如,如果您使用 Maven,则可以添加以下依赖项
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-graphql</artifactId>
</dependency>
定义 GraphQL Schema
在您的应用程序中定义 GraphQL Schema。Schema 定义了可查询的类型和字段。您可以使用 SDL(Schema Definition Language)或编程方式定义 Schema。
对于Spring Boot 工程来说schema文件放到resources/graphql/目录下,文件名后缀graphqls,下面是我定义一个的简单的schema.graphqls。
它指定了两个查询实现,author(id:Int)表示通过id查询Author,allAuthors则表示查询Author数组。
schema {
query: Query
}type Query {
author(id:Int): Author
allAuthors: [Author]
}type Author {
id:Int
firstName:String
lastName:String
email:String
birthdate:String
}
实现RuntimeWirinGConfigurer
RuntimeWiringConfigurer是实现GraphQL获取数据的核心,使用GraphQL并不能直接去掉mybatis/Jpa这类持久层框架,从数据库获取数据仍然需要这类框架的支持。
而RuntimeWiringConfigurer则类似于Spring中的service层,它是实现基础数据的核心。
以下是一个简单示例:
@Component
public class AuthorWiring implements RuntimeWiringConfigurer {
private final AuthorRepository authorRepository;
public AuthorWiring(AuthorRepository authorRepository) {
this.authorRepository = authorRepository;
}
@Override
public void configure(RuntimeWiring.Builder builder) {
builder.type("Query", typeWiring -> typeWiring
.dataFetcher("allAuthors", environment -> authorRepository.findAll())
.dataFetcher("author", environment -> authorRepository.getReferenceById(environment.getArgument("id")))
}
}
这里configure方法内部分别定义了两个DataFetcher对象,用来指定author和allAuthors查询数据的方式,可以看出依然是通过JPA去查询数据。
定义GraphQL Controller
我么定义GraphQLController用来接收WEB请求的入参,示例如下:
@RestController
@RequestMapping("graphql")
public class GraphQLController {
private final GraphQL graphQL;
@Autowired
public GraphQLController(GraphQlSource graphQlSource) {
graphQL = graphQlSource.graphQl();
}
@PostMapping("query")
public ResponseEntity<Object> query(@RequestBody String query) {
ExecutionResult result = graphQL.execute(query);
return ResponseEntity.ok(result.getData());
}
}
代码中GraphQL对象是执行查询的入口,但GraphQL只有一个私有的构造方法,所以不能直接注入,必须通过注入GraphQlSource的方式来获取GraphQL对象。
注意在GraphQL中我们只能使用String来接收参数,无法使用model对象,这是因为Graph请求参数并不是JSON结构。
测试Graph请求
我们创建一个graphql.Http的文件,用于在idea中执行http请求
### Send POST request with json body
POST http://localhost:8080/graphql/query
Content-Type: application/json{
author(id: 1) {
id
firstName
lastName
birthdate
}
}### Send POST request with json body
POST http://localhost:8080/graphql/query
Content-Type: application/json{
allAuthors {
id
firstName
lastName
birthdate
}
}
运行author(id: 1) 的查询,可以看到正常返回结果了。如果我们只需要 firstName和lastName两个字段,那么在请求入参中直接去掉id和birthdate就好了,而不用改动任何后端代码。
完整项目已上传GitHub 👉 graphql-demo
到此这篇关于SpringBoot使用GraphQL开发Web API实现方案示例讲解的文章就介绍到这了,更多相关SpringBoot GraphQL内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: SpringBoot使用GraphQL开发WebAPI实现方案示例讲解
本文链接: https://www.lsjlt.com/news/203096.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0