我为我的控制器编写了这个单元测试,我的模式包含一个Date对象,它由下面的DateScalarConfiguration实现。当我运行应用程序时,没有问题,但是当我测试它时,它会给我一个错误。这是我的代码:
@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:
@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
implementation 'org.springframework.boot:spring-boot-starter-graphql'
testImplementation 'org.springframework.graphql:spring-graphql'
testImplementation 'org.springframework.graphql:spring-graphql-test'当我做这个测试时,我得到了:
[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.'},
....我遵循一些图托斯,我认为我的代码没有什么问题?
发布于 2022-09-13 10:38:14
据我所知,问题在于'org.springframework.boot:spring-boot-starter-graphql‘--这是一个新的依赖关系,所有教程都使用'com.graphql-java-kickstart’。我在您的代码中做了一些更改,对于我的实现,我使用了一个JPA存储库"DateModelRepository“,用您自己的存储库替换它。
@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)
}
}https://stackoverflow.com/questions/73292862
复制相似问题