How to configure GraphQL SPQR to use Gson instead of Jackson

549 Views Asked by At

I noticed that GraphQL-SPQR has at least a couple properties that you can configure (e.g. graphql.spqr.gui.enabled=true). Is there a configuration for specifying to use Gson instead of Jackson for object serialization/deserialization?

If there is not configuration, is there some other way I can use Gson instead of Jackson for GraphQL-SPQR?

NOTE: I am already using spring.http.converters.preferred-json-mapper=gson in my application.properties, to override spring in general to use Gson instead of Jackson, but it doesn't seem to override GraphQL-SPQR's value mapper (defaults to Jackson).

Any help is greatly appreciated :)

1

There are 1 best solutions below

0
On BEST ANSWER

All you need to do is register a ValueMapperFactory bean, e.g.

@Bean
public ValueMapperFactory gson() {
    return new GsonValueMapperFactory();
}

To customize the factory, use the builder instead of the constructor:

@Bean
public ValueMapperFactory gson(Gson gson) {
    return GsonValueMapperFactory.builder()
            .withPrototype(gson)
            .build();
}

You can find most customizable beans listed here.

I also think respecting spring.http.converters.preferred-json-mapper is a great idea. I'll add that in a future release. Follow this issue for progress.

Be warned though that Gson mapping is much simpler than Jackson's. It has a lot less features, only takes fields into accounts (not constructor parameters or setters) and will likely not behave exactly the same as Jackson-based mapping. But I guess you're aware of that since you explicitly wish to use it.