Search in sources :

Example 1 with ErrorType

use of org.springframework.graphql.execution.ErrorType 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)

Example 2 with ErrorType

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