spring-graphql icon indicating copy to clipboard operation
spring-graphql copied to clipboard

Query did not validate Date scalar Type

Open matndev opened this issue 3 years ago • 0 comments

I implemented a custom scalar for the type Date. But the tests don't pass the validation process.

schema.graphqls

scalar Date

type MyType {
    ....
    MyDate: Date

DateScalarConfiguration

@Configuration
public class DateScalarConfiguration  {

    @Bean
    public GraphQLScalarType dateScalar() {
        return GraphQLScalarType.newScalar()
                .name("Date")
                .description("Java 8 LocalDate as scalar.")
                .coercing(new Coercing<Object, Object>() {
                    @Override
                    public String serialize(final Object dataFetcherResult) {
                        if (dataFetcherResult instanceof LocalDate) {
                            return dataFetcherResult.toString();
                        } else {
                            throw new CoercingSerializeException("Expected a LocalDate object.");
                        }
                    }

                    @Override
                    public LocalDate parseValue(final Object input) {
                        try {
                            if (input instanceof String) {
                                return LocalDate.parse((String) input);
                            } else {
                                throw new CoercingParseValueException("Expected a String");
                            }
                        } catch (DateTimeParseException e) {
                            throw new CoercingParseValueException(String.format("Not a valid date: '%s'.", input), e
                            );
                        }
                    }

                    @Override
                    public LocalDate parseLiteral(final Object input) {
                        if (input instanceof StringValue) {
                            try {
                                return LocalDate.parse(((StringValue) input).getValue());
                            } catch (DateTimeParseException e) {
                                throw new CoercingParseLiteralException(e);
                            }
                        } else {
                            throw new CoercingParseLiteralException("Expected a StringValue.");
                        }
                    }
                }).build();
    }

    @Bean
    RuntimeWiringConfigurer configurer() {
        GraphQLScalarType scalarType = dateScalar();
        return (builder) -> builder.scalar(scalarType);
    }
}

Test :

@GraphQlTest(MyController.class)
@Import(DateScalarConfiguration.class)

   .....

   @Test
    public void findMyObjectTest() {
        List<String> fileExtensions = Arrays.asList(".graphql", ".gql", ".graphqls");
        DocumentSource documentSource = new ResourceDocumentSource(
                Collections.singletonList(new ClassPathResource("graphql/")),
                fileExtensions); // ResourceDocumentSource.FILE_EXTENSIONS
        this.graphQlTester
                .mutate()
                .documentSource(documentSource)
                .build().documentName("schema")
                .variable("myVar",618)
                .execute()
                .path("findMyObject")
                .matchesJson("{\"data\":{\"findMyObject\":{\"ATTR1\":618,\"ATTR2\":\"1999-12-31\"}}}");
    }

Result :

WARN 19548 --- [ Test worker] notprivacysafe.graphql.GraphQL : Query did not validate : 'scalar Date

Response has 5 unexpected error(s) of 5 total. If expected, please filter them out: [ValidationError{validationErrorType=NonExecutableDefinition, queryPath=null, message=Validation error of type NonExecutableDefinition: The Date definition is not executable., locations=[SourceLocation{line=1, column=1}], description='The Date definition is not executable.'}, ValidationError{validationErrorType=NonExecutableDefinition, queryPath=null, message=Validation error of type NonExecutableDefinition: The Query definition is not executable., locations=[SourceLocation{line=3, column=1}], description='The Query definition is not executable.'}, ..........

matndev avatar Aug 09 '22 15:08 matndev