Spring Boot 自定义GraphQL标量LocalDateTime

zfciruhq  于 2023-02-04  发布在  Spring
关注(0)|答案(3)|浏览(233)

我正在使用graphql-spring-boot-starter来创建GraphQL API。AFAIK没有默认的LocalDateTime标量,所以我想创建我的自定义标量。我尝试这样做:

@Bean
public GraphQLScalarType dateTimeType() {
    return new GraphQLScalarType("LocalDateTime", "DataTime scalar", new Coercing() {
        @Override
        public String serialize(Object input) {
            LocalDateTime date = (LocalDateTime) input;
            return   date.toString();
        }

        @Override
        public LocalDateTime parseValue(Object input) {
            return LocalDateTime.parse((String) input);
        }

        @Override
        public LocalDateTime parseLiteral(Object input) {
            return LocalDateTime.parse((String) input);
        }
    });

}

我有个例外:
java. lang.非法参数异常:无法构造java.time.LocalDateTime的示例(不存在与默认构造类似的创建者):无法从Object值反序列化(没有基于委托或基于属性的Creator)
你能告诉我如何在我的项目中处理日期时间吗?

8aqjt8rx

8aqjt8rx1#

下面是我在项目中传递日期和时间的方法

@Component
public class DateScalarType extends GraphQLScalarType {
public DateScalarType() {
    super("Date", "Date TIME", new Coercing() {
        @Override
        public Object serialize(Object o) throws CoercingSerializeException {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.ENGLISH);
            return formatter.format((Date)o);
        }

        @Override
        public Object parseValue(Object o) throws CoercingParseValueException {
            return o;
        }

        @Override
        public Object parseLiteral(Object o) throws CoercingParseLiteralException {
            if (o==null){
                return null;
            }
            return o.toString();
        }
    });
}}

在graphqls文件中

scalar Date

type example {
creationDate:Date! }
guicsvcw

guicsvcw2#

我认为在为SchemaParserOptions提供了经过调整的Map器配置之后,它就可以工作了:

@Bean
public SchemaParserOptions schemaParserOptions(){
   return SchemaParserOptions.newOptions().objectMapperConfigurer((mapper, context) -> {
        mapper.registerModule(new JavaTimeModule());
    }).build();
}

GraphQL使用它自己的对象Map器,因此您必须在它上面注册JavaTimeModule。
更多信息,例如这里:github.com/graphql-java-kickstart/graphql-spring-boot/issues/32
感谢“弗洛里安·谢克纳”奥斯特博士

zbdgwd5y

zbdgwd5y3#

添加Bean

@Bean
    public GraphQLScalarType timestampScalarType() {
        return GraphQLScalarType.newScalar()
                .name("Timestamp")
                .description("Timestamp scalar")
                .coercing(new Coercing() {
                    @Override
                    public String serialize(Object input) {
                        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.ENGLISH);
                        return formatter.format((Date)input);
                    }
    
                    @Override
                    public Object parseValue(Object o) throws CoercingParseValueException {
                        return o;
                    }
    
                    @Override
                    public Object parseLiteral(Object o) throws CoercingParseLiteralException {
                        return o.toString();
                    }
                })
                .build();

}

添加配置

@Bean
public RuntimeWiringConfigurer runtimeWiringConfigurer() {
    return wiringBuilder -> wiringBuilder
            .scalar(ExtendedScalars.GraphQLLong)
            .scalar(ExtendedScalars.Date)
            .scalar(timestampScalarType());
}

添加graphql文件

scalar Timestamp
......
createdAt: Timestamp
updatedAt: Timestamp
deletedAt: Timestamp

相关问题