Search in sources :

Example 1 with DataFetcherExceptionResolver

use of org.springframework.graphql.execution.DataFetcherExceptionResolver 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();
}
Also used : 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) GraphQlResponse(org.springframework.graphql.GraphQlResponse) DataFetcherExceptionResolver(org.springframework.graphql.execution.DataFetcherExceptionResolver) 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