概述
一、graphql基本语法:
https://graphql.cn/learn/queries/#variables
二、graphql-java(graphql java客户端):
demo:https://blog.csdn.net/tangyaya8/article/details/105328461
扩展:数据关联
author对象下存在fans(一对多关联)
创建fans对象
@Entity
@Data
public class Fans {
/**
* 主键
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
/**
* 创建时间
*/
private LocalDateTime createdTime;
/**
* 更新时间
*/
private LocalDateTime updatedTime;
/**
* firstName
*/
private String firstName;
/**
* lastName
*/
private String lastName;
/**
* authorId
*/
@Column(name = "author_id")
private Long authorId;
}
author对象添加关联信息
@OneToMany(targetEntity = Fans.class)
@JoinColumn(name = "author_id",referencedColumnName = "id")
private List<Fans> fans;
service、repository省略…
FansFetcher(相当于配置给graphql的service)
@Component
public class FansFetcher {
private final FansService fansService;
public FansFetcher(FansService fansService) {
this.fansService = fansService;
}
public DataFetcher<List<Fans>> getAllFans() {
return dataFetchingEnvironment -> fansService.getAllFans();
}
public DataFetcher<List<Fans>> getAuthorFans() {
return dataFetchingEnvironment -> fansService.getAuthorFans(dataFetchingEnvironment.getArgument("authorId"));
}
public DataFetcher<Fans> fansById() {
return dataFetchingEnvironment -> fansService.fansById(dataFetchingEnvironment.getArgument("id"));
}
public DataFetcher<Fans> saveFans() {
return dataFetchingEnvironment ->
fansService.saveFans(this.mapLinkedMapToFans(dataFetchingEnvironment.getArgument("fans")));
}
public DataFetcher<Boolean> deleteFans() {
return dataFetchingEnvironment -> {
fansService.deleteFansById(dataFetchingEnvironment.getArgument("id"));
return true;
};
}
private Fans mapLinkedMapToFans(Map<String, String> mapping) {
Fans fans = new Fans();
fans.setCreatedTime(LocalDateTime.now());
fans.setUpdatedTime(LocalDateTime.now());
fans.setLastName(mapping.get("lastName"));
fans.setFirstName(mapping.get("firstName"));
fans.setAuthorId(Long.parseLong(mapping.get("authorId")));
return fans;
}
}
AuthorSchema改造
@Component
public class AuthorSchema {
private final AuthorFetcher authorFetcher;
private final FansFetcher fansFetcher;
private GraphQL graphQL;
public AuthorSchema(AuthorFetcher authorFetcher, FansFetcher fansFetcher) {
this.authorFetcher = authorFetcher;
this.fansFetcher = fansFetcher;
}
@Bean
public GraphQL genGraphQL() {
return graphQL;
}
@PostConstruct
public void init() throws FileNotFoundException {
GraphQLSchema graphQLSchema = this.graphQLSchema(ResourceUtils.getFile("classpath:graphql/author.graphqls"));
graphQL = GraphQL.newGraphQL(graphQLSchema).build();
}
private GraphQLSchema graphQLSchema(File sdl) {
TypeDefinitionRegistry typeDefinitionRegistry = new SchemaParser().parse(sdl);
RuntimeWiring runtimeWiring = this.buildRunTimeWiring();
SchemaGenerator schemaGenerator = new SchemaGenerator();
return schemaGenerator.makeExecutableSchema(typeDefinitionRegistry, runtimeWiring);
}
private RuntimeWiring buildRunTimeWiring() {
return RuntimeWiring.newRuntimeWiring()
.type("Query",
builder -> builder.dataFetcher("allAuthor", authorFetcher.getAllAuthor())
.dataFetcher("authorById", authorFetcher.authorById())
.dataFetcher("authorFans", fansFetcher.getAuthorFans())
.dataFetcher("fansById", fansFetcher.fansById())
)
.type("Mutation",
builder -> builder.dataFetcher("saveAuthor", authorFetcher.saveAuthor())
.dataFetcher("deleteAuthorById", authorFetcher.deleteAuthor())
.dataFetcher("saveFans", fansFetcher.saveFans())
.dataFetcher("deleteFansById", fansFetcher.deleteFans())
)
.build();
}
}
graphqls文件改造
schema {
query: Query
mutation: Mutation
}
type Query{
# 查询所有作者
allAuthor:[Author!]
# 根据Id获取作者
authorById(id:Long):Author!
# 查询作者的粉丝
authorFans(id:Long):[Fans!]
# 根据Id获取粉丝
fansById(id:Long):Fans!
}
type Mutation{
# 保存作者
saveAuthor(author:AuthorInput):Author
# 根据作者id删除作者
deleteAuthorById(id:Long):Boolean
# 保存粉丝
saveFans(fans:FansInput):Fans
# 根据作者id删除粉丝
deleteFansById(id:Long):Boolean
}
input AuthorInput{
id:ID
firstName:String
lastName:String
}
scalar Long
#作者对象
type Author{
id:ID
firstName:String
lastName:String
fans:[Fans!]
}
input FansInput{
id:ID
firstName:String
lastName:String
authorId:Long
}
#粉丝对象
type Fans{
id:ID
firstName:String
lastName:String
}
测试
请求:
{
authorList: allAuthor
{
...comparisonFields
}
}
fragment comparisonFields on Author {
id
firstName
lastName
fans {
fansId:id
fansFirstName:firstName
}
}
响应
{
"data": {
"authorList": [
{
"id": "1",
"firstName": "希望",
"lastName": "测试",
"fans": [
{
"fansId": "1",
"fansFirstName": "fans"
},
{
"fansId": "2",
"fansFirstName": "fans"
}
]
},
{
"id": "2",
"firstName": "A",
"lastName": "B",
"fans": [
{
"fansId": "3",
"fansFirstName": "fans"
}
]
}
]
}
}
三、其他
- 注意:schema只能同时实现一个,如果存在多个对象/模块可以配置到一个schema文件里
- 关联信息由程序控制,并非graphql控制,比如上述案例中
fans并非是graphql在获取到作者信息后,根据作者的id去查询的粉丝信息,而是直接在orm框架查询出来的结果中读取的(如果orm框架并没有查询粉丝数据,那么graphql并不会去查询fans的信息,只会返回空的粉丝数据)。
同理,则orm逻辑并不会收graphql影响(如果查询没有要求粉丝的信息,而orm框架的配置是要读取粉丝信息,那么哪怕是不返回,orm也会查询相关信息)。
最后
以上就是默默蓝天为你收集整理的graphql-java的CRUD及关联查询的全部内容,希望文章能够帮你解决graphql-java的CRUD及关联查询所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复