Skip to content
This repository has been archived by the owner on Dec 19, 2023. It is now read-only.

ObjectMapperConfigurer not working (anymore) #97

Closed
mxab opened this issue Jul 22, 2018 · 13 comments
Closed

ObjectMapperConfigurer not working (anymore) #97

mxab opened this issue Jul 22, 2018 · 13 comments

Comments

@mxab
Copy link

mxab commented Jul 22, 2018

Hi,
I registered a graphql.servlet.ObjectMapperConfigurer bean that applies the JavaTimeModule but it seems that it does not work and I'm getting the error:

Cannot construct instance of `java.time.LocalDateTime` (no Creators...

See this showcase test

It show cases a mutation that creates a post. The schema contains a PostInput with a date string field. The Java PostInput type is a LocalDateTime.

The full diff: master...mxab:javatime

@oliemansm
Copy link
Member

I'm not seeing the related PostInput class in your branch. I think you haven't pushed everything yet? And the .graphqls file shows the following definition currently using a String instead of your own custom scalar for a Date or DateTime. I'm assuming in your local version that's different seeing the errors you're getting?

input PostInput {
    date: String
}

You could try the following config for your ObjectMapperConfigurer:

@Bean
public ObjectMapperConfigurer objectMapperConfigurer(){
    return mapper -> mapper.registerModule(new JavaTimeModule())
            .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
}

Because of the mentioned missing items I couldn't really test it properly with the test case you provided.

Nice improvements to that test util! Was thinking about extracting it together with some other features into a graphql-spring-boot-starter-test to ease testing of GraphQL.

@mxab
Copy link
Author

mxab commented Jul 22, 2018

Hi,
the PostInput class is a static class inside the Mutation class.
As far as I understand the project you don't necessarily need a custom scalar as the mapping happens from a Mapwith the input value to the target class

@mxab
Copy link
Author

mxab commented Jul 22, 2018

I like the idea with regarding the graphql-spring-boot-starter-test

@oliemansm
Copy link
Member

Found the PostInput class, thanks. Will take a look.

@mxab
Copy link
Author

mxab commented Jul 22, 2018

Hi,
I also did some more debugging, it looks like the configured ObjectMapper is not used for the actual parsing.

I noticed that the acutal parsing happens from the graphql java tools which configures its own mapper.
After providing SchemaParserOptions with a objectMapperConfigurer it works:

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

@oliemansm
Copy link
Member

Just saw that in of my own projects as well how to configure it. Glad you found it. There's already an open issue if it's possible to just use one ObjectMapperConfigurer, but there were some concerns about that idea, see #65.

@mxab
Copy link
Author

mxab commented Jul 22, 2018

What confuses me know is what the purpose of the graphql.servlet.ObjectMapperConfigurer is and what object mapper it configures as it seems that its mapper is not used at all

@oliemansm
Copy link
Member

Quickly looking at the current branch it indeed doesn't seem to be used. If you check the subscription-support branch for graphql-servlet you'll see that it is used there (through the GraphQLObjectMapper). See #95 and Subscription support.

@mxab
Copy link
Author

mxab commented Jul 24, 2018

Hi,
I haven't looked at the whole java-graphql tool ecosystem, but I don't understand why you would need to different interfaces of ObjectMapperConfigurer for one setup. (javax.servlet/com.coxautodev.graphql.tools). And from what I understand they both have nothing to do with spring boots default objectmapper

@oliemansm
Copy link
Member

Don't know that either, perhaps it can be cleaned up. @apottere Any light you could shed on this?

@mxab
Copy link
Author

mxab commented Aug 15, 2018

Hi, I saw that on the graphql.tools there is a ObjectMapper is created for each fielddefintion it encounters, which is a valid option. I toyed a bit arround to allow unifying the object mappers . see further comments here: #65 (comment)

@oliemansm oliemansm added this to In progress in GraphQL Spring Boot Aug 25, 2018
GraphQL Spring Boot automation moved this from In progress to Done Aug 25, 2018
@dhudec
Copy link

dhudec commented Nov 6, 2018

The resolution here is not completely clear. I would like to see the documentation improved, at the very least. I had to implement a combination of an ObjectMapperConfigurer with a custom schemaParserOptions bean, as maxb pointed out.

@Nxtra
Copy link

Nxtra commented Aug 12, 2020

The resolution here is not completely clear. I would like to see the documentation improved, at the very least. I had to implement a combination of an ObjectMapperConfigurer with a custom schemaParserOptions bean, as maxb pointed out.

Sounds great. Can you share how you did that?
I created this issue: #447. Because I cannot figure it out from the docs.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
Development

No branches or pull requests

4 participants