Skip to content

Commit fe13e10

Browse files
committed
made a supplier of normalised variables
1 parent 6563ef9 commit fe13e10

10 files changed

Lines changed: 57 additions & 36 deletions

File tree

src/main/java/graphql/execution/Execution.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@
2828
import graphql.schema.GraphQLObjectType;
2929
import graphql.schema.GraphQLSchema;
3030
import graphql.schema.impl.SchemaUtil;
31+
import graphql.util.FpKit;
3132
import org.reactivestreams.Publisher;
3233

3334
import java.util.Collections;
3435
import java.util.List;
3536
import java.util.Map;
3637
import java.util.Optional;
3738
import java.util.concurrent.CompletableFuture;
39+
import java.util.function.Supplier;
3840

3941
import static graphql.execution.ExecutionContextBuilder.newExecutionContextBuilder;
4042
import static graphql.execution.ExecutionStepInfo.newExecutionStepInfo;
@@ -77,14 +79,20 @@ public CompletableFuture<ExecutionResult> execute(Document document, GraphQLSche
7779
List<VariableDefinition> variableDefinitions = operationDefinition.getVariableDefinitions();
7880

7981
CoercedVariables coercedVariables;
80-
NormalizedVariables normalizedVariableValues;
82+
Supplier<NormalizedVariables> normalizedVariableValues;
8183
try {
82-
coercedVariables = ValuesResolver.coerceVariableValues(graphQLSchema, variableDefinitions, inputVariables, executionInput.getGraphQLContext(), executionInput.getLocale());
83-
84-
normalizedVariableValues = ValuesResolver.getNormalizedVariableValues(graphQLSchema,
84+
coercedVariables = ValuesResolver.coerceVariableValues(graphQLSchema,
8585
variableDefinitions,
8686
inputVariables,
87-
executionInput.getGraphQLContext(), executionInput.getLocale());
87+
executionInput.getGraphQLContext(),
88+
executionInput.getLocale());
89+
90+
normalizedVariableValues = FpKit.intraThreadMemoize(() ->
91+
ValuesResolver.getNormalizedVariableValues(graphQLSchema,
92+
variableDefinitions,
93+
inputVariables,
94+
executionInput.getGraphQLContext(), executionInput.getLocale())
95+
);
8896
} catch (RuntimeException rte) {
8997
if (rte instanceof GraphQLError) {
9098
return completedFuture(new ExecutionResultImpl((GraphQLError) rte));

src/main/java/graphql/execution/ExecutionContext.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class ExecutionContext {
4545
private final OperationDefinition operationDefinition;
4646
private final Document document;
4747
private final CoercedVariables coercedVariables;
48-
private final NormalizedVariables normalizedVariables;
48+
private final Supplier<NormalizedVariables> normalizedVariables;
4949
private final Object root;
5050
private final Object context;
5151
private final GraphQLContext graphQLContext;
@@ -129,7 +129,10 @@ public CoercedVariables getCoercedVariables() {
129129
return coercedVariables;
130130
}
131131

132-
public NormalizedVariables getNormalizedVariables() {
132+
/**
133+
* @return a supplier that will give out the operations variables in normalized form
134+
*/
135+
public Supplier<NormalizedVariables> getNormalizedVariables() {
133136
return normalizedVariables;
134137
}
135138

src/main/java/graphql/execution/ExecutionContextBuilder.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.Locale;
2020
import java.util.Map;
21+
import java.util.function.Supplier;
2122

2223
import static graphql.Assert.assertNotNull;
2324
import static graphql.collect.ImmutableKit.emptyList;
@@ -38,7 +39,7 @@ public class ExecutionContextBuilder {
3839
Document document;
3940
OperationDefinition operationDefinition;
4041
CoercedVariables coercedVariables = CoercedVariables.emptyVariables();
41-
NormalizedVariables normalizedVariables = NormalizedVariables.emptyVariables();
42+
Supplier<NormalizedVariables> normalizedVariables = NormalizedVariables::emptyVariables;
4243
ImmutableMap<String, FragmentDefinition> fragmentsByName = ImmutableKit.emptyMap();
4344
DataLoaderRegistry dataLoaderRegistry;
4445
Locale locale;
@@ -171,7 +172,7 @@ public ExecutionContextBuilder coercedVariables(CoercedVariables coercedVariable
171172
return this;
172173
}
173174

174-
public ExecutionContextBuilder normalizedVariableValues(NormalizedVariables normalizedVariables) {
175+
public ExecutionContextBuilder normalizedVariableValues(Supplier<NormalizedVariables> normalizedVariables) {
175176
this.normalizedVariables = normalizedVariables;
176177
return this;
177178
}

src/main/java/graphql/execution/ExecutionStrategy.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,8 +463,8 @@ Async.CombinedBuilder<FieldValueInfo> getAsyncFieldValueInfo(
463463
DataFetchingFieldSelectionSet fieldCollector = DataFetchingFieldSelectionSetImpl.newCollector(executionContext.getGraphQLSchema(), fieldDef.getType(), normalizedFieldSupplier);
464464
QueryDirectives queryDirectives = new QueryDirectivesImpl(field,
465465
executionContext.getGraphQLSchema(),
466-
executionContext.getCoercedVariables().toMap(),
467-
executionContext.getNormalizedVariables().toMap(),
466+
executionContext.getCoercedVariables(),
467+
executionContext.getNormalizedVariables(),
468468
executionContext.getGraphQLContext(),
469469
executionContext.getLocale());
470470

src/main/java/graphql/execution/directives/DirectivesResolver.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class DirectivesResolver {
2626
public DirectivesResolver() {
2727
}
2828

29-
public BiMap<GraphQLDirective, Directive> resolveDirectives(List<Directive> directives, GraphQLSchema schema, Map<String, Object> variables, GraphQLContext graphQLContext, Locale locale) {
29+
public BiMap<GraphQLDirective, Directive> resolveDirectives(List<Directive> directives, GraphQLSchema schema, CoercedVariables variables, GraphQLContext graphQLContext, Locale locale) {
3030
GraphQLCodeRegistry codeRegistry = schema.getCodeRegistry();
3131
BiMap<GraphQLDirective, Directive> directiveMap = HashBiMap.create();
3232
directives.forEach(directive -> {
@@ -43,10 +43,10 @@ private void buildArguments(GraphQLDirective.Builder directiveBuilder,
4343
GraphQLCodeRegistry codeRegistry,
4444
GraphQLDirective protoType,
4545
Directive fieldDirective,
46-
Map<String, Object> variables,
46+
CoercedVariables variables,
4747
GraphQLContext graphQLContext,
4848
Locale locale) {
49-
Map<String, Object> argumentValues = ValuesResolver.getArgumentValues(codeRegistry, protoType.getArguments(), fieldDirective.getArguments(), CoercedVariables.of(variables), graphQLContext, locale);
49+
Map<String, Object> argumentValues = ValuesResolver.getArgumentValues(codeRegistry, protoType.getArguments(), fieldDirective.getArguments(), variables, graphQLContext, locale);
5050
directiveBuilder.clearArguments();
5151
protoType.getArguments().forEach(protoArg -> {
5252
if (argumentValues.containsKey(protoArg.getName())) {

src/main/java/graphql/execution/directives/QueryDirectives.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.List;
1414
import java.util.Locale;
1515
import java.util.Map;
16+
import java.util.function.Supplier;
1617

1718
/**
1819
* This gives you access to the immediate directives on a {@link graphql.execution.MergedField}. This does not include directives on parent
@@ -120,7 +121,7 @@ interface Builder {
120121

121122
Builder coercedVariables(CoercedVariables coercedVariables);
122123

123-
Builder normalizedVariables(NormalizedVariables normalizedVariables);
124+
Builder normalizedVariables(Supplier<NormalizedVariables> normalizedVariables);
124125

125126
Builder graphQLContext(GraphQLContext graphQLContext);
126127

src/main/java/graphql/execution/directives/QueryDirectivesBuilder.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99
import graphql.schema.GraphQLSchema;
1010

1111
import java.util.Locale;
12+
import java.util.function.Supplier;
1213

1314
@Internal
1415
public class QueryDirectivesBuilder implements QueryDirectives.Builder {
1516

1617
private MergedField mergedField;
1718
private GraphQLSchema schema;
1819
private CoercedVariables coercedVariables = CoercedVariables.emptyVariables();
19-
private NormalizedVariables normalizedVariables = NormalizedVariables.emptyVariables();
20+
private Supplier<NormalizedVariables> normalizedVariables = NormalizedVariables::emptyVariables;
2021
private GraphQLContext graphQLContext = GraphQLContext.getDefault();
2122
private Locale locale = Locale.getDefault();
2223

@@ -45,7 +46,7 @@ public QueryDirectives.Builder coercedVariables(CoercedVariables coercedVariable
4546
}
4647

4748
@Override
48-
public QueryDirectives.Builder normalizedVariables(NormalizedVariables normalizedVariables) {
49+
public QueryDirectives.Builder normalizedVariables(Supplier<NormalizedVariables> normalizedVariables) {
4950
this.normalizedVariables = normalizedVariables;
5051
return this;
5152
}
@@ -65,6 +66,6 @@ public QueryDirectives.Builder locale(Locale locale) {
6566

6667
@Override
6768
public QueryDirectives build() {
68-
return new QueryDirectivesImpl(mergedField, schema, coercedVariables.toMap(), normalizedVariables.toMap(), graphQLContext, locale);
69+
return new QueryDirectivesImpl(mergedField, schema, coercedVariables, normalizedVariables, graphQLContext, locale);
6970
}
7071
}

src/main/java/graphql/execution/directives/QueryDirectivesImpl.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@
44
import com.google.common.collect.HashBiMap;
55
import com.google.common.collect.ImmutableList;
66
import com.google.common.collect.ImmutableMap;
7+
import graphql.Assert;
78
import graphql.GraphQLContext;
89
import graphql.Internal;
10+
import graphql.execution.CoercedVariables;
911
import graphql.execution.MergedField;
12+
import graphql.execution.NormalizedVariables;
1013
import graphql.execution.ValuesResolver;
1114
import graphql.language.Directive;
1215
import graphql.language.Field;
1316
import graphql.normalized.NormalizedInputValue;
1417
import graphql.schema.GraphQLArgument;
1518
import graphql.schema.GraphQLDirective;
1619
import graphql.schema.GraphQLSchema;
17-
import graphql.util.FpKit;
1820
import graphql.util.LockKit;
1921
import org.jetbrains.annotations.Nullable;
2022

@@ -23,7 +25,9 @@
2325
import java.util.List;
2426
import java.util.Locale;
2527
import java.util.Map;
28+
import java.util.function.Supplier;
2629

30+
import static graphql.Assert.assertNotNull;
2731
import static graphql.collect.ImmutableKit.emptyList;
2832

2933
/**
@@ -37,9 +41,8 @@ public class QueryDirectivesImpl implements QueryDirectives {
3741
private final DirectivesResolver directivesResolver = new DirectivesResolver();
3842
private final MergedField mergedField;
3943
private final GraphQLSchema schema;
40-
private final Map<String, Object> coercedVariables;
41-
@Nullable
42-
private final Map<String, NormalizedInputValue> normalizedVariableValues;
44+
private final CoercedVariables coercedVariables;
45+
private final Supplier<NormalizedVariables> normalizedVariableValues;
4346
private final GraphQLContext graphQLContext;
4447
private final Locale locale;
4548

@@ -50,13 +53,13 @@ public class QueryDirectivesImpl implements QueryDirectives {
5053
private volatile ImmutableMap<String, List<QueryAppliedDirective>> fieldAppliedDirectivesByName;
5154
private volatile ImmutableMap<QueryAppliedDirective, Map<String, NormalizedInputValue>> normalizedValuesByAppliedDirective;
5255

53-
public QueryDirectivesImpl(MergedField mergedField, GraphQLSchema schema, Map<String, Object> coercedVariables, Map<String, NormalizedInputValue> normalizedVariableValues, GraphQLContext graphQLContext, Locale locale) {
54-
this.mergedField = mergedField;
55-
this.schema = schema;
56-
this.coercedVariables = coercedVariables;
57-
this.normalizedVariableValues = normalizedVariableValues;
58-
this.graphQLContext = graphQLContext;
59-
this.locale = locale;
56+
public QueryDirectivesImpl(MergedField mergedField, GraphQLSchema schema, CoercedVariables coercedVariables, Supplier<NormalizedVariables> normalizedVariableValues, GraphQLContext graphQLContext, Locale locale) {
57+
this.mergedField = assertNotNull(mergedField);
58+
this.schema = assertNotNull(schema);
59+
this.coercedVariables = assertNotNull(coercedVariables);
60+
this.normalizedVariableValues = assertNotNull(normalizedVariableValues);
61+
this.graphQLContext = assertNotNull(graphQLContext);
62+
this.locale = assertNotNull(locale);
6063
}
6164

6265
private void computeValuesLazily() {
@@ -100,14 +103,16 @@ private void computeValuesLazily() {
100103

101104
// create NormalizedInputValue values for directive arguments
102105
Map<QueryAppliedDirective, Map<String, NormalizedInputValue>> normalizedValuesByAppliedDirective = new LinkedHashMap<>();
103-
if (this.normalizedVariableValues != null) {
106+
NormalizedVariables normalizedVariableValues = this.normalizedVariableValues.get();
107+
if (normalizedVariableValues != null) {
104108
byNameApplied.values().forEach(directiveList -> {
105109
for (QueryAppliedDirective queryAppliedDirective : directiveList) {
106110
GraphQLDirective graphQLDirective = gqlDirectiveCounterPartsInverse.get(queryAppliedDirective);
107111
// we need this counterpart because the ValuesResolver needs the runtime and AST element
108112
Directive directive = directiveCounterParts.get(graphQLDirective);
109113
if (directive != null) {
110-
Map<String, NormalizedInputValue> normalizedArgumentValues = ValuesResolver.getNormalizedArgumentValues(graphQLDirective.getArguments(), directive.getArguments(), this.normalizedVariableValues);
114+
Map<String, NormalizedInputValue> normalizedVariables = normalizedVariableValues.toMap();
115+
Map<String, NormalizedInputValue> normalizedArgumentValues = ValuesResolver.getNormalizedArgumentValues(graphQLDirective.getArguments(), directive.getArguments(), normalizedVariables);
111116
normalizedValuesByAppliedDirective.put(queryAppliedDirective, normalizedArgumentValues);
112117
}
113118
}

src/main/java/graphql/normalized/ExecutableNormalizedOperationFactory.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -518,11 +518,10 @@ private ExecutableNormalizedOperation createNormalizedQueryImpl() {
518518

519519
private void captureMergedField(ExecutableNormalizedField enf, MergedField mergedFld) {
520520
// QueryDirectivesImpl is a lazy object and only computes itself when asked for
521-
Map<String, NormalizedInputValue> normalizedVariableValues = Optional.ofNullable(this.normalizedVariableValues).map(NormalizedVariables::toMap).orElse(null);
522521
QueryDirectives queryDirectives = new QueryDirectivesImpl(mergedFld,
523522
graphQLSchema,
524-
coercedVariableValues.toMap(),
525-
normalizedVariableValues,
523+
coercedVariableValues,
524+
() -> normalizedVariableValues,
526525
options.getGraphQLContext(),
527526
options.getLocale());
528527
normalizedFieldToQueryDirectives.put(enf, queryDirectives);

src/test/groovy/graphql/execution/directives/QueryDirectivesImplTest.groovy

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ class QueryDirectivesImplTest extends Specification {
3535

3636
def mergedField = MergedField.newMergedField([f1, f2]).build()
3737

38-
def impl = new QueryDirectivesImpl(mergedField, schema, [var: 10], [var : new NormalizedInputValue("type", IntValue.of(10))], GraphQLContext.getDefault(), Locale.getDefault())
38+
def impl = new QueryDirectivesImpl(mergedField, schema,
39+
CoercedVariables.of([var: 10]),
40+
{ -> NormalizedVariables.of([var: new NormalizedInputValue("type", IntValue.of(10))]) },
41+
GraphQLContext.getDefault(), Locale.getDefault())
3942

4043
when:
4144
def directives = impl.getImmediateDirectivesByName()
@@ -80,7 +83,7 @@ class QueryDirectivesImplTest extends Specification {
8083
.mergedField(mergedField)
8184
.schema(schema)
8285
.coercedVariables(CoercedVariables.of([var: 10]))
83-
.normalizedVariables(NormalizedVariables.of([var: new NormalizedInputValue("type", IntValue.of(10))]))
86+
.normalizedVariables({ NormalizedVariables.of([var: new NormalizedInputValue("type", IntValue.of(10))]) })
8487
.graphQLContext(GraphQLContext.getDefault())
8588
.locale(Locale.getDefault())
8689
.build()

0 commit comments

Comments
 (0)