Skip to content
This repository was archived by the owner on Oct 25, 2021. It is now read-only.

Added support for setting context object in execution input #3

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |



Expand Down
Original file line number Diff line number Diff line change
@@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the risk of making "InvocationData" API, would we not want that here since its parsed and ready to go

 Object build(GraphQLInvocationData invocationData, ServerWebExchange webRequest);

or perhaps it should be

Object build(ExecutionInput startingExecutionInput, ServerWebExchange webRequest);

OR perhaps we don't have this but rather a ExecutionInputCustomizer where they can return the starting ExecutionInput or they can ei.transform() it themselves.

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,9 +19,14 @@ public class DefaultGraphQLInvocation implements GraphQLInvocation {
@Autowired
private GraphQL graphQL;

@Autowired(required = false)
private GraphQLContextBuilder contextBuilder;

@Override
public Mono<ExecutionResult> 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())
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,9 +20,14 @@ public class DefaultGraphQLInvocation implements GraphQLInvocation {
@Autowired
private GraphQL graphQL;

@Autowired(required = false)
private GraphQLContextBuilder contextBuilder;

@Override
public CompletableFuture<ExecutionResult> 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())
Expand Down
12 changes: 12 additions & 0 deletions graphql-java-spring.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id="graphql-java-spring" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" external.system.module.group="" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
<excludeFolder url="file://$MODULE_DIR$/build" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>