Skip to content

Commit b471852

Browse files
committed
You can now change the context during ExecutionInput build but the values are copied into the known instance
1 parent f8fdb49 commit b471852

File tree

3 files changed

+65
-8
lines changed

3 files changed

+65
-8
lines changed

src/main/java/graphql/ExecutionInput.java

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public ExecutionInput transform(Consumer<Builder> builderConsumer) {
153153
.query(this.query)
154154
.operationName(this.operationName)
155155
.context(this.context)
156-
.graphQLContext(this.graphQLContext)
156+
.transfer(this.graphQLContext)
157157
.localContext(this.localContext)
158158
.root(this.root)
159159
.dataLoaderRegistry(this.dataLoaderRegistry)
@@ -282,12 +282,6 @@ public Builder context(Object context) {
282282
return this;
283283
}
284284

285-
// hidden on purpose
286-
private Builder graphQLContext(GraphQLContext graphQLContext) {
287-
this.graphQLContext = graphQLContext;
288-
return this;
289-
}
290-
291285
/**
292286
* The legacy context object
293287
*
@@ -319,6 +313,39 @@ public Builder context(UnaryOperator<GraphQLContext.Builder> contextBuilderFunct
319313
return context(builder.build());
320314
}
321315

316+
/**
317+
* This will give you a builder of {@link GraphQLContext} and any values you set will be copied
318+
* into the underlying {@link GraphQLContext} of this execution input
319+
*
320+
* @param builderFunction a builder function you can use to put values into the context
321+
*
322+
* @return this builder
323+
*/
324+
public Builder graphQLContext(Consumer<GraphQLContext.Builder> builderFunction) {
325+
GraphQLContext.Builder builder = GraphQLContext.newContext();
326+
builderFunction.accept(builder);
327+
this.graphQLContext.putAll(builder);
328+
return this;
329+
}
330+
331+
/**
332+
* This will put all the valuyes from the map into the underlying {@link GraphQLContext} of this execution input
333+
*
334+
* @param mapOfContext a map of values to put in the context
335+
*
336+
* @return this builder
337+
*/
338+
public Builder graphQLContext(Map<?, Object> mapOfContext) {
339+
this.graphQLContext.putAll(mapOfContext);
340+
return this;
341+
}
342+
343+
// hidden on purpose
344+
private Builder transfer(GraphQLContext graphQLContext) {
345+
this.graphQLContext = Assert.assertNotNull(graphQLContext);
346+
return this;
347+
}
348+
322349
public Builder root(Object root) {
323350
this.root = root;
324351
return this;

src/main/java/graphql/GraphQLContext.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,14 @@ public static Builder newContext() {
236236
public static class Builder {
237237
private final ConcurrentMap<Object, Object> map = new ConcurrentHashMap<>();
238238

239+
public Builder put(
240+
Object key1, Object value1
241+
) {
242+
return putImpl(
243+
key1, value1
244+
);
245+
}
246+
239247
public Builder of(
240248
Object key1, Object value1
241249
) {
@@ -311,6 +319,17 @@ public Builder of(Map<?, Object> mapOfContext) {
311319
return this;
312320
}
313321

322+
/**
323+
* Adds all of the values in the map into the context builder. All keys and values MUST be non null
324+
*
325+
* @param mapOfContext the map to put into context
326+
*
327+
* @return this builder
328+
*/
329+
public Builder putAll(Map<?, Object> mapOfContext) {
330+
return of(mapOfContext);
331+
}
332+
314333
/**
315334
* Adds all of the values in the map into the context builder. All keys and values MUST be non null
316335
*

src/test/groovy/graphql/ExecutionInputTest.groovy

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ class ExecutionInputTest extends Specification {
2626
.variables(variables)
2727
.root(root)
2828
.context(context)
29+
.graphQLContext({ it.of(["a": "b"]) })
2930
.locale(Locale.GERMAN)
3031
.extensions([some: "map"])
3132
.build()
3233
then:
3334
executionInput.context == context
34-
executionInput.graphQLContext != null
35+
executionInput.graphQLContext.get("a") == "b"
3536
executionInput.root == root
3637
executionInput.variables == variables
3738
executionInput.dataLoaderRegistry == registry
@@ -41,6 +42,15 @@ class ExecutionInputTest extends Specification {
4142
executionInput.extensions == [some: "map"]
4243
}
4344

45+
def "map context build works"() {
46+
when:
47+
def executionInput = ExecutionInput.newExecutionInput().query(query)
48+
.graphQLContext([a: "b"])
49+
.build()
50+
then:
51+
executionInput.graphQLContext.get("a") == "b"
52+
}
53+
4454
def "legacy context methods work"() {
4555
when:
4656
def executionInput = ExecutionInput.newExecutionInput().query(query)
@@ -83,6 +93,7 @@ class ExecutionInputTest extends Specification {
8393
.extensions([some: "map"])
8494
.root(root)
8595
.context(context)
96+
.graphQLContext({ it.of(["a": "b"]) })
8697
.locale(Locale.GERMAN)
8798
.build()
8899
def graphQLContext = executionInputOld.getGraphQLContext()

0 commit comments

Comments
 (0)