Minor refactoring and polishing
- Remove (unused) GraphQLResponseBody - Collapse servlet and reactive packages and rename handlers to WebFluxGraphQLHandler and WebMvcGraphQLHandler. - Minor refactoring and polishing in each handler also removing some protected methods that overlap in purpose. - Rename GraphQLRequestBody to RequestInput and make it package private.
This commit is contained in:
@@ -10,7 +10,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplicat
|
||||
import org.springframework.boot.graphql.GraphQLAutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.graphql.reactive.GraphQLHandler;
|
||||
import org.springframework.graphql.WebFluxGraphQLHandler;
|
||||
import org.springframework.web.reactive.function.server.RouterFunction;
|
||||
import org.springframework.web.reactive.function.server.RouterFunctions;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
@@ -24,12 +24,12 @@ public class GraphQLWebFluxAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public GraphQLHandler graphQLHandler(GraphQL.Builder graphQLBuilder) {
|
||||
return new GraphQLHandler(graphQLBuilder);
|
||||
public WebFluxGraphQLHandler graphQLHandler(GraphQL.Builder graphQLBuilder) {
|
||||
return new WebFluxGraphQLHandler(graphQLBuilder);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RouterFunction<ServerResponse> graphQLQueryEndpoint(GraphQLHandler handler) {
|
||||
public RouterFunction<ServerResponse> graphQLQueryEndpoint(WebFluxGraphQLHandler handler) {
|
||||
return RouterFunctions.route().POST("/graphql", handler::handle).build();
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplicat
|
||||
import org.springframework.boot.graphql.GraphQLAutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.graphql.servlet.GraphQLHandler;
|
||||
import org.springframework.graphql.WebMvcGraphQLHandler;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.servlet.function.RouterFunction;
|
||||
import org.springframework.web.servlet.function.RouterFunctions;
|
||||
@@ -27,12 +27,12 @@ public class GraphQLWebAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public GraphQLHandler graphQLHandler(GraphQL.Builder graphQLBuilder) {
|
||||
return new GraphQLHandler(graphQLBuilder);
|
||||
public WebMvcGraphQLHandler graphQLHandler(GraphQL.Builder graphQLBuilder) {
|
||||
return new WebMvcGraphQLHandler(graphQLBuilder);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RouterFunction<ServerResponse> graphQLQueryEndpoint(GraphQLHandler handler) {
|
||||
public RouterFunction<ServerResponse> graphQLQueryEndpoint(WebMvcGraphQLHandler handler) {
|
||||
return RouterFunctions.route()
|
||||
.POST("/graphql", accept(MediaType.APPLICATION_JSON), handler::handle)
|
||||
.build();
|
||||
|
||||
@@ -6,6 +6,7 @@ import graphql.ExecutionResult;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
|
||||
public interface GraphQLInterceptor {
|
||||
|
||||
ExecutionInput preHandle(ExecutionInput input, HttpHeaders headers);
|
||||
|
||||
ExecutionResult postHandle(ExecutionResult result);
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
package org.springframework.graphql;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class GraphQLResponseBody<T> {
|
||||
|
||||
private T data;
|
||||
|
||||
private List<Map<String, Object>> errors = Collections.emptyList();
|
||||
|
||||
public GraphQLResponseBody() {
|
||||
}
|
||||
|
||||
public GraphQLResponseBody(T data) {
|
||||
this.data = data;
|
||||
this.errors = errors;
|
||||
}
|
||||
|
||||
public T getData() {
|
||||
return this.data;
|
||||
}
|
||||
|
||||
public void setData(T data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> getErrors() {
|
||||
return this.errors;
|
||||
}
|
||||
|
||||
public void setErrors(List<Map<String, Object>> errors) {
|
||||
this.errors = errors;
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@ import org.springframework.lang.Nullable;
|
||||
* @author Andreas Marek
|
||||
* @author Brian Clozel
|
||||
*/
|
||||
public class GraphQLRequestBody {
|
||||
class RequestInput {
|
||||
|
||||
private String query;
|
||||
|
||||
@@ -17,13 +17,13 @@ public class GraphQLRequestBody {
|
||||
|
||||
private Map<String, Object> variables = Collections.emptyMap();
|
||||
|
||||
public GraphQLRequestBody(String query, String operationName, Map<String, Object> variables) {
|
||||
public RequestInput(String query, String operationName, Map<String, Object> variables) {
|
||||
this.query = query;
|
||||
this.operationName = operationName;
|
||||
this.variables = variables;
|
||||
}
|
||||
|
||||
public GraphQLRequestBody() {
|
||||
public RequestInput() {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@@ -0,0 +1,50 @@
|
||||
package org.springframework.graphql;
|
||||
|
||||
import graphql.ExecutionInput;
|
||||
import graphql.ExecutionResult;
|
||||
import graphql.GraphQL;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.web.reactive.function.server.ServerRequest;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
|
||||
public class WebFluxGraphQLHandler {
|
||||
|
||||
private final GraphQL graphQL;
|
||||
|
||||
public WebFluxGraphQLHandler(GraphQL.Builder graphQLBuilder) {
|
||||
this.graphQL = graphQLBuilder.build();
|
||||
}
|
||||
|
||||
public Mono<ServerResponse> handle(ServerRequest request) {
|
||||
return request.bodyToMono(RequestInput.class)
|
||||
.flatMap(body -> {
|
||||
String query = body.getQuery();
|
||||
if (query == null) {
|
||||
query = "";
|
||||
}
|
||||
ExecutionInput executionInput = ExecutionInput.newExecutionInput()
|
||||
.query(query)
|
||||
.operationName(body.getOperationName())
|
||||
.variables(body.getVariables())
|
||||
.build();
|
||||
// Invoke GraphQLInterceptor's preHandle here
|
||||
return customizeExecutionInput(executionInput, request.headers().asHttpHeaders());
|
||||
})
|
||||
.flatMap(input -> {
|
||||
// Invoke GraphQLInterceptor's postHandle here
|
||||
return execute(input);
|
||||
})
|
||||
.flatMap(result -> ServerResponse.ok().bodyValue(result.toSpecification()));
|
||||
}
|
||||
|
||||
protected Mono<ExecutionInput> customizeExecutionInput(ExecutionInput input, HttpHeaders headers) {
|
||||
return Mono.just(input);
|
||||
}
|
||||
|
||||
protected Mono<ExecutionResult> execute(ExecutionInput input) {
|
||||
return Mono.fromFuture(graphQL.executeAsync(input));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.springframework.graphql.servlet;
|
||||
package org.springframework.graphql;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
@@ -10,25 +11,24 @@ 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 {
|
||||
public class WebMvcGraphQLHandler {
|
||||
|
||||
private final GraphQL graphQL;
|
||||
|
||||
public GraphQLHandler(GraphQL.Builder graphQL) {
|
||||
public WebMvcGraphQLHandler(GraphQL.Builder graphQL) {
|
||||
this.graphQL = graphQL.build();
|
||||
}
|
||||
|
||||
public ServerResponse handle(ServerRequest serverRequest) {
|
||||
GraphQLRequestBody body;
|
||||
RequestInput body;
|
||||
try {
|
||||
body = serverRequest.body(GraphQLRequestBody.class);
|
||||
body = serverRequest.body(RequestInput.class);
|
||||
}
|
||||
catch (ServletException | IOException ex) {
|
||||
throw new ServerWebInputException("Failed to read request body", null, ex);
|
||||
@@ -42,11 +42,19 @@ public class GraphQLHandler {
|
||||
.operationName(body.getOperationName())
|
||||
.variables(body.getVariables())
|
||||
.build();
|
||||
|
||||
// Invoke GraphQLInterceptor's preHandle here
|
||||
CompletableFuture<ExecutionResult> resultFuture =
|
||||
customizeExecutionInput(input, serverRequest.headers().asHttpHeaders()).thenCompose(this::execute);
|
||||
|
||||
CompletableFuture<Map<String, Object>> future =
|
||||
customizeExecutionInput(input, serverRequest.headers().asHttpHeaders())
|
||||
.thenCompose(this::execute)
|
||||
.thenApply(ExecutionResult::toSpecification);
|
||||
|
||||
// Invoke GraphQLInterceptor's postHandle here
|
||||
return customizeExecutionResult(resultFuture);
|
||||
|
||||
return future.isDone() ?
|
||||
ServerResponse.ok().body(getResult(future)) :
|
||||
ServerResponse.ok().body(future);
|
||||
}
|
||||
|
||||
protected CompletableFuture<ExecutionInput> customizeExecutionInput(ExecutionInput input, HttpHeaders headers) {
|
||||
@@ -57,15 +65,9 @@ public class GraphQLHandler {
|
||||
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) {
|
||||
private Map<String, Object> getResult(CompletableFuture<Map<String, Object>> future) {
|
||||
try {
|
||||
return resultFuture.get();
|
||||
return future.get();
|
||||
}
|
||||
catch (InterruptedException | ExecutionException ex) {
|
||||
throw new ServerErrorException("Failed to get result", ex);
|
||||
@@ -1,52 +0,0 @@
|
||||
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));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
@NonNullApi
|
||||
@NonNullFields
|
||||
package org.springframework.graphql.reactive;
|
||||
|
||||
import org.springframework.lang.NonNullApi;
|
||||
import org.springframework.lang.NonNullFields;
|
||||
@@ -1,6 +0,0 @@
|
||||
@NonNullApi
|
||||
@NonNullFields
|
||||
package org.springframework.graphql.servlet;
|
||||
|
||||
import org.springframework.lang.NonNullApi;
|
||||
import org.springframework.lang.NonNullFields;
|
||||
Reference in New Issue
Block a user