diff --git a/README.md b/README.md index e442b23..c79e167 100644 --- a/README.md +++ b/README.md @@ -111,10 +111,11 @@ The following properties are currently available: The following Beans can be overridden by providing a different implementation. -| Interface | Description | -| --- | --- | -| GraphQLInvocation | Executes one request. The default impl just calls the provided `GraphQL` bean.| -| ExecutionResultHandler | Takes a `ExecutionResult` and sends the result back to the client. The default impl returns `ExecutionResult.toSpecification()` as json. | +| Interface | Description | Default Implementation | +| --- | --- | --- | +| `GraphQLInvocation` | Executes one request. The default impl just calls the provided `GraphQL` bean. | `DefaultGraphQLInvocation` | +| `GraphQLContextBuilder` | Builds the context `Object` passed to the `ExecutionInput`. Used by the `DefaultGraphQLInvocation`. | None | +| `ExecutionResultHandler` | Takes a `ExecutionResult` and sends the result back to the client. The default impl returns `ExecutionResult.toSpecification()` as json. | `DefaultExecutionResultHandler` | diff --git a/graphql-java-spring-webflux/src/main/java/graphql/spring/web/reactive/GraphQLContextBuilder.java b/graphql-java-spring-webflux/src/main/java/graphql/spring/web/reactive/GraphQLContextBuilder.java new file mode 100644 index 0000000..3b345be --- /dev/null +++ b/graphql-java-spring-webflux/src/main/java/graphql/spring/web/reactive/GraphQLContextBuilder.java @@ -0,0 +1,10 @@ +package graphql.spring.web.reactive; + +import graphql.PublicApi; +import org.springframework.web.server.ServerWebExchange; + +@PublicApi +public interface GraphQLContextBuilder { + + Object build(ServerWebExchange webRequest); +} diff --git a/graphql-java-spring-webflux/src/main/java/graphql/spring/web/reactive/components/DefaultGraphQLInvocation.java b/graphql-java-spring-webflux/src/main/java/graphql/spring/web/reactive/components/DefaultGraphQLInvocation.java index 5d0c5a0..6b8c735 100644 --- a/graphql-java-spring-webflux/src/main/java/graphql/spring/web/reactive/components/DefaultGraphQLInvocation.java +++ b/graphql-java-spring-webflux/src/main/java/graphql/spring/web/reactive/components/DefaultGraphQLInvocation.java @@ -4,6 +4,7 @@ import graphql.ExecutionResult; import graphql.GraphQL; import graphql.Internal; +import graphql.spring.web.reactive.GraphQLContextBuilder; import graphql.spring.web.reactive.GraphQLInvocation; import graphql.spring.web.reactive.GraphQLInvocationData; import org.springframework.beans.factory.annotation.Autowired; @@ -18,9 +19,14 @@ public class DefaultGraphQLInvocation implements GraphQLInvocation { @Autowired private GraphQL graphQL; + @Autowired(required = false) + private GraphQLContextBuilder contextBuilder; + @Override public Mono invoke(GraphQLInvocationData invocationData, ServerWebExchange serverWebExchange) { + Object context = contextBuilder != null ? contextBuilder.build(serverWebExchange) : null; ExecutionInput executionInput = ExecutionInput.newExecutionInput() + .context(context) .query(invocationData.getQuery()) .operationName(invocationData.getOperationName()) .variables(invocationData.getVariables()) diff --git a/graphql-java-spring-webmvc/src/main/java/graphql/spring/web/servlet/GraphQLContextBuilder.java b/graphql-java-spring-webmvc/src/main/java/graphql/spring/web/servlet/GraphQLContextBuilder.java new file mode 100644 index 0000000..483c34c --- /dev/null +++ b/graphql-java-spring-webmvc/src/main/java/graphql/spring/web/servlet/GraphQLContextBuilder.java @@ -0,0 +1,10 @@ +package graphql.spring.web.servlet; + +import graphql.PublicApi; +import org.springframework.web.context.request.WebRequest; + +@PublicApi +public interface GraphQLContextBuilder { + + Object build(WebRequest webRequest); +} 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 4a3ef15..fffea67 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 @@ -4,6 +4,7 @@ import graphql.ExecutionResult; import graphql.GraphQL; import graphql.Internal; +import graphql.spring.web.servlet.GraphQLContextBuilder; import graphql.spring.web.servlet.GraphQLInvocation; import graphql.spring.web.servlet.GraphQLInvocationData; import org.springframework.beans.factory.annotation.Autowired; @@ -19,9 +20,14 @@ public class DefaultGraphQLInvocation implements GraphQLInvocation { @Autowired private GraphQL graphQL; + @Autowired(required = false) + private GraphQLContextBuilder contextBuilder; + @Override public CompletableFuture invoke(GraphQLInvocationData invocationData, WebRequest webRequest) { + Object context = contextBuilder != null ? contextBuilder.build(webRequest) : null; ExecutionInput executionInput = ExecutionInput.newExecutionInput() + .context(context) .query(invocationData.getQuery()) .operationName(invocationData.getOperationName()) .variables(invocationData.getVariables())