use of org.springframework.graphql.TestThreadLocalAccessor 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.TestThreadLocalAccessor in project spring-graphql by spring-projects.
the class ContextDataFetcherDecoratorTests method dataFetcherWithThreadLocalContext.
@Test
void dataFetcherWithThreadLocalContext() {
ThreadLocal<String> nameThreadLocal = new ThreadLocal<>();
nameThreadLocal.set("007");
TestThreadLocalAccessor<String> accessor = new TestThreadLocalAccessor<>(nameThreadLocal);
try {
GraphQL graphQl = GraphQlSetup.schemaContent("type Query { greeting: String }").queryFetcher("greeting", (env) -> "Hello " + nameThreadLocal.get()).toGraphQl();
ExecutionInput input = ExecutionInput.newExecutionInput().query("{ greeting }").build();
ContextView view = ReactorContextManager.extractThreadLocalValues(accessor, Context.empty());
ReactorContextManager.setReactorContext(view, input);
Mono<ExecutionResult> resultMono = Mono.delay(Duration.ofMillis(10)).flatMap((aLong) -> Mono.fromFuture(graphQl.executeAsync(input)));
String greeting = GraphQlResponse.from(resultMono).toEntity("greeting", String.class);
assertThat(greeting).isEqualTo("Hello 007");
} finally {
nameThreadLocal.remove();
}
}
use of org.springframework.graphql.TestThreadLocalAccessor in project spring-graphql by spring-projects.
the class ReactorContextManagerTests method restoreThreadLocaValues.
@Test
void restoreThreadLocaValues() {
ThreadLocal<String> threadLocal = new ThreadLocal<>();
threadLocal.set("myValue");
Context context = ReactorContextManager.extractThreadLocalValues(new TestThreadLocalAccessor<>(threadLocal), Context.empty());
try {
Mono.delay(Duration.ofMillis(10)).doOnNext(aLong -> {
assertThat(threadLocal.get()).isNull();
ReactorContextManager.restoreThreadLocalValues(context);
assertThat(threadLocal.get()).isEqualTo("myValue");
ReactorContextManager.resetThreadLocalValues(context);
}).block();
} finally {
threadLocal.remove();
}
}
use of org.springframework.graphql.TestThreadLocalAccessor 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