我已经为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";
}
}
答案 0 :(得分:0)
问题是您要反序列化该对象的类没有公共默认构造函数。
答案 1 :(得分:0)
我认为在为SchemaParserOptions提供经过调整的mapper配置后,它可以工作:
@Bean
public SchemaParserOptions schemaParserOptions(){
return SchemaParserOptions.newOptions().objectMapperConfigurer((mapper, context) -> {
mapper.registerModule(new JavaTimeModule());
}).build();
}
GraphQL使用其自己的objectmapper,因此必须在其上注册JavaTimeModule。