use of org.springframework.graphql.execution.DataFetcherExceptionResolverAdapter 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();
}
}
Aggregations