我创建了一个对象 H1BigDecimal,它为 BigDecimal 数据类型提供了默认比例。因此,当用户查询 BigDecimal 时,他们能够确定比例是多少。如果他们愿意,用户还可以选择设置比例值。我的主要问题是如何映射我的 H1BigDecimal 对象以在用户查询 BigDecimal 时返回?
到目前为止,我尝试的是创建自定义标量类型和自定义类型映射器。此方法返回带有值字段的新标量类型(“NewBigDecimal”),但不让用户选择查询值或自行缩放值,因为它不是对象。
所以例如我做查询:
ExampleEntity_find(pageRequest:{pageNumber:0,pageSize:2}){
content{
exampleBigDecimal
}
}
}
我得到结果
"data": {
"ExampleEntity_find": {
"content": [
{
"exampleBigDecimal": {
"value": "56.0000000000"
}
}
]
}
}
}
我想要的是让用户获得默认值或根据自己的喜好调整值:
exampleBigDecimal{
value
}
or
exampleBigDeciaml{
scaledValue(5,Half_down)
}
public class H1BigDecimal{
private BigDecimal value;
public H1BigDecimal(BigDecimal value){
this.value = value;
}
@GraphQLQuery
public String getValue(){
return this.value.setScale(10, RoundingMode.HALF_UP).toString();
}
public BigDecimal asBigDecimalValue(){
return this.value.setScale(10,RoundingMode.HALF_UP);
}
@GraphQLQuery
public String getScaledValue(int scale, RoundingMode roundingMode){
return new BigDecimal(this.value.toString()).setScale(scale,roundingMode).toString();
}
}
public class h1bigDecimalMapper implements TypeMapper {
private static final GraphQLScalarType newBigDecimal = GraphQLScalarType.newScalar().name("NewBigDecimal")
.description("New Big Decimal class").coercing(new Coercing(){
private boolean isNumberIsh(Object input) {
return true;
//return input instanceof Number || input instanceof String;
}
private BigDecimal convertImpl(Object input) {
if (isNumberIsh(input)) {
try {
return new BigDecimal(input.toString());
} catch (NumberFormatException var3) {
return null;
}
} else {
return null;
}
}
@Override
public H1BigDecimal serialize(Object o) throws CoercingSerializeException {
H1BigDecimal result = new H1BigDecimal(convertImpl(o));
if (result == null) {
throw new CoercingSerializeException("Expected type 'BigDecimal' but was '" + "'.");
} else {
return result;
}
}
@Override
public H1BigDecimal parseValue(Object o) throws CoercingParseValueException {
H1BigDecimal result = new H1BigDecimal(convertImpl(o));
if (result == null) {
throw new CoercingParseValueException("Expected type 'BigDecimal' but was '" + "'.");
} else {
return result;
}
}
@Override
public H1BigDecimal parseLiteral(Object o) throws CoercingParseLiteralException {
if (o instanceof StringValue) {
try {
return new H1BigDecimal(new BigDecimal(((StringValue)o).getValue()));
} catch (NumberFormatException var3) {
throw new CoercingParseLiteralException("Unable to turn AST input into a 'BigDecimal' : '" + o + "'");
}
} else if (o instanceof IntValue) {
return new H1BigDecimal(new BigDecimal(((IntValue)o).getValue()));
} else if (o instanceof FloatValue) {
return new H1BigDecimal(((FloatValue)o).getValue());
} else {
throw new CoercingParseLiteralException("Expected AST type 'IntValue', 'StringValue' or 'FloatValue' but was '" + "'.");
}
}
}).build();
@Override
public GraphQLOutputType toGraphQLType(AnnotatedType javaType,
Set<Class<? extends TypeMapper>> mappersToSkip, TypeMappingEnvironment env) {
return newBigDecimal;
}
@Override
public GraphQLInputType toGraphQLInputType(AnnotatedType javaType,
Set<Class<? extends TypeMapper>> mappersToSkip, TypeMappingEnvironment env) {
return newBigDecimal;
}
@Override
public boolean supports(AnnotatedElement element, AnnotatedType type) {
return ClassUtils.getRawType(type.getType()).equals(BigDecimal.class);
}
}