basic spring mvc and spring mvc starter integration which exposes a GraphQL endpoint
This commit is contained in:
committed by
Rossen Stoyanchev
parent
e8db755430
commit
7b549d7932
15
graphql-java-spring-webmvc/build.gradle
Normal file
15
graphql-java-spring-webmvc/build.gradle
Normal file
@@ -0,0 +1,15 @@
|
||||
description = "GraphQL Java Spring Webmvc integration"
|
||||
|
||||
dependencies {
|
||||
compile "org.springframework:spring-webmvc:$springVersion"
|
||||
compile "org.springframework:spring-context:$springVersion"
|
||||
compile "com.fasterxml.jackson.core:jackson-databind:$jacksonVersion"
|
||||
compile "com.graphql-java:graphql-java:$graphqlJavaVersion"
|
||||
|
||||
testCompile("org.assertj:assertj-core:$assertJVersion")
|
||||
testCompile group: 'junit', name: 'junit', version: '4.12'
|
||||
testCompile "org.springframework:spring-test:$springVersion"
|
||||
testCompile group: 'javax.servlet', name: 'javax.servlet-api', version: '4.0.1'
|
||||
testCompile group: 'com.jayway.jsonpath', name: 'json-path', version: '2.4.0'
|
||||
testCompile "org.mockito:mockito-core:2.+"
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package org.springframework.graphql;
|
||||
|
||||
import graphql.ExecutionInput;
|
||||
import graphql.PublicApi;
|
||||
import org.springframework.web.context.request.WebRequest;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
/**
|
||||
* Lets you customize the #ExecutionInput before the query is executed.
|
||||
* You can for example set a context object or define a root value.
|
||||
* <p>
|
||||
* This is only used if you use the default {@link GraphQLInvocation}.
|
||||
*/
|
||||
@PublicApi
|
||||
public interface ExecutionInputCustomizer {
|
||||
|
||||
CompletableFuture<ExecutionInput> customizeExecutionInput(ExecutionInput executionInput, WebRequest webRequest);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package org.springframework.graphql;
|
||||
|
||||
import graphql.ExecutionResult;
|
||||
import graphql.PublicSpi;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@PublicSpi
|
||||
public interface ExecutionResultHandler {
|
||||
|
||||
Object handleExecutionResult(CompletableFuture<ExecutionResult> executionResultCF);
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package org.springframework.graphql;
|
||||
|
||||
import graphql.ExecutionResult;
|
||||
import graphql.PublicApi;
|
||||
import org.springframework.web.context.request.WebRequest;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@PublicApi
|
||||
public interface GraphQLInvocation {
|
||||
|
||||
CompletableFuture<ExecutionResult> invoke(GraphQLInvocationData invocationData, WebRequest webRequest);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package org.springframework.graphql;
|
||||
|
||||
import graphql.Assert;
|
||||
import graphql.PublicApi;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
@PublicApi
|
||||
public class GraphQLInvocationData {
|
||||
|
||||
private final String query;
|
||||
private final String operationName;
|
||||
private final Map<String, Object> variables;
|
||||
|
||||
public GraphQLInvocationData(String query, String operationName, Map<String, Object> variables) {
|
||||
this.query = Assert.assertNotNull(query, () -> "query must be provided");
|
||||
this.operationName = operationName;
|
||||
this.variables = variables != null ? variables : Collections.emptyMap();
|
||||
}
|
||||
|
||||
public String getQuery() {
|
||||
return query;
|
||||
}
|
||||
|
||||
public String getOperationName() {
|
||||
return operationName;
|
||||
}
|
||||
|
||||
public Map<String, Object> getVariables() {
|
||||
return variables;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package org.springframework.graphql.components;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import graphql.ExecutionResult;
|
||||
import graphql.Internal;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.graphql.ExecutionResultHandler;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@Component
|
||||
@Internal
|
||||
public class DefaultExecutionResultHandler implements ExecutionResultHandler {
|
||||
|
||||
@Autowired
|
||||
ObjectMapper objectMapper;
|
||||
|
||||
@Override
|
||||
public Object handleExecutionResult(CompletableFuture<ExecutionResult> executionResultCF) {
|
||||
return executionResultCF.thenApply(ExecutionResult::toSpecification);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package org.springframework.graphql.components;
|
||||
|
||||
import graphql.ExecutionInput;
|
||||
import graphql.ExecutionResult;
|
||||
import graphql.GraphQL;
|
||||
import graphql.Internal;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.graphql.ExecutionInputCustomizer;
|
||||
import org.springframework.graphql.GraphQLInvocation;
|
||||
import org.springframework.graphql.GraphQLInvocationData;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.context.request.WebRequest;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@Component
|
||||
@Internal
|
||||
public class DefaultGraphQLInvocation implements GraphQLInvocation {
|
||||
|
||||
@Autowired
|
||||
GraphQL graphQL;
|
||||
|
||||
@Autowired
|
||||
ExecutionInputCustomizer executionInputCustomizer;
|
||||
|
||||
@Override
|
||||
public CompletableFuture<ExecutionResult> invoke(GraphQLInvocationData invocationData, WebRequest webRequest) {
|
||||
ExecutionInput executionInput = ExecutionInput.newExecutionInput()
|
||||
.query(invocationData.getQuery())
|
||||
.operationName(invocationData.getOperationName())
|
||||
.variables(invocationData.getVariables())
|
||||
.build();
|
||||
CompletableFuture<ExecutionInput> customizedExecutionInput = executionInputCustomizer.customizeExecutionInput(executionInput, webRequest);
|
||||
return customizedExecutionInput.thenCompose(graphQL::executeAsync);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package org.springframework.graphql.components;
|
||||
|
||||
|
||||
import graphql.ExecutionResult;
|
||||
import graphql.Internal;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.graphql.ExecutionResultHandler;
|
||||
import org.springframework.graphql.GraphQLInvocation;
|
||||
import org.springframework.graphql.GraphQLInvocationData;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.context.request.WebRequest;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@RestController
|
||||
@Internal
|
||||
public class GraphQLController {
|
||||
|
||||
@Autowired
|
||||
GraphQLInvocation graphQLInvocation;
|
||||
|
||||
@Autowired
|
||||
ExecutionResultHandler executionResultHandler;
|
||||
|
||||
@RequestMapping(value = "${graphql.url:graphql}",
|
||||
method = RequestMethod.POST,
|
||||
consumes = MediaType.APPLICATION_JSON_VALUE,
|
||||
produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||
public Object graphqlPOST(@RequestBody GraphQLRequestBody body,
|
||||
WebRequest webRequest) {
|
||||
String query = body.getQuery();
|
||||
if (query == null) {
|
||||
query = "";
|
||||
}
|
||||
CompletableFuture<ExecutionResult> executionResult = graphQLInvocation.invoke(new GraphQLInvocationData(query, body.getOperationName(), body.getVariables()), webRequest);
|
||||
return executionResultHandler.handleExecutionResult(executionResult);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package org.springframework.graphql.components;
|
||||
|
||||
import graphql.Internal;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Internal
|
||||
public class GraphQLRequestBody {
|
||||
private String query;
|
||||
private String operationName;
|
||||
private Map<String, Object> variables;
|
||||
|
||||
public String getQuery() {
|
||||
return query;
|
||||
}
|
||||
|
||||
public void setQuery(String query) {
|
||||
this.query = query;
|
||||
}
|
||||
|
||||
public String getOperationName() {
|
||||
return operationName;
|
||||
}
|
||||
|
||||
public void setOperationName(String operationName) {
|
||||
this.operationName = operationName;
|
||||
}
|
||||
|
||||
public Map<String, Object> getVariables() {
|
||||
return variables;
|
||||
}
|
||||
|
||||
public void setVariables(Map<String, Object> variables) {
|
||||
this.variables = variables;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user