Search in sources :

Example 1 with RequestInput

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

the class GraphQlMessageHandler method handleRequestMessage.

@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
    RequestInput requestInput;
    if (requestMessage.getPayload() instanceof RequestInput) {
        requestInput = (RequestInput) requestMessage.getPayload();
    } else {
        Assert.notNull(this.operationExpression, "'operationExpression' must not be null");
        String query = evaluateOperationExpression(requestMessage);
        String operationName = evaluateOperationNameExpression(requestMessage);
        Map<String, Object> variables = evaluateVariablesExpression(requestMessage);
        String id = evaluateExecutionIdExpression(requestMessage);
        requestInput = new RequestInput(query, operationName, variables, this.locale, id);
    }
    return this.graphQlService.execute(requestInput);
}
Also used : RequestInput(org.springframework.graphql.RequestInput)

Example 2 with RequestInput

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

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

the class GraphQlMessageHandlerTests method testHandleMessageForQueryWithRequestInputProvided.

@Test
@SuppressWarnings("unchecked")
void testHandleMessageForQueryWithRequestInputProvided() {
    StepVerifier verifier = StepVerifier.create(Flux.from(this.resultChannel).map(Message::getPayload).cast(RequestOutput.class)).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");
    }).thenCancel().verifyLater();
    RequestInput payload = new RequestInput("{ testQuery { id } }", null, null, null, UUID.randomUUID().toString());
    this.inputChannel.send(MessageBuilder.withPayload(payload).build());
    verifier.verify(Duration.ofSeconds(10));
}
Also used : RequestInput(org.springframework.graphql.RequestInput) StepVerifier(reactor.test.StepVerifier) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 4 with RequestInput

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

the class GraphQlMessageHandlerTests method testHandleMessageForMutationWithRequestInputProvided.

@Test
@SuppressWarnings("unchecked")
void testHandleMessageForMutationWithRequestInputProvided() {
    String fakeId = UUID.randomUUID().toString();
    Update expected = new Update(fakeId);
    StepVerifier verifier = StepVerifier.create(Flux.from(this.resultChannel).map(Message::getPayload).cast(RequestOutput.class)).consumeNextWith(result -> {
        assertThat(result).isInstanceOf(RequestOutput.class);
        Map<String, Object> data = result.getData();
        Map<String, Object> update = (Map<String, Object>) data.get("update");
        assertThat(update.get("id")).isEqualTo(fakeId);
        assertThat(this.updateRepository.current().block()).isEqualTo(expected);
    }).thenCancel().verifyLater();
    RequestInput payload = new RequestInput("mutation { update(id: \"" + fakeId + "\") { id } }", null, null, null, UUID.randomUUID().toString());
    this.inputChannel.send(MessageBuilder.withPayload(payload).build());
    verifier.verify(Duration.ofSeconds(10));
    StepVerifier.create(this.updateRepository.current()).expectNext(expected).expectComplete().verify();
}
Also used : RequestInput(org.springframework.graphql.RequestInput) StepVerifier(reactor.test.StepVerifier) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 5 with RequestInput

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

the class GraphQlTesterTests method operationNameAndVariables.

@Test
void operationNameAndVariables() {
    String document = "query HeroNameAndFriends($episode: Episode) {" + "  hero(episode: $episode) {" + "    name" + "  }" + "}";
    setMockResponse("{\"hero\": {\"name\":\"R2-D2\"}}");
    GraphQlTester.Response response = graphQlTester().document(document).operationName("HeroNameAndFriends").variable("episode", "JEDI").variable("foo", "bar").variable("keyOnly", null).execute();
    response.path("hero").entity(MovieCharacter.class).isEqualTo(MovieCharacter.create("R2-D2"));
    RequestInput input = requestInput();
    assertThat(input.getDocument()).contains(document);
    assertThat(input.getOperationName()).isEqualTo("HeroNameAndFriends");
    assertThat(input.getVariables()).hasSize(3);
    assertThat(input.getVariables()).containsEntry("episode", "JEDI");
    assertThat(input.getVariables()).containsEntry("foo", "bar");
    assertThat(input.getVariables()).containsEntry("keyOnly", null);
}
Also used : RequestInput(org.springframework.graphql.RequestInput) Test(org.junit.jupiter.api.Test)

Aggregations

RequestInput (org.springframework.graphql.RequestInput)6 Test (org.junit.jupiter.api.Test)5 Map (java.util.Map)3 StepVerifier (reactor.test.StepVerifier)3 RequestOutput (org.springframework.graphql.RequestOutput)2 GraphQLContext (graphql.GraphQLContext)1 SubscriptionPublisher (graphql.execution.reactive.SubscriptionPublisher)1 Duration (java.time.Duration)1 Locale (java.util.Locale)1 UUID (java.util.UUID)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 Bean (org.springframework.context.annotation.Bean)1 Configuration (org.springframework.context.annotation.Configuration)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1 Author (org.springframework.graphql.Author)1 GraphQlService (org.springframework.graphql.GraphQlService)1 TestRequestInput (org.springframework.graphql.TestRequestInput)1 Argument (org.springframework.graphql.data.method.annotation.Argument)1