当前位置:网站首页>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());
}
}
建造者模式
边栏推荐
- JS convert numbers to uppercase
- Brief analysis of compiling principle of.Net CLR R2R
- BGP basic experiment
- solo 文章标题会过滤掉部分标签
- [leetcode ladder] linked list · 206 reverse linked list
- Wechat applet implements a global event bus by itself
- Galaxy Securities opens an account online. Is it safe to open an account on your mobile phone
- No wonder the application effect of ERP in domestic enterprises is generally not ideal
- DHCP: prevent rogue DHCP server in the network
- USB转CAN设备在核酸提取仪 高性能USB接口CAN卡
猜你喜欢
![[ CTF ]天格战队WriteUp-首届数字空间安全攻防大赛(初赛)](/img/61/5547822b782043672b626f6b86d304.png)
[ CTF ]天格战队WriteUp-首届数字空间安全攻防大赛(初赛)

1、 Simplification of digital logic

TAP 系列文章4 | 基于 Backstage 的 TAP 开发者门户

Tap series article 8 | tap Learning Center - learn through hands-on tutorials

Smart IOT source code with configuration IOT source code industrial IOT source code: support sensor analysis services, real-time data collection and remote control

一,数字逻辑的化简

BGP basic experiment

Light up the LED light of little bear patting learning

Tap series article 5 | cloud native build service

SQL语句实战学习
随机推荐
[leetcode ladder] linked list · 203 remove linked list elements
Quickly learn to use file permissions
bjdctf_2020_babystack
Principal component analysis (matlab)
树形DP
二,数字逻辑功能单元
A great open source micro community light forum source code
1、 Simplification of digital logic
What is the difference between go run, go build and go install
js把数字转大写
solo 文章标题会过滤掉部分标签
Introduction to mysqlbinlog command (remote pull binlog)
Build your own target detection environment, model configuration, data configuration mmdetection
Strncat () strncmp ()
YOLOv4: Optimal Speed and Accuracy of Object Detection
Chinese NFT? NFR was born
Baidu editor uploads pictures and sets custom directories
Analytic hierarchy process (matlab)
1000 okaleido tiger launched binance NFT, triggering a rush to buy
solo 文章正文含有 &lt;&gt; 标签会影响到页面样式