use of org.springframework.graphql.GraphQlResponse in project spring-graphql by spring-projects.
the class ClassNameTypeResolverTests method typeResolutionViaMapping.
@Test
void typeResolutionViaMapping() {
String document = "" + "query Sightings {" + " sightings {" + " __typename" + " ... on Bird {" + " name" + " }" + " ... on Mammal {" + " name" + " }" + " ... on Plant {" + " family" + " }" + " }" + "}";
ClassNameTypeResolver typeResolver = new ClassNameTypeResolver();
typeResolver.addMapping(Tree.class, "Plant");
Mono<RequestOutput> result = graphQlSetup.queryFetcher("sightings", env -> animalAndPlantList).typeResolver(typeResolver).toGraphQlService().execute(TestRequestInput.forDocument(document));
GraphQlResponse response = GraphQlResponse.from(result);
for (int i = 0; i < animalAndPlantList.size(); i++) {
Object sighting = animalAndPlantList.get(i);
if (sighting instanceof Animal) {
Animal animal = (Animal) response.toEntity("sightings[" + i + "]", sighting.getClass());
assertThat(animal.getName()).isEqualTo(((Animal) sighting).getName());
} else if (sighting instanceof Tree) {
Tree tree = (Tree) response.toEntity("sightings[" + i + "]", sighting.getClass());
assertThat(tree.getFamily()).isEqualTo(((Tree) sighting).getFamily());
} else {
throw new IllegalStateException();
}
}
}
use of org.springframework.graphql.GraphQlResponse in project spring-graphql by spring-projects.
the class ClassNameTypeResolverTests method typeResolutionViaSuperHierarchy.
@Test
void typeResolutionViaSuperHierarchy() {
String document = "" + "query Animals {" + " animals {" + " __typename" + " name" + " ... on Bird {" + " flightless" + " }" + " ... on Mammal {" + " herbivore" + " }" + " }" + "}";
Mono<RequestOutput> resultMono = graphQlSetup.queryFetcher("animals", env -> animalList).toGraphQlService().execute(TestRequestInput.forDocument(document));
GraphQlResponse response = GraphQlResponse.from(resultMono);
for (int i = 0; i < animalList.size(); i++) {
Animal animal = animalList.get(i);
if (animal instanceof Bird) {
Bird bird = (Bird) response.toEntity("animals[" + i + "]", animal.getClass());
assertThat(bird.isFlightless()).isEqualTo(((Bird) animal).isFlightless());
} else if (animal instanceof Mammal) {
Mammal mammal = (Mammal) response.toEntity("animals[" + i + "]", animal.getClass());
assertThat(mammal.isHerbivore()).isEqualTo(((Mammal) animal).isHerbivore());
} else {
throw new IllegalStateException();
}
}
}
use of org.springframework.graphql.GraphQlResponse in project spring-graphql by spring-projects.
the class ExceptionResolversExceptionHandlerTests method resolveExceptionWithThreadLocal.
@Test
void resolveExceptionWithThreadLocal() {
ThreadLocal<String> nameThreadLocal = new ThreadLocal<>();
nameThreadLocal.set("007");
TestThreadLocalAccessor<String> accessor = new TestThreadLocalAccessor<>(nameThreadLocal);
try {
DataFetcherExceptionResolverAdapter resolver = DataFetcherExceptionResolverAdapter.from((ex, env) -> GraphqlErrorBuilder.newError(env).message("Resolved error: " + ex.getMessage() + ", name=" + nameThreadLocal.get()).errorType(ErrorType.BAD_REQUEST).build());
resolver.setThreadLocalContextAware(true);
ContextView view = ReactorContextManager.extractThreadLocalValues(accessor, Context.empty());
ReactorContextManager.setReactorContext(view, input);
Mono<ExecutionResult> result = Mono.delay(Duration.ofMillis(10)).flatMap((aLong) -> Mono.fromFuture(this.graphQlSetup.exceptionResolver(resolver).toGraphQl().executeAsync(this.input)));
GraphQlResponse response = GraphQlResponse.from(result);
assertThat(response.errorCount()).isEqualTo(1);
assertThat(response.error(0).message()).isEqualTo("Resolved error: Invalid greeting, name=007");
} finally {
nameThreadLocal.remove();
}
}
use of org.springframework.graphql.GraphQlResponse in project spring-graphql by spring-projects.
the class ExceptionResolversExceptionHandlerTests method resolveExceptionWithReactorContext.
@Test
void resolveExceptionWithReactorContext() throws Exception {
DataFetcherExceptionResolver resolver = (ex, env) -> Mono.deferContextual((view) -> Mono.just(Collections.singletonList(GraphqlErrorBuilder.newError(env).message("Resolved error: " + ex.getMessage() + ", name=" + view.get("name")).errorType(ErrorType.BAD_REQUEST).build())));
ReactorContextManager.setReactorContext(Context.of("name", "007"), input);
ExecutionResult result = this.graphQlSetup.exceptionResolver(resolver).toGraphQl().executeAsync(this.input).get();
GraphQlResponse response = GraphQlResponse.from(result);
assertThat(response.errorCount()).isEqualTo(1);
assertThat(response.error(0).message()).isEqualTo("Resolved error: Invalid greeting, name=007");
}
use of org.springframework.graphql.GraphQlResponse in project spring-graphql by spring-projects.
the class ExceptionResolversExceptionHandlerTests method resolveException.
@Test
void resolveException() throws Exception {
DataFetcherExceptionResolver resolver = DataFetcherExceptionResolverAdapter.from((ex, env) -> GraphqlErrorBuilder.newError(env).message("Resolved error: " + ex.getMessage()).errorType(ErrorType.BAD_REQUEST).build());
ExecutionResult result = this.graphQlSetup.exceptionResolver(resolver).toGraphQl().executeAsync(this.input).get();
GraphQlResponse response = GraphQlResponse.from(result);
assertThat(response.errorCount()).isEqualTo(1);
assertThat(response.error(0).message()).isEqualTo("Resolved error: Invalid greeting");
assertThat(response.error(0).errorType()).isEqualTo("BAD_REQUEST");
String greeting = response.rawValue("greeting");
assertThat(greeting).isNull();
}
Aggregations