Search in sources :

Example 1 with TestThreadLocalAccessor

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();
    }
}
Also used : TestThreadLocalAccessor(org.springframework.graphql.TestThreadLocalAccessor) ExecutionResult(graphql.ExecutionResult) GraphQlResponse(org.springframework.graphql.GraphQlResponse) ContextView(reactor.util.context.ContextView) Test(org.junit.jupiter.api.Test)

Example 2 with TestThreadLocalAccessor

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();
    }
}
Also used : GraphQlResponse(org.springframework.graphql.GraphQlResponse) StepVerifier(reactor.test.StepVerifier) GraphQL(graphql.GraphQL) ContextView(reactor.util.context.ContextView) Context(reactor.util.context.Context) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) GraphQlSetup(org.springframework.graphql.GraphQlSetup) Mono(reactor.core.publisher.Mono) ExecutionInput(graphql.ExecutionInput) ExecutionResult(graphql.ExecutionResult) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) List(java.util.List) Duration(java.time.Duration) TestThreadLocalAccessor(org.springframework.graphql.TestThreadLocalAccessor) TestThreadLocalAccessor(org.springframework.graphql.TestThreadLocalAccessor) GraphQL(graphql.GraphQL) ExecutionResult(graphql.ExecutionResult) ExecutionInput(graphql.ExecutionInput) ContextView(reactor.util.context.ContextView) Test(org.junit.jupiter.api.Test)

Example 3 with TestThreadLocalAccessor

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();
    }
}
Also used : Context(reactor.util.context.Context) Test(org.junit.jupiter.api.Test) Duration(java.time.Duration) Context(reactor.util.context.Context) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TestThreadLocalAccessor(org.springframework.graphql.TestThreadLocalAccessor) Mono(reactor.core.publisher.Mono) Test(org.junit.jupiter.api.Test)

Example 4 with TestThreadLocalAccessor

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();
    }
}
Also used : DataFetcherExceptionResolverAdapter(org.springframework.graphql.execution.DataFetcherExceptionResolverAdapter) DataFetcherExceptionResolverAdapter(org.springframework.graphql.execution.DataFetcherExceptionResolverAdapter) GraphQlResponse(org.springframework.graphql.GraphQlResponse) HttpHeaders(org.springframework.http.HttpHeaders) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) GraphQlSetup(org.springframework.graphql.GraphQlSetup) Mono(reactor.core.publisher.Mono) GraphqlErrorBuilder(graphql.GraphqlErrorBuilder) Test(org.junit.jupiter.api.Test) ErrorType(org.springframework.graphql.execution.ErrorType) Duration(java.time.Duration) DataFetcher(graphql.schema.DataFetcher) TestThreadLocalAccessor(org.springframework.graphql.TestThreadLocalAccessor) URI(java.net.URI) Collections(java.util.Collections) DataFetcherExceptionResolver(org.springframework.graphql.execution.DataFetcherExceptionResolver) TestThreadLocalAccessor(org.springframework.graphql.TestThreadLocalAccessor) GraphQlResponse(org.springframework.graphql.GraphQlResponse) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)4 TestThreadLocalAccessor (org.springframework.graphql.TestThreadLocalAccessor)4 Duration (java.time.Duration)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 GraphQlResponse (org.springframework.graphql.GraphQlResponse)3 Mono (reactor.core.publisher.Mono)3 ExecutionResult (graphql.ExecutionResult)2 GraphQlSetup (org.springframework.graphql.GraphQlSetup)2 Context (reactor.util.context.Context)2 ContextView (reactor.util.context.ContextView)2 ExecutionInput (graphql.ExecutionInput)1 GraphQL (graphql.GraphQL)1 GraphqlErrorBuilder (graphql.GraphqlErrorBuilder)1 DataFetcher (graphql.schema.DataFetcher)1 URI (java.net.URI)1 Collections (java.util.Collections)1 List (java.util.List)1 DataFetcherExceptionResolver (org.springframework.graphql.execution.DataFetcherExceptionResolver)1 DataFetcherExceptionResolverAdapter (org.springframework.graphql.execution.DataFetcherExceptionResolverAdapter)1 ErrorType (org.springframework.graphql.execution.ErrorType)1