Search in sources :

Example 6 with WebInput

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

the class GraphQlHttpHandler method handleRequest.

/**
 * Handle GraphQL requests over HTTP.
 * @param request the incoming HTTP request
 * @return the HTTP response
 */
public Mono<ServerResponse> handleRequest(ServerRequest request) {
    return request.bodyToMono(MAP_PARAMETERIZED_TYPE_REF).flatMap((body) -> {
        WebInput input = new WebInput(request.uri(), request.headers().asHttpHeaders(), body, request.exchange().getRequest().getId(), request.exchange().getLocaleContext().getLocale());
        if (logger.isDebugEnabled()) {
            logger.debug("Executing: " + input);
        }
        return this.graphQlHandler.handleRequest(input);
    }).flatMap((output) -> {
        Map<String, Object> spec = output.toSpecification();
        if (logger.isDebugEnabled()) {
            logger.debug("Execution complete");
        }
        ServerResponse.BodyBuilder builder = ServerResponse.ok();
        builder.headers(headers -> headers.putAll(output.getResponseHeaders()));
        return builder.bodyValue(spec);
    });
}
Also used : WebGraphQlHandler(org.springframework.graphql.web.WebGraphQlHandler) WebInput(org.springframework.graphql.web.WebInput) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) ServerResponse(org.springframework.web.reactive.function.server.ServerResponse) Map(java.util.Map) Log(org.apache.commons.logging.Log) Mono(reactor.core.publisher.Mono) LogFactory(org.apache.commons.logging.LogFactory) ServerRequest(org.springframework.web.reactive.function.server.ServerRequest) Assert(org.springframework.util.Assert) ServerResponse(org.springframework.web.reactive.function.server.ServerResponse) WebInput(org.springframework.graphql.web.WebInput)

Example 7 with WebInput

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

the class WebGraphQlClientBuilderTests method mutateUrlHeaders.

@ParameterizedTest
@MethodSource("argumentSource")
void mutateUrlHeaders(ClientBuilderSetup builderSetup) {
    String url = "/graphql-one";
    // Original
    WebGraphQlClient.Builder<?> builder = builderSetup.initBuilder().url(url).headers(headers -> headers.add("h", "one"));
    WebGraphQlClient client = builder.build();
    client.document(DOCUMENT).execute().block(TIMEOUT);
    WebInput input = builderSetup.getWebInput();
    assertThat(input.getUri().toString()).isEqualTo(url);
    assertThat(input.getHeaders().get("h")).containsExactly("one");
    // Mutate to add header value
    builder = client.mutate().headers(headers -> headers.add("h", "two"));
    client = builder.build();
    client.document(DOCUMENT).execute().block(TIMEOUT);
    assertThat(builderSetup.getWebInput().getHeaders().get("h")).containsExactly("one", "two");
    // Mutate to replace header
    builder = client.mutate().header("h", "three", "four");
    client = builder.build();
    client.document(DOCUMENT).execute().block(TIMEOUT);
    input = builderSetup.getWebInput();
    assertThat(input.getUri().toString()).isEqualTo(url);
    assertThat(input.getHeaders().get("h")).containsExactly("three", "four");
}
Also used : RouterFunctions(org.springframework.web.reactive.function.server.RouterFunctions) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) DecodingException(org.springframework.core.codec.DecodingException) WebClient(org.springframework.web.reactive.function.client.WebClient) HashMap(java.util.HashMap) TestWebSocketClient(org.springframework.graphql.web.TestWebSocketClient) ExecutionResult(graphql.ExecutionResult) DocumentSource(org.springframework.graphql.support.DocumentSource) MimeType(org.springframework.util.MimeType) Duration(java.time.Duration) Map(java.util.Map) TestWebSocketConnection(org.springframework.graphql.web.TestWebSocketConnection) HttpHandlerConnector(org.springframework.test.web.reactive.server.HttpHandlerConnector) WebInterceptor(org.springframework.graphql.web.WebInterceptor) Nullable(org.springframework.lang.Nullable) WebSocketHandler(org.springframework.web.reactive.socket.WebSocketHandler) URI(java.net.URI) ResolvableType(org.springframework.core.ResolvableType) ExecutionResultImpl(graphql.ExecutionResultImpl) MethodSource(org.junit.jupiter.params.provider.MethodSource) RouterFunctions.route(org.springframework.web.reactive.function.server.RouterFunctions.route) Jackson2JsonDecoder(org.springframework.http.codec.json.Jackson2JsonDecoder) WebGraphQlHandler(org.springframework.graphql.web.WebGraphQlHandler) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) HandlerStrategies(org.springframework.web.reactive.function.server.HandlerStrategies) ExecutionInput(graphql.ExecutionInput) Test(org.junit.jupiter.api.Test) ClientCodecConfigurer(org.springframework.http.codec.ClientCodecConfigurer) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Stream(java.util.stream.Stream) WebInput(org.springframework.graphql.web.WebInput) HttpHandler(org.springframework.http.server.reactive.HttpHandler) RouterFunction(org.springframework.web.reactive.function.server.RouterFunction) ServerResponse(org.springframework.web.reactive.function.server.ServerResponse) GraphQlWebSocketHandler(org.springframework.graphql.web.webflux.GraphQlWebSocketHandler) Collections(java.util.Collections) RequestOutput(org.springframework.graphql.RequestOutput) GraphQlHttpHandler(org.springframework.graphql.web.webflux.GraphQlHttpHandler) Assert(org.springframework.util.Assert) WebInput(org.springframework.graphql.web.WebInput) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 8 with WebInput

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

the class WebGraphQlTesterBuilderTests method mutateUrlHeaders.

@ParameterizedTest
@MethodSource("argumentSource")
void mutateUrlHeaders(TesterBuilderSetup builderSetup) {
    String url = "/graphql-one";
    // Original
    WebGraphQlTester.Builder<?> builder = builderSetup.initBuilder().url(url).headers(headers -> headers.add("h", "one"));
    WebGraphQlTester tester = builder.build();
    tester.document(DOCUMENT).execute();
    WebInput input = builderSetup.getWebInput();
    assertThat(input.getUri().toString()).isEqualTo(url);
    assertThat(input.getHeaders().get("h")).containsExactly("one");
    // Mutate to add header value
    builder = tester.mutate().headers(headers -> headers.add("h", "two"));
    tester = builder.build();
    tester.document(DOCUMENT).execute();
    assertThat(builderSetup.getWebInput().getHeaders().get("h")).containsExactly("one", "two");
    // Mutate to replace header
    builder = tester.mutate().header("h", "three", "four");
    tester = builder.build();
    tester.document(DOCUMENT).execute();
    input = builderSetup.getWebInput();
    assertThat(input.getUri().toString()).isEqualTo(url);
    assertThat(input.getHeaders().get("h")).containsExactly("three", "four");
}
Also used : Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) DecodingException(org.springframework.core.codec.DecodingException) HashMap(java.util.HashMap) TestWebSocketClient(org.springframework.graphql.web.TestWebSocketClient) ExecutionResult(graphql.ExecutionResult) DocumentSource(org.springframework.graphql.support.DocumentSource) WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) MimeType(org.springframework.util.MimeType) Duration(java.time.Duration) Map(java.util.Map) TestWebSocketConnection(org.springframework.graphql.web.TestWebSocketConnection) WebInterceptor(org.springframework.graphql.web.WebInterceptor) Nullable(org.springframework.lang.Nullable) WebSocketHandler(org.springframework.web.reactive.socket.WebSocketHandler) URI(java.net.URI) ResolvableType(org.springframework.core.ResolvableType) ExecutionResultImpl(graphql.ExecutionResultImpl) MethodSource(org.junit.jupiter.params.provider.MethodSource) RouterFunctions.route(org.springframework.web.reactive.function.server.RouterFunctions.route) Jackson2JsonDecoder(org.springframework.http.codec.json.Jackson2JsonDecoder) WebGraphQlHandler(org.springframework.graphql.web.WebGraphQlHandler) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) ExecutionInput(graphql.ExecutionInput) Test(org.junit.jupiter.api.Test) ClientCodecConfigurer(org.springframework.http.codec.ClientCodecConfigurer) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Stream(java.util.stream.Stream) WebInput(org.springframework.graphql.web.WebInput) RouterFunction(org.springframework.web.reactive.function.server.RouterFunction) ServerResponse(org.springframework.web.reactive.function.server.ServerResponse) GraphQlWebSocketHandler(org.springframework.graphql.web.webflux.GraphQlWebSocketHandler) Collections(java.util.Collections) RequestOutput(org.springframework.graphql.RequestOutput) GraphQlHttpHandler(org.springframework.graphql.web.webflux.GraphQlHttpHandler) Assert(org.springframework.util.Assert) WebInput(org.springframework.graphql.web.WebInput) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

WebInput (org.springframework.graphql.web.WebInput)8 Map (java.util.Map)7 WebGraphQlHandler (org.springframework.graphql.web.WebGraphQlHandler)7 Assert (org.springframework.util.Assert)7 Mono (reactor.core.publisher.Mono)7 ExecutionResult (graphql.ExecutionResult)6 Duration (java.time.Duration)6 Collections (java.util.Collections)6 URI (java.net.URI)5 Nullable (org.springframework.lang.Nullable)5 ServerResponse (org.springframework.web.reactive.function.server.ServerResponse)5 WebSocketHandler (org.springframework.web.reactive.socket.WebSocketHandler)5 ExecutionInput (graphql.ExecutionInput)4 ExecutionResultImpl (graphql.ExecutionResultImpl)4 HashMap (java.util.HashMap)4 Stream (java.util.stream.Stream)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 Test (org.junit.jupiter.api.Test)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 MethodSource (org.junit.jupiter.params.provider.MethodSource)4