Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce common GraphQLHandler which doesn't depend on either webflu…
…x or webmvc specifics. Refactor webflux to use the new Handler
- Loading branch information
Showing
11 changed files
with
187 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
description = "GraphQL Java Spring Common" | ||
|
||
apply plugin: 'java-library' | ||
|
||
dependencies { | ||
implementation "org.springframework:spring-web:$springVersion" | ||
implementation 'io.projectreactor:reactor-core:3.3.8.RELEASE' | ||
api "com.graphql-java:graphql-java:$graphqlJavaVersion" | ||
|
||
testImplementation("org.assertj:assertj-core:$assertJVersion") | ||
testImplementation('org.junit.jupiter:junit-jupiter:5.6.2') | ||
testImplementation "org.springframework:spring-test:$springVersion" | ||
testImplementation group: 'com.jayway.jsonpath', name: 'json-path', version: '2.4.0' | ||
testImplementation "org.mockito:mockito-core:2.+" | ||
} |
24 changes: 24 additions & 0 deletions
24
...g-graphql-common/src/main/java/org/springframework/graphql/DefaultGraphQLInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.springframework.graphql; | ||
|
||
import graphql.ExecutionInput; | ||
import graphql.ExecutionResult; | ||
import org.springframework.http.HttpHeaders; | ||
import reactor.core.publisher.Mono; | ||
|
||
public class DefaultGraphQLInterceptor implements GraphQLInterceptor { | ||
|
||
@Override | ||
public Mono<ExecutionInput> preHandle(ExecutionInput input, HttpHeaders headers) { | ||
return Mono.just(input); | ||
} | ||
|
||
@Override | ||
public Mono<ExecutionResult> postHandle(ExecutionResult result, HttpHeaders httpHeaders) { | ||
return Mono.just(result); | ||
} | ||
|
||
@Override | ||
public Mono<GraphQLResponseBody> customizeResponseBody(GraphQLResponseBody graphQLResponseBody, ExecutionResult executionResult, HttpHeaders httpHeader) { | ||
return Mono.just(graphQLResponseBody); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
spring-graphql-common/src/main/java/org/springframework/graphql/GraphQLHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package org.springframework.graphql; | ||
|
||
import graphql.ExecutionInput; | ||
import graphql.ExecutionResult; | ||
import graphql.GraphQL; | ||
import org.springframework.http.HttpHeaders; | ||
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<GraphQLResponseBody> graphqlPOST(GraphQLRequestBody body, HttpHeaders httpHeaders) { | ||
String query = body.getQuery(); | ||
if (query == null) { | ||
query = ""; | ||
} | ||
ExecutionInput input = ExecutionInput.newExecutionInput() | ||
.query(query) | ||
.operationName(body.getOperationName()) | ||
.variables(body.getVariables()) | ||
.build(); | ||
Mono<ExecutionInput> executionInput = interceptor.preHandle(input, httpHeaders); | ||
return executionInput | ||
.flatMap(this::execute) | ||
.flatMap(result -> interceptor.postHandle(result, httpHeaders)) | ||
.flatMap(result -> toResponseBody(result, httpHeaders)); | ||
} | ||
|
||
private Mono<GraphQLResponseBody> toResponseBody(ExecutionResult executionResult, HttpHeaders httpHeaders) { | ||
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"); | ||
GraphQLResponseBody responseBody = new GraphQLResponseBody(data, | ||
errors, | ||
extensions); | ||
Mono<GraphQLResponseBody> graphQLResponseBodyMono = interceptor.customizeResponseBody(responseBody, executionResult, httpHeaders); | ||
return graphQLResponseBodyMono; | ||
} | ||
|
||
|
||
protected Mono<ExecutionResult> execute(ExecutionInput input) { | ||
return Mono.fromCompletionStage(graphQL.executeAsync(input)); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
spring-graphql-common/src/main/java/org/springframework/graphql/GraphQLInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.springframework.graphql; | ||
|
||
import graphql.ExecutionInput; | ||
import graphql.ExecutionResult; | ||
import org.springframework.http.HttpHeaders; | ||
import reactor.core.publisher.Mono; | ||
|
||
public interface GraphQLInterceptor { | ||
|
||
Mono<ExecutionInput> preHandle(ExecutionInput input, HttpHeaders headers); | ||
|
||
Mono<ExecutionResult> postHandle(ExecutionResult result, HttpHeaders httpHeaders); | ||
|
||
Mono<GraphQLResponseBody> customizeResponseBody(GraphQLResponseBody graphQLResponseBody, ExecutionResult executionResult, HttpHeaders httpHeader); | ||
|
||
} |
14 changes: 5 additions & 9 deletions
14
...aphql/reactive/GraphQLInvocationData.java → ...framework/graphql/GraphQLRequestBody.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
spring-graphql-common/src/main/java/org/springframework/graphql/GraphQLResponseBody.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.springframework.graphql; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class GraphQLResponseBody { | ||
|
||
private final Object data; | ||
private final List<Map<String, Object>> errors; | ||
private final Map<String, Object> extensions; | ||
|
||
public GraphQLResponseBody(Object data, List<Map<String, Object>> errors, Map<String, Object> extensions) { | ||
this.data = data; | ||
this.errors = errors; | ||
this.extensions = extensions; | ||
} | ||
|
||
public Object getData() { | ||
return data; | ||
} | ||
|
||
public List<Map<String, Object>> getErrors() { | ||
return errors; | ||
} | ||
|
||
public Map<String, Object> getExtensions() { | ||
return extensions; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 0 additions & 44 deletions
44
.../src/main/java/org/springframework/graphql/reactive/components/GraphQLRequestHandler.java
This file was deleted.
Oops, something went wrong.