Search in sources :

Example 1 with RequestOutput

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

the class GraphQlMessageHandlerTests method testHandleMessageForSubscriptionWithRequestInputProvided.

@Test
@SuppressWarnings("unchecked")
void testHandleMessageForSubscriptionWithRequestInputProvided() {
    StepVerifier verifier = StepVerifier.create(Flux.from(this.resultChannel).map(Message::getPayload).cast(RequestOutput.class).mapNotNull(RequestOutput::getData).cast(SubscriptionPublisher.class).map(Flux::from).flatMap(data -> data)).consumeNextWith(requestOutput -> {
        Map<String, Object> results = requestOutput.getData();
        assertThat(results).containsKey("results");
        Map<String, Object> operationResult = (Map<String, Object>) results.get("results");
        assertThat(operationResult).containsKey("id").containsValue("test-data-01");
    }).expectNextCount(9).thenCancel().verifyLater();
    RequestInput payload = new RequestInput("subscription { results { id } }", null, null, null, UUID.randomUUID().toString());
    this.inputChannel.send(MessageBuilder.withPayload(payload).build());
    verifier.verify(Duration.ofSeconds(10));
}
Also used : DirtiesContext(org.springframework.test.annotation.DirtiesContext) QueueChannel(org.springframework.integration.channel.QueueChannel) StepVerifier(reactor.test.StepVerifier) IntegrationFlow(org.springframework.integration.dsl.IntegrationFlow) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ClassPathResource(org.springframework.core.io.ClassPathResource) ExecutionGraphQlService(org.springframework.graphql.execution.ExecutionGraphQlService) Autowired(org.springframework.beans.factory.annotation.Autowired) FluxMessageChannel(org.springframework.integration.channel.FluxMessageChannel) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Controller(org.springframework.stereotype.Controller) Argument(org.springframework.graphql.data.method.annotation.Argument) RequestInput(org.springframework.graphql.RequestInput) MessageChannels(org.springframework.integration.dsl.MessageChannels) SpringJUnitConfig(org.springframework.test.context.junit.jupiter.SpringJUnitConfig) MessageHandlingException(org.springframework.messaging.MessageHandlingException) Locale(java.util.Locale) Duration(java.time.Duration) Map(java.util.Map) GraphQlService(org.springframework.graphql.GraphQlService) SubscriptionMapping(org.springframework.graphql.data.method.annotation.SubscriptionMapping) IntegrationFlows(org.springframework.integration.dsl.IntegrationFlows) Message(org.springframework.messaging.Message) PollableChannel(org.springframework.messaging.PollableChannel) Repository(org.springframework.stereotype.Repository) QueryMapping(org.springframework.graphql.data.method.annotation.QueryMapping) MutationMapping(org.springframework.graphql.data.method.annotation.MutationMapping) SubscriptionPublisher(graphql.execution.reactive.SubscriptionPublisher) Mono(reactor.core.publisher.Mono) UUID(java.util.UUID) EnableIntegration(org.springframework.integration.config.EnableIntegration) Test(org.junit.jupiter.api.Test) Configuration(org.springframework.context.annotation.Configuration) Flux(reactor.core.publisher.Flux) GraphQlSource(org.springframework.graphql.execution.GraphQlSource) AnnotatedControllerConfigurer(org.springframework.graphql.data.method.annotation.support.AnnotatedControllerConfigurer) Bean(org.springframework.context.annotation.Bean) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageBuilder(org.springframework.messaging.support.MessageBuilder) RequestOutput(org.springframework.graphql.RequestOutput) RequestOutput(org.springframework.graphql.RequestOutput) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) SubscriptionPublisher(graphql.execution.reactive.SubscriptionPublisher) Flux(reactor.core.publisher.Flux) RequestInput(org.springframework.graphql.RequestInput) StepVerifier(reactor.test.StepVerifier) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 2 with RequestOutput

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

the class GraphQlMessageHandlerTests method testHandleMessageForQueryWithQueryProvided.

@Test
@SuppressWarnings("unchecked")
void testHandleMessageForQueryWithQueryProvided() {
    String fakeQuery = "{ testQuery { id } }";
    this.graphQlMessageHandler.setOperation(fakeQuery);
    Locale locale = Locale.getDefault();
    this.graphQlMessageHandler.setLocale(locale);
    Mono<RequestOutput> resultMono = (Mono<RequestOutput>) this.graphQlMessageHandler.handleRequestMessage(new GenericMessage<>(fakeQuery));
    StepVerifier.create(resultMono).consumeNextWith(result -> {
        assertThat(result).isInstanceOf(RequestOutput.class);
        Map<String, Object> data = result.getData();
        Map<String, Object> testQuery = (Map<String, Object>) data.get("testQuery");
        assertThat(testQuery.get("id")).isEqualTo("test-data");
    }).expectComplete().verify();
}
Also used : Locale(java.util.Locale) RequestOutput(org.springframework.graphql.RequestOutput) GenericMessage(org.springframework.messaging.support.GenericMessage) Mono(reactor.core.publisher.Mono) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 3 with RequestOutput

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

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

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

the class SchemaMappingInvocationTests method queryWithScalarArgument.

@Test
void queryWithScalarArgument() {
    String document = "{ " + "  bookById(id:\"1\") { " + "    id" + "    name" + "    author {" + "      firstName" + "      lastName" + "    }" + "  }" + "}";
    Mono<RequestOutput> resultMono = graphQlService().execute(TestRequestInput.forDocument(document));
    Book book = GraphQlResponse.from(resultMono).toEntity("bookById", Book.class);
    assertThat(book.getId()).isEqualTo(1);
    assertThat(book.getName()).isEqualTo("Nineteen Eighty-Four");
    Author author = book.getAuthor();
    assertThat(author.getFirstName()).isEqualTo("George");
    assertThat(author.getLastName()).isEqualTo("Orwell");
}
Also used : RequestOutput(org.springframework.graphql.RequestOutput) Book(org.springframework.graphql.Book) Author(org.springframework.graphql.Author) Test(org.junit.jupiter.api.Test)

Aggregations

RequestOutput (org.springframework.graphql.RequestOutput)12 Test (org.junit.jupiter.api.Test)9 Mono (reactor.core.publisher.Mono)5 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 Author (org.springframework.graphql.Author)4 GraphQlResponse (org.springframework.graphql.GraphQlResponse)4 TestRequestInput (org.springframework.graphql.TestRequestInput)4 ExecutionInput (graphql.ExecutionInput)3 Map (java.util.Map)3 GraphQlSetup (org.springframework.graphql.GraphQlSetup)3 Flux (reactor.core.publisher.Flux)3 ExecutionResult (graphql.ExecutionResult)2 ExecutionResultImpl (graphql.ExecutionResultImpl)2 Duration (java.time.Duration)2 List (java.util.List)2 Locale (java.util.Locale)2 Book (org.springframework.graphql.Book)2 GraphQlService (org.springframework.graphql.GraphQlService)2 QueryMapping (org.springframework.graphql.data.method.annotation.QueryMapping)2 SubscriptionMapping (org.springframework.graphql.data.method.annotation.SubscriptionMapping)2