Spring Boot 2 / GraphQL扩展标量不起作用:异常“无法构造实例”“无法反序列化”

时间:2019-07-17 07:25:53

标签: spring-boot graphql

我已经为GraphQL API(Spring Boot 2,GraphQL Spring Boot Starter,GraphiQL)设置了有效的设置。

然后,我尝试引入由库 graphql-java-extended-scalars 提供的自定义标量(在这种情况下,对于类型为 DateScalar > java.time.LocalDate ):

我在架构中声明了自定义标量和类型,

scalar Date
...
  somedate: Date
...

我提供了GraphQLScalarType作为Spring Bean(否则服务器将无法启动):

@Bean
public GraphQLScalarType date() {
   return ExtendedScalars.Date;
}

我用 Date 执行了变异查询。

mutation {
  createSomething(
    something:{
      ...
      somedate: "2018-07-20",
      ...
    }
  )
}

但是不幸的是,我遇到了这个异常,

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `java.time.LocalDate` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)

经过几个小时的研究,我不知道有什么问题。尽管配置中提供了标量,但似乎仍未拾取标量。

GraphQLInputType看起来像这样,

@Data
public class Something implements GraphQLInputType {

    ...
    private LocalDate somedate;
    ...

    @Override
    public String getName() {
        return "Something";
    }
}

2 个答案:

答案 0 :(得分:0)

问题是您要反序列化该对象的类没有公共默认构造函数。

答案 1 :(得分:0)

我认为在为SchemaParserOptions提供经过调整的mapper配置后,它可以工作:

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

GraphQL使用其自己的objectmapper,因此必须在其上注册JavaTimeModule。