use of org.springframework.graphql.GraphQlResponse in project spring-graphql by spring-projects.
the class WebGraphQlHandlerTests method reactorContextPropagationToExceptionResolver.
@Test
void reactorContextPropagationToExceptionResolver() {
DataFetcherExceptionResolver exceptionResolver = (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())));
Mono<WebOutput> outputMono = this.graphQlSetup.queryFetcher("greeting", this.errorDataFetcher).exceptionResolver(exceptionResolver).toWebGraphQlHandler().handleRequest(webInput).contextWrite((cxt) -> cxt.put("name", "007"));
GraphQlResponse response = GraphQlResponse.from(outputMono);
assertThat(response.errorCount()).isEqualTo(1);
assertThat(response.error(0).message()).isEqualTo("Resolved error: Invalid greeting, name=007");
String greeting = response.rawValue("greeting");
assertThat(greeting).isNull();
}
use of org.springframework.graphql.GraphQlResponse in project spring-graphql by spring-projects.
the class WebGraphQlHandlerTests method threadLocalContextPropagationToExceptionResolver.
@Test
void threadLocalContextPropagationToExceptionResolver() {
ThreadLocal<String> nameThreadLocal = new ThreadLocal<>();
nameThreadLocal.set("007");
TestThreadLocalAccessor<String> threadLocalAccessor = new TestThreadLocalAccessor<>(nameThreadLocal);
try {
DataFetcherExceptionResolverAdapter exceptionResolver = DataFetcherExceptionResolverAdapter.from((ex, env) -> GraphqlErrorBuilder.newError(env).message("Resolved error: " + ex.getMessage() + ", name=" + nameThreadLocal.get()).errorType(ErrorType.BAD_REQUEST).build());
exceptionResolver.setThreadLocalContextAware(true);
Mono<WebOutput> outputMono = this.graphQlSetup.queryFetcher("greeting", this.errorDataFetcher).exceptionResolver(exceptionResolver).webInterceptor((input, next) -> Mono.delay(Duration.ofMillis(10)).flatMap((aLong) -> next.next(input))).threadLocalAccessor(threadLocalAccessor).toWebGraphQlHandler().handleRequest(webInput);
GraphQlResponse response = GraphQlResponse.from(outputMono);
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 unresolvedException.
@Test
void unresolvedException() throws Exception {
DataFetcherExceptionResolverAdapter resolver = DataFetcherExceptionResolverAdapter.from((ex, env) -> null);
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("Invalid greeting");
assertThat(response.error(0).errorType()).isEqualTo("INTERNAL_ERROR");
String greeting = response.rawValue("greeting");
assertThat(greeting).isNull();
}
Aggregations