From c68cb12dfd318721a98cc4068cbab7fc1fa6be0c Mon Sep 17 00:00:00 2001 From: Arnaud Nauwynck Date: Sun, 30 May 2021 21:18:51 +0200 Subject: [PATCH] added GraphQLRootProvider --- .../servlet/GraphQLLocalContextProvider.java | 13 ++++++++ .../web/servlet/GraphQLRootProvider.java | 13 ++++++++ .../components/DefaultGraphQLInvocation.java | 33 ++++++++++++++----- 3 files changed, 51 insertions(+), 8 deletions(-) create mode 100755 graphql-java-spring-webmvc/src/main/java/graphql/spring/web/servlet/GraphQLLocalContextProvider.java create mode 100755 graphql-java-spring-webmvc/src/main/java/graphql/spring/web/servlet/GraphQLRootProvider.java diff --git a/graphql-java-spring-webmvc/src/main/java/graphql/spring/web/servlet/GraphQLLocalContextProvider.java b/graphql-java-spring-webmvc/src/main/java/graphql/spring/web/servlet/GraphQLLocalContextProvider.java new file mode 100755 index 0000000..7bfb3fb --- /dev/null +++ b/graphql-java-spring-webmvc/src/main/java/graphql/spring/web/servlet/GraphQLLocalContextProvider.java @@ -0,0 +1,13 @@ +package graphql.spring.web.servlet; + +import java.util.function.Supplier; + +import graphql.PublicSpi; + +@PublicSpi +public interface GraphQLLocalContextProvider extends Supplier { + + @Override + public Object get(); + +} diff --git a/graphql-java-spring-webmvc/src/main/java/graphql/spring/web/servlet/GraphQLRootProvider.java b/graphql-java-spring-webmvc/src/main/java/graphql/spring/web/servlet/GraphQLRootProvider.java new file mode 100755 index 0000000..5255683 --- /dev/null +++ b/graphql-java-spring-webmvc/src/main/java/graphql/spring/web/servlet/GraphQLRootProvider.java @@ -0,0 +1,13 @@ +package graphql.spring.web.servlet; + +import java.util.function.Supplier; + +import graphql.PublicSpi; + +@PublicSpi +public interface GraphQLRootProvider extends Supplier { + + @Override + public Object get(); + +} diff --git a/graphql-java-spring-webmvc/src/main/java/graphql/spring/web/servlet/components/DefaultGraphQLInvocation.java b/graphql-java-spring-webmvc/src/main/java/graphql/spring/web/servlet/components/DefaultGraphQLInvocation.java index d96cff9..9371fe5 100644 --- a/graphql-java-spring-webmvc/src/main/java/graphql/spring/web/servlet/components/DefaultGraphQLInvocation.java +++ b/graphql-java-spring-webmvc/src/main/java/graphql/spring/web/servlet/components/DefaultGraphQLInvocation.java @@ -1,5 +1,12 @@ package graphql.spring.web.servlet.components; +import java.util.concurrent.CompletableFuture; + +import org.dataloader.DataLoaderRegistry; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.web.context.request.WebRequest; + import graphql.ExecutionInput; import graphql.ExecutionResult; import graphql.GraphQL; @@ -7,12 +14,8 @@ import graphql.spring.web.servlet.ExecutionInputCustomizer; import graphql.spring.web.servlet.GraphQLInvocation; import graphql.spring.web.servlet.GraphQLInvocationData; -import org.dataloader.DataLoaderRegistry; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; -import org.springframework.web.context.request.WebRequest; - -import java.util.concurrent.CompletableFuture; +import graphql.spring.web.servlet.GraphQLLocalContextProvider; +import graphql.spring.web.servlet.GraphQLRootProvider; @Component @Internal @@ -21,6 +24,12 @@ public class DefaultGraphQLInvocation implements GraphQLInvocation { @Autowired GraphQL graphQL; + @Autowired(required = false) + GraphQLRootProvider rootProvider; + + @Autowired(required = false) + GraphQLLocalContextProvider localContextProvider; + @Autowired(required = false) DataLoaderRegistry dataLoaderRegistry; @@ -29,11 +38,19 @@ public class DefaultGraphQLInvocation implements GraphQLInvocation { @Override public CompletableFuture invoke(GraphQLInvocationData invocationData, WebRequest webRequest) { - ExecutionInput.Builder executionInputBuilder = ExecutionInput.newExecutionInput() + ExecutionInput.Builder executionInputBuilder = ExecutionInput.newExecutionInput() .query(invocationData.getQuery()) .operationName(invocationData.getOperationName()) .variables(invocationData.getVariables()); - if (dataLoaderRegistry != null) { + if (rootProvider != null) { + Object root = rootProvider.get(); + executionInputBuilder.root(root); + } + if (localContextProvider != null) { + Object localContext = localContextProvider.get(); + executionInputBuilder.localContext(localContext); + } + if (dataLoaderRegistry != null) { executionInputBuilder.dataLoaderRegistry(dataLoaderRegistry); } ExecutionInput executionInput = executionInputBuilder.build();