GraphQL添加自定义标量

继承GraphQLScalarType类,然后重写父类的构造器,传入三个参数。这个方法已经过时了,最新的构造器需要传入5个参数,都有非空校验

private GraphQLScalarType(String name, String description, Coercing<?, ?> coercing, List<GraphQLDirective> directives, List<GraphQLAppliedDirective> appliedDirectives, ScalarTypeDefinition definition, List<ScalarTypeExtensionDefinition> extensionDefinitions, String specifiedByUrl) {
        Assert.assertValidName(name);
        Assert.assertNotNull(coercing, () -> {
            return "coercing can't be null";
        });
        Assert.assertNotNull(directives, () -> {
            return "directives can't be null";
        });
        this.name = name;
        this.description = description;
        this.coercing = coercing;
        this.definition = definition;
        this.directivesHolder = new DirectivesHolder(directives, appliedDirectives);
        this.extensionDefinitions = ImmutableList.copyOf(extensionDefinitions);
        this.specifiedByUrl = specifiedByUrl;
    }
可用的方法为,直接向Spring容器中注入一个GraphQLScalarType对象
比如我向Spring容器中添加了一个Date类型的自定义标量
@Bean
    public GraphQLScalarType graphQLDate() {
        return GraphQLScalarType
                .newScalar()
                .name("Date")
                .description("Date 类型")
                .coercing(new Coercing<Date, String>() {
                    @Override
                    public String serialize(Object dataFetcherResult) throws CoercingSerializeException {
                        return new SimpleDateFormat(DATE_FORMAT_PATTERN_DEFAULT).format((Date) dataFetcherResult);
                    }

                    @Override
                    public Date parseValue(Object input) throws CoercingParseValueException {
                        if (input instanceof String) {
                            try {
                                return new SimpleDateFormat(DATE_FORMAT_PATTERN_DEFAULT).parse((String) input);
                            } catch (ParseException e) {
                                throw new CoercingParseValueException(e);
                            }
                        }
                        throw new CoercingParseValueException(
                                "Expected a 'String' but was '" + Kit.typeName(input) + "'."
                        );
                    }

                    @Override
                    public Date parseLiteral(Object input) throws CoercingParseLiteralException {
                        if (!(input instanceof StringValue)) {
                            throw new CoercingParseLiteralException(
                                    "Expected AST type 'StringValue' but was '" + input + "'."
                            );
                        }
                        try {
                            return new SimpleDateFormat(DATE_FORMAT_PATTERN_DEFAULT).parse(((StringValue) input).getValue());
                        } catch (ParseException e) {
                            throw new CoercingParseValueException(e);
                        }
                    }
                })
                .build();
    }

这样就可以在graphQL的文件中添加Date类型

#scalar定义一个数据模型
scalar Date

type Query{
    events1: [Date!]!
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值