Search in sources :

Example 1 with DataFetcherExceptionResolverAdapter

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();
    }
}
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

GraphqlErrorBuilder (graphql.GraphqlErrorBuilder)1 DataFetcher (graphql.schema.DataFetcher)1 URI (java.net.URI)1 Duration (java.time.Duration)1 Collections (java.util.Collections)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Test (org.junit.jupiter.api.Test)1 GraphQlResponse (org.springframework.graphql.GraphQlResponse)1 GraphQlSetup (org.springframework.graphql.GraphQlSetup)1 TestThreadLocalAccessor (org.springframework.graphql.TestThreadLocalAccessor)1 DataFetcherExceptionResolver (org.springframework.graphql.execution.DataFetcherExceptionResolver)1 DataFetcherExceptionResolverAdapter (org.springframework.graphql.execution.DataFetcherExceptionResolverAdapter)1 ErrorType (org.springframework.graphql.execution.ErrorType)1 HttpHeaders (org.springframework.http.HttpHeaders)1 Mono (reactor.core.publisher.Mono)1