首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于单元测试的GraphQL java spring启动插件无法识别的自定义标量日期

用于单元测试的GraphQL java spring启动插件无法识别的自定义标量日期
EN

Stack Overflow用户
提问于 2022-08-09 13:40:41
回答 1查看 448关注 0票数 0

我为我的控制器编写了这个单元测试,我的模式包含一个Date对象,它由下面的DateScalarConfiguration实现。当我运行应用程序时,没有问题,但是当我测试它时,它会给我一个错误。这是我的代码:

代码语言:javascript
复制
@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\"}}}");
    }

DateScalarConfiguration:

代码语言:javascript
复制
@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);
    }
}

使用此配置:

build.gradle

代码语言:javascript
复制
    implementation 'org.springframework.boot:spring-boot-starter-graphql'
    testImplementation 'org.springframework.graphql:spring-graphql'
    testImplementation 'org.springframework.graphql:spring-graphql-test'

当我做这个测试时,我得到了:

代码语言:javascript
复制
[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.'},
....

我遵循一些图托斯,我认为我的代码没有什么问题?

EN

回答 1

Stack Overflow用户

发布于 2022-09-13 10:38:14

据我所知,问题在于'org.springframework.boot:spring-boot-starter-graphql‘--这是一个新的依赖关系,所有教程都使用'com.graphql-java-kickstart’。我在您的代码中做了一些更改,对于我的实现,我使用了一个JPA存储库"DateModelRepository“,用您自己的存储库替换它。

代码语言:javascript
复制
@Configuration
public class DateScalarConfiguration  {
@Bean
public RuntimeWiringConfigurer dateWiring(DateModelRepository dateModelRepository){
var dateScalar = 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();
}

return wiringBuilder -> wiringBuilder.scalar(dateScalar)
}
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73292862

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档