当前位置:网站首页>DGS之Mutations
DGS之Mutations
2022-07-23 23:30:00 【你好y】
Mutations(突变)
DGS框架支持突变,其结构与数据提取器相同,使用@DgsData注解。下面是一个突变的简单例子。
type Mutation {
addRating(title: String, stars: Int):Rating
}
type Rating {
avgStars: Float
}
@DgsComponent
public class RatingMutation {
@DgsData(parentType = "Mutation", field = "addRating")
public Rating addRating(DataFetchingEnvironment dataFetchingEnvironment) {
int stars = dataFetchingEnvironment.getArgument("stars");
if(stars < 1) {
throw new IllegalArgumentException("Stars must be 1-5");
}
String title = dataFetchingEnvironment.getArgument("title");
System.out.println("Rated " + title + " with " + stars + " stars") ;
return new Rating(stars);
}
}
注意,上面的代码通过调用DataFetchingEnvironment.getArgument方法为Mutation检索输入数据,就像数据提取器为其参数所做的那样。
Input Types
在上面的例子中,输入是两个标准的标量类型。你也可以使用复杂的类型,你应该在你的模式中把这些定义为输入类型。输入类型几乎与GraphQL中的类型相同,但有一些额外的规则。
根据GraphQL规范,输入类型应该总是以Map的形式传递给数据提取器。这意味着输入类型的DataFetchingEnvironment.getArgument是一个Map,而不是你可能有的Java/Kotlin表示。该框架有一个围绕这个问题的便利机制,这将在接下来讨论。让我们先看看一个直接使用DataFetchingEnvironment的例子。
type Mutation {
addRating(input: RatingInput):Rating
}
input RatingInput {
title: String,
stars: Int
}
type Rating {
avgStars: Float
}
--------------------------------
@DgsComponent
public class RatingMutation {
@DgsData(parentType = "Mutation", field = "addRating")
public Rating addRating(DataFetchingEnvironment dataFetchingEnvironment) {
//输入类型应该总是以Map的形式传递给数据提取器
Map<String,Object> input = dataFetchingEnvironment.getArgument("input");
//为Mutation检索输入数据
RatingInput ratingInput = new ObjectMapper().convertValue(input, RatingInput.class);
System.out.println("Rated " + ratingInput.getTitle() + " with " + ratingInput.getStars() + " stars") ;
return new Rating(ratingInput.getStars());
}
}
class RatingInput {
private String title;
private int stars;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getStars() {
return stars;
}
public void setStars(int stars) {
this.stars = stars;
}
}

作为数据获取器方法参数的输入参数
该框架使获取输入参数变得更加容易。你可以指定参数作为数据获取器的方法参数。
@DgsComponent
public class RatingMutation {
@DgsData(parentType = "Mutation", field = "addRating")
public Rating addRating(@InputArgument("input") RatingInput ratingInput) {
//No need for custom parsing anymore!
System.out.println("Rated " + ratingInput.getTitle() + " with " + ratingInput.getStars() + " stars") ;
return new Rating(ratingInput.getStars());
}
}
@InputArgument注解对于指定输入参数的名称很重要,因为参数可以以任何顺序指定。如果没有注解,框架会尝试使用参数名称,但这只有在代码以特定的编译器设置进行编译时才能实现。输入类型参数可以与DataFetchingEnvironment参数相结合。
@DgsComponent
public class RatingMutation {
@DgsData(parentType = "Mutation", field = "addRating")
public Rating addRating(@InputArgument("input") RatingInput ratingInput, DataFetchingEnvironment dfe) {
//No need for custom parsing anymore!
System.out.println("Rated " + ratingInput.getTitle() + " with " + ratingInput.getStars() + " stars") ;
System.out.println("DataFetchingEnvironment: " + dfe.getArgument(ratingInput));
return new Rating(ratingInput.getStars());
}
}
建造者模式
边栏推荐
- Entropy weight method to optimize TOPSIS (matlab)
- Stm32f4 check the frequency of each part of the system
- How to migrate databases in the flask framework
- strncat() strncmp()
- 在openEuler社区开源的Embedded SIG,来聊聊它的多 OS 混合部署框架
- BGP基础实验
- Android金九银十的面试你准备的怎么样了?最新Android面试真题汇总助你备战
- Redis管道技术/分区
- [OGeek2019]babyrop
- STM32F4查看系统各部分频率
猜你喜欢

在openEuler社区开源的Embedded SIG,来聊聊它的多 OS 混合部署框架

Open source embedded sig in the openeuler community. Let's talk about its multi OS hybrid deployment framework

Lixia action 2022 Yuanqi digital round table forum will be launched soon

1000个Okaleido Tiger首发上线Binance NFT,引发抢购热潮

Open source embedded sig in the openeuler community. Let's talk about its multi OS hybrid deployment framework

Interviewer: if the order is not paid within 30 minutes after it is generated, it will be automatically cancelled. How to realize it?

Arrayslist and sequence table -- Simulation Implementation

史上最全的2022年版Android面试题

BGP基础实验

STM32F4查看系统各部分频率
随机推荐
strncat() strncmp()
Analysis of mobile semantics and perfect forwarding
1000个Okaleido Tiger首发上线Binance NFT,引发抢购热潮
ciscn_2019_n_8
[web vulnerability exploration] SQL injection vulnerability
Smart IOT source code with configuration IOT source code industrial IOT source code: support sensor analysis services, real-time data collection and remote control
Getting started database days3
The I2C interface mode offline burning operation method of h7-tool has been released (2022-07-16)
Can Intel NUC replace the host_ It's finally perfect! The latest Intel NUC Mini host comes online
anchor free yolov1
【Error】TypeError: expected str, bytes or os. PathLike object, not int
汇编语言伪指令详解(附实例)
What are the three-party payment companies?
USB转CAN设备在核酸提取仪 高性能USB接口CAN卡
[数组]NC95 数组中的最长连续子序列-较难
cmake 编译工具小记
Exch:pop3 and IMAP4 operation guide
YOLOv4: Optimal Speed and Accuracy of Object Detection
Getting started database days2
Galaxy Securities opens an account online. Is it safe to open an account on your mobile phone