Create web stack-specific GraphQL handlers
Instead of trying to create a single handler for both reactive and non-reactive implementations, this commit splits the implementations.
This commit is contained in:
@@ -1,57 +0,0 @@
|
||||
package org.springframework.graphql;
|
||||
|
||||
import graphql.ExecutionInput;
|
||||
import graphql.ExecutionResult;
|
||||
import graphql.GraphQL;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class GraphQLHandler {
|
||||
private GraphQL graphQL;
|
||||
|
||||
private GraphQLInterceptor interceptor;
|
||||
|
||||
public GraphQLHandler(GraphQL graphQL, GraphQLInterceptor interceptor) {
|
||||
this.graphQL = graphQL;
|
||||
this.interceptor = interceptor;
|
||||
}
|
||||
|
||||
public Mono<GraphQLHttpResponse> graphqlPOST(GraphQLHttpRequest graphQLHttpRequest) {
|
||||
String query = graphQLHttpRequest.getQuery();
|
||||
ExecutionInput input = ExecutionInput.newExecutionInput()
|
||||
.query(query)
|
||||
.operationName(graphQLHttpRequest.getOperationName())
|
||||
.variables(graphQLHttpRequest.getVariables())
|
||||
.build();
|
||||
MultiValueMap<String, String> requestParams = graphQLHttpRequest.getRequestParams();
|
||||
Mono<ExecutionInput> executionInput = interceptor.preHandle(input,
|
||||
graphQLHttpRequest.getHttpHeaders(),
|
||||
requestParams);
|
||||
return executionInput
|
||||
.flatMap(this::execute)
|
||||
.flatMap(result -> interceptor.postHandle(result, graphQLHttpRequest.getHttpHeaders(), requestParams))
|
||||
.flatMap(result -> toResponseBody(result, graphQLHttpRequest));
|
||||
}
|
||||
|
||||
private Mono<GraphQLHttpResponse> toResponseBody(ExecutionResult executionResult, GraphQLHttpRequest graphQLHttpRequest) {
|
||||
Map<String, Object> responseBodyRaw = executionResult.toSpecification();
|
||||
Object data = responseBodyRaw.get("data");
|
||||
List<Map<String, Object>> errors = (List<Map<String, Object>>) responseBodyRaw.get("errors");
|
||||
Map<String, Object> extensions = (Map<String, Object>) responseBodyRaw.get("extensions");
|
||||
GraphQLHttpResponse responseBody = new GraphQLHttpResponse(data,
|
||||
errors,
|
||||
extensions,
|
||||
new HttpHeaders());
|
||||
Mono<GraphQLHttpResponse> graphQLResponseBodyMono = interceptor.customizeGraphQLHttpResponse(responseBody, executionResult, graphQLHttpRequest);
|
||||
return graphQLResponseBodyMono;
|
||||
}
|
||||
|
||||
|
||||
protected Mono<ExecutionResult> execute(ExecutionInput input) {
|
||||
return Mono.fromCompletionStage(graphQL.executeAsync(input));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package org.springframework.graphql.reactive;
|
||||
|
||||
import graphql.ExecutionInput;
|
||||
import graphql.ExecutionResult;
|
||||
import graphql.GraphQL;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.graphql.GraphQLRequestBody;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.web.reactive.function.server.ServerRequest;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
|
||||
public class GraphQLHandler {
|
||||
|
||||
private final GraphQL graphQL;
|
||||
|
||||
public GraphQLHandler(GraphQL.Builder graphQLBuilder) {
|
||||
this.graphQL = graphQLBuilder.build();
|
||||
}
|
||||
|
||||
public Mono<ServerResponse> handle(ServerRequest request) {
|
||||
Mono<GraphQLRequestBody> bodyMono = request.bodyToMono(GraphQLRequestBody.class);
|
||||
return bodyMono.map(body -> {
|
||||
String query = body.getQuery();
|
||||
if (query == null) {
|
||||
query = "";
|
||||
}
|
||||
ExecutionInput executionInput = ExecutionInput.newExecutionInput()
|
||||
.query(query)
|
||||
.operationName(body.getOperationName())
|
||||
.variables(body.getVariables())
|
||||
.build();
|
||||
return customizeExecutionInput(executionInput, request.headers().asHttpHeaders())
|
||||
.then(execute(executionInput));
|
||||
})
|
||||
.flatMap(this::toServerResponse);
|
||||
}
|
||||
|
||||
protected Mono<ExecutionInput> customizeExecutionInput(ExecutionInput input, HttpHeaders headers) {
|
||||
return Mono.just(input);
|
||||
}
|
||||
|
||||
protected Mono<ExecutionResult> execute(ExecutionInput input) {
|
||||
return Mono.fromFuture((graphQL.executeAsync(input)));
|
||||
}
|
||||
|
||||
protected Mono<ServerResponse> toServerResponse(Mono<ExecutionResult> result) {
|
||||
return result.map(ExecutionResult::toSpecification)
|
||||
.flatMap(spec -> ServerResponse.ok().bodyValue(spec));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
@NonNullApi
|
||||
@NonNullFields
|
||||
package org.springframework.graphql.reactive;
|
||||
|
||||
import org.springframework.lang.NonNullApi;
|
||||
import org.springframework.lang.NonNullFields;
|
||||
@@ -0,0 +1,74 @@
|
||||
package org.springframework.graphql.servlet;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
|
||||
import graphql.ExecutionInput;
|
||||
import graphql.ExecutionResult;
|
||||
import graphql.GraphQL;
|
||||
|
||||
import org.springframework.graphql.GraphQLRequestBody;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.web.server.ServerErrorException;
|
||||
import org.springframework.web.server.ServerWebInputException;
|
||||
import org.springframework.web.servlet.function.ServerRequest;
|
||||
import org.springframework.web.servlet.function.ServerResponse;
|
||||
|
||||
public class GraphQLHandler {
|
||||
|
||||
private final GraphQL graphQL;
|
||||
|
||||
public GraphQLHandler(GraphQL.Builder graphQL) {
|
||||
this.graphQL = graphQL.build();
|
||||
}
|
||||
|
||||
public ServerResponse handle(ServerRequest serverRequest) {
|
||||
GraphQLRequestBody body;
|
||||
try {
|
||||
body = serverRequest.body(GraphQLRequestBody.class);
|
||||
}
|
||||
catch (ServletException | IOException ex) {
|
||||
throw new ServerWebInputException("Failed to read request body", null, ex);
|
||||
}
|
||||
String query = body.getQuery();
|
||||
if (query == null) {
|
||||
query = "";
|
||||
}
|
||||
ExecutionInput input = ExecutionInput.newExecutionInput()
|
||||
.query(query)
|
||||
.operationName(body.getOperationName())
|
||||
.variables(body.getVariables())
|
||||
.build();
|
||||
// Invoke GraphQLInterceptor's preHandle here
|
||||
CompletableFuture<ExecutionResult> resultFuture =
|
||||
customizeExecutionInput(input, serverRequest.headers().asHttpHeaders()).thenCompose(this::execute);
|
||||
// Invoke GraphQLInterceptor's postHandle here
|
||||
return customizeExecutionResult(resultFuture);
|
||||
}
|
||||
|
||||
protected CompletableFuture<ExecutionInput> customizeExecutionInput(ExecutionInput input, HttpHeaders headers) {
|
||||
return CompletableFuture.completedFuture(input);
|
||||
}
|
||||
|
||||
protected CompletableFuture<ExecutionResult> execute(ExecutionInput input) {
|
||||
return graphQL.executeAsync(input);
|
||||
}
|
||||
|
||||
protected ServerResponse customizeExecutionResult(CompletableFuture<ExecutionResult> resultFuture) {
|
||||
return resultFuture.isDone() ?
|
||||
ServerResponse.ok().body(getResult(resultFuture)) :
|
||||
ServerResponse.ok().body(resultFuture);
|
||||
}
|
||||
|
||||
private ExecutionResult getResult(CompletableFuture<ExecutionResult> resultFuture) {
|
||||
try {
|
||||
return resultFuture.get();
|
||||
}
|
||||
catch (InterruptedException | ExecutionException ex) {
|
||||
throw new ServerErrorException("Failed to get result", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
@NonNullApi
|
||||
@NonNullFields
|
||||
package org.springframework.graphql.servlet;
|
||||
|
||||
import org.springframework.lang.NonNullApi;
|
||||
import org.springframework.lang.NonNullFields;
|
||||
Reference in New Issue
Block a user