2
votes

I've got a working setup for a GraphQL API (Spring Boot 2, GraphQL Spring Boot Starter, GraphiQL).

Then I tried to introduce a custom scalar provided by the library graphql-java-extended-scalars (in this case the DateScalar for a member with the type java.time.LocalDate):

I declared the custom scalar and the type in the schema,

scalar Date
...
  somedate: Date
...

I provided GraphQLScalarType as a Spring Bean (otherwise the server wouldn't start up):

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

I executed a mutation query with a Date.

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

But unfortunately I get this exception,

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)

After some hours of research, I have no idea what could be wrong. It seems that the Scalar was not picked up despite provided in the configuration.

The GraphQLInputType looks like this,

@Data
public class Something implements GraphQLInputType {

    ...
    private LocalDate somedate;
    ...

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

2 Answers

5
votes

I think after providing SchemaParserOptions with a adjusted configuration of mapper it works:

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

GraphQL uses its own objectmapper, therefore you have to register the JavaTimeModule on it.

0
votes

The problem is that the class where you want to deserialize the object it doesn't have a public default constructor.