leangen / graphql-spqr

Build a GraphQL service in seconds
Apache License 2.0
1.09k stars 179 forks source link

Change Date format #499

Closed sura2k closed 3 weeks ago

sura2k commented 3 weeks ago

Is there a way to format the output Date format to something such as "yyyyMMdd" overriding the default "yyyy-MM-dd"?

Balf commented 3 weeks ago

I think what you need is a GraphQLDirective, like so: https://www.graphql-java.com/documentation/sdl-directives/#declaring-directives. However, I'm not sure how this ties into GraphQL SPQR, @kaqqao any thoughts?

sura2k commented 3 weeks ago

@Balf @kaqqao After analyzing the code, I was able to get it done.

  1. Created class CustomScalarMapper extends CachingMapper<>{} by overriding the super methods
  2. Created class CustomScalars {} which contains my java.util.Date scalar with custom date formatting
  3. Referred CustomScalars via CustomScalarMapper methods to keep it similar to the style used in the library
  4. With Spring Boot version
    @Autowired
    private GraphQLSchemaGenerator schemaGenerator;
    schemaGenerator.withTypeMappersPrepended(new CustomScalarMapper());  //Before adding to exising `ScalarMapper` is required I guess

    Since I don't have access to post anything to outside repos from my working computer, posting just only the steps from my personal computer. Hope this approach is valid.

Thanks!