Search in sources :

Example 6 with GraphQlResponse

use of org.springframework.graphql.GraphQlResponse 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 7 with GraphQlResponse

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

Example 8 with GraphQlResponse

use of org.springframework.graphql.GraphQlResponse in project spring-graphql by spring-projects.

the class ExceptionResolversExceptionHandlerTests method unresolvedException.

@Test
void unresolvedException() throws Exception {
    DataFetcherExceptionResolverAdapter resolver = DataFetcherExceptionResolverAdapter.from((ex, env) -> null);
    ExecutionResult result = this.graphQlSetup.exceptionResolver(resolver).toGraphQl().executeAsync(this.input).get();
    GraphQlResponse response = GraphQlResponse.from(result);
    assertThat(response.errorCount()).isEqualTo(1);
    assertThat(response.error(0).message()).isEqualTo("Invalid greeting");
    assertThat(response.error(0).errorType()).isEqualTo("INTERNAL_ERROR");
    String greeting = response.rawValue("greeting");
    assertThat(greeting).isNull();
}
Also used : ExecutionResult(graphql.ExecutionResult) GraphQlResponse(org.springframework.graphql.GraphQlResponse) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)8 GraphQlResponse (org.springframework.graphql.GraphQlResponse)8 ExecutionResult (graphql.ExecutionResult)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 GraphQlSetup (org.springframework.graphql.GraphQlSetup)4 TestThreadLocalAccessor (org.springframework.graphql.TestThreadLocalAccessor)4 Mono (reactor.core.publisher.Mono)4 GraphqlErrorBuilder (graphql.GraphqlErrorBuilder)3 Duration (java.time.Duration)3 Collections (java.util.Collections)3 DataFetcher (graphql.schema.DataFetcher)2 URI (java.net.URI)2 List (java.util.List)2 RequestOutput (org.springframework.graphql.RequestOutput)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 ContextView (reactor.util.context.ContextView)2 JsonIgnoreProperties (com.fasterxml.jackson.annotation.JsonIgnoreProperties)1