Search in sources :

Example 1 with GraphQlResponse

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

the class ClassNameTypeResolverTests method typeResolutionViaMapping.

@Test
void typeResolutionViaMapping() {
    String document = "" + "query Sightings {" + "  sightings {" + "    __typename" + "    ... on Bird {" + "      name" + "    }" + "    ... on Mammal {" + "      name" + "    }" + "    ... on Plant {" + "      family" + "    }" + "  }" + "}";
    ClassNameTypeResolver typeResolver = new ClassNameTypeResolver();
    typeResolver.addMapping(Tree.class, "Plant");
    Mono<RequestOutput> result = graphQlSetup.queryFetcher("sightings", env -> animalAndPlantList).typeResolver(typeResolver).toGraphQlService().execute(TestRequestInput.forDocument(document));
    GraphQlResponse response = GraphQlResponse.from(result);
    for (int i = 0; i < animalAndPlantList.size(); i++) {
        Object sighting = animalAndPlantList.get(i);
        if (sighting instanceof Animal) {
            Animal animal = (Animal) response.toEntity("sightings[" + i + "]", sighting.getClass());
            assertThat(animal.getName()).isEqualTo(((Animal) sighting).getName());
        } else if (sighting instanceof Tree) {
            Tree tree = (Tree) response.toEntity("sightings[" + i + "]", sighting.getClass());
            assertThat(tree.getFamily()).isEqualTo(((Tree) sighting).getFamily());
        } else {
            throw new IllegalStateException();
        }
    }
}
Also used : Test(org.junit.jupiter.api.Test) Arrays(java.util.Arrays) List(java.util.List) GraphQlResponse(org.springframework.graphql.GraphQlResponse) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) GraphQlSetup(org.springframework.graphql.GraphQlSetup) Mono(reactor.core.publisher.Mono) RequestOutput(org.springframework.graphql.RequestOutput) TestRequestInput(org.springframework.graphql.TestRequestInput) JsonIgnoreProperties(com.fasterxml.jackson.annotation.JsonIgnoreProperties) RequestOutput(org.springframework.graphql.RequestOutput) GraphQlResponse(org.springframework.graphql.GraphQlResponse) Test(org.junit.jupiter.api.Test)

Example 2 with GraphQlResponse

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

the class ClassNameTypeResolverTests method typeResolutionViaSuperHierarchy.

@Test
void typeResolutionViaSuperHierarchy() {
    String document = "" + "query Animals {" + "  animals {" + "    __typename" + "    name" + "    ... on Bird {" + "      flightless" + "    }" + "    ... on Mammal {" + "      herbivore" + "    }" + "  }" + "}";
    Mono<RequestOutput> resultMono = graphQlSetup.queryFetcher("animals", env -> animalList).toGraphQlService().execute(TestRequestInput.forDocument(document));
    GraphQlResponse response = GraphQlResponse.from(resultMono);
    for (int i = 0; i < animalList.size(); i++) {
        Animal animal = animalList.get(i);
        if (animal instanceof Bird) {
            Bird bird = (Bird) response.toEntity("animals[" + i + "]", animal.getClass());
            assertThat(bird.isFlightless()).isEqualTo(((Bird) animal).isFlightless());
        } else if (animal instanceof Mammal) {
            Mammal mammal = (Mammal) response.toEntity("animals[" + i + "]", animal.getClass());
            assertThat(mammal.isHerbivore()).isEqualTo(((Mammal) animal).isHerbivore());
        } else {
            throw new IllegalStateException();
        }
    }
}
Also used : RequestOutput(org.springframework.graphql.RequestOutput) GraphQlResponse(org.springframework.graphql.GraphQlResponse) Test(org.junit.jupiter.api.Test)

Example 3 with GraphQlResponse

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

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

the class ExceptionResolversExceptionHandlerTests method resolveExceptionWithReactorContext.

@Test
void resolveExceptionWithReactorContext() throws Exception {
    DataFetcherExceptionResolver resolver = (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())));
    ReactorContextManager.setReactorContext(Context.of("name", "007"), input);
    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("Resolved error: Invalid greeting, name=007");
}
Also used : DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) GraphQlResponse(org.springframework.graphql.GraphQlResponse) 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) GraphqlErrorBuilder(graphql.GraphqlErrorBuilder) ExecutionInput(graphql.ExecutionInput) ExecutionResult(graphql.ExecutionResult) Test(org.junit.jupiter.api.Test) List(java.util.List) GraphQLError(graphql.GraphQLError) Duration(java.time.Duration) TestThreadLocalAccessor(org.springframework.graphql.TestThreadLocalAccessor) Collections(java.util.Collections) ExecutionResult(graphql.ExecutionResult) GraphQlResponse(org.springframework.graphql.GraphQlResponse) Test(org.junit.jupiter.api.Test)

Example 5 with GraphQlResponse

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

the class ExceptionResolversExceptionHandlerTests method resolveException.

@Test
void resolveException() throws Exception {
    DataFetcherExceptionResolver resolver = DataFetcherExceptionResolverAdapter.from((ex, env) -> GraphqlErrorBuilder.newError(env).message("Resolved error: " + ex.getMessage()).errorType(ErrorType.BAD_REQUEST).build());
    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("Resolved error: Invalid greeting");
    assertThat(response.error(0).errorType()).isEqualTo("BAD_REQUEST");
    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