Skip to content

Commit c615df1

Browse files
committed
DirectivesResolver should take context and locale from input
1 parent 9d42a8d commit c615df1

5 files changed

Lines changed: 26 additions & 9 deletions

File tree

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,11 @@ protected CompletableFuture<FetchedValue> fetchField(ExecutionContext executionC
250250

251251
// DataFetchingFieldSelectionSet and QueryDirectives is a supplier of sorts - eg a lazy pattern
252252
DataFetchingFieldSelectionSet fieldCollector = DataFetchingFieldSelectionSetImpl.newCollector(executionContext.getGraphQLSchema(), fieldType, normalizedFieldSupplier);
253-
QueryDirectives queryDirectives = new QueryDirectivesImpl(field, executionContext.getGraphQLSchema(), executionContext.getVariables());
253+
QueryDirectives queryDirectives = new QueryDirectivesImpl(field,
254+
executionContext.getGraphQLSchema(),
255+
executionContext.getCoercedVariables().toMap(),
256+
executionContext.getGraphQLContext(),
257+
executionContext.getLocale());
254258

255259

256260
DataFetchingEnvironment environment = newDataFetchingEnvironment(executionContext)

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,27 @@ public class DirectivesResolver {
2525
public DirectivesResolver() {
2626
}
2727

28-
public Map<String, GraphQLDirective> resolveDirectives(List<Directive> directives, GraphQLSchema schema, Map<String, Object> variables) {
28+
public Map<String, GraphQLDirective> resolveDirectives(List<Directive> directives, GraphQLSchema schema, Map<String, Object> variables, GraphQLContext graphQLContext, Locale locale) {
2929
GraphQLCodeRegistry codeRegistry = schema.getCodeRegistry();
3030
Map<String, GraphQLDirective> directiveMap = new LinkedHashMap<>();
3131
directives.forEach(directive -> {
3232
GraphQLDirective protoType = schema.getDirective(directive.getName());
3333
if (protoType != null) {
34-
GraphQLDirective newDirective = protoType.transform(builder -> buildArguments(builder, codeRegistry, protoType, directive, variables));
34+
GraphQLDirective newDirective = protoType.transform(builder -> buildArguments(builder, codeRegistry, protoType, directive, variables, graphQLContext, locale));
3535
directiveMap.put(newDirective.getName(), newDirective);
3636
}
3737
});
3838
return ImmutableMap.copyOf(directiveMap);
3939
}
4040

41-
private void buildArguments(GraphQLDirective.Builder directiveBuilder, GraphQLCodeRegistry codeRegistry, GraphQLDirective protoType, Directive fieldDirective, Map<String, Object> variables) {
42-
Map<String, Object> argumentValues = ValuesResolver.getArgumentValues(codeRegistry, protoType.getArguments(), fieldDirective.getArguments(), CoercedVariables.of(variables), GraphQLContext.getDefault(), Locale.getDefault());
41+
private void buildArguments(GraphQLDirective.Builder directiveBuilder,
42+
GraphQLCodeRegistry codeRegistry,
43+
GraphQLDirective protoType,
44+
Directive fieldDirective,
45+
Map<String, Object> variables,
46+
GraphQLContext graphQLContext,
47+
Locale locale) {
48+
Map<String, Object> argumentValues = ValuesResolver.getArgumentValues(codeRegistry, protoType.getArguments(), fieldDirective.getArguments(), CoercedVariables.of(variables), graphQLContext, locale);
4349
directiveBuilder.clearArguments();
4450
protoType.getArguments().forEach(protoArg -> {
4551
if (argumentValues.containsKey(protoArg.getName())) {

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.google.common.collect.ImmutableList;
44
import com.google.common.collect.ImmutableMap;
5+
import graphql.GraphQLContext;
56
import graphql.Internal;
67
import graphql.collect.ImmutableKit;
78
import graphql.execution.MergedField;
@@ -14,6 +15,7 @@
1415
import java.util.ArrayList;
1516
import java.util.LinkedHashMap;
1617
import java.util.List;
18+
import java.util.Locale;
1719
import java.util.Map;
1820

1921
import static graphql.collect.ImmutableKit.emptyList;
@@ -30,15 +32,19 @@ public class QueryDirectivesImpl implements QueryDirectives {
3032
private final MergedField mergedField;
3133
private final GraphQLSchema schema;
3234
private final Map<String, Object> variables;
35+
private final GraphQLContext graphQLContext;
36+
private final Locale locale;
3337
private volatile ImmutableMap<Field, List<GraphQLDirective>> fieldDirectivesByField;
3438
private volatile ImmutableMap<String, List<GraphQLDirective>> fieldDirectivesByName;
3539
private volatile ImmutableMap<Field, List<QueryAppliedDirective>> fieldAppliedDirectivesByField;
3640
private volatile ImmutableMap<String, List<QueryAppliedDirective>> fieldAppliedDirectivesByName;
3741

38-
public QueryDirectivesImpl(MergedField mergedField, GraphQLSchema schema, Map<String, Object> variables) {
42+
public QueryDirectivesImpl(MergedField mergedField, GraphQLSchema schema, Map<String, Object> variables, GraphQLContext graphQLContext, Locale locale) {
3943
this.mergedField = mergedField;
4044
this.schema = schema;
4145
this.variables = variables;
46+
this.graphQLContext = graphQLContext;
47+
this.locale = locale;
4248
}
4349

4450
private void computeValuesLazily() {
@@ -53,7 +59,7 @@ private void computeValuesLazily() {
5359
List<Directive> directives = field.getDirectives();
5460
ImmutableList<GraphQLDirective> resolvedDirectives = ImmutableList.copyOf(
5561
directivesResolver
56-
.resolveDirectives(directives, schema, variables)
62+
.resolveDirectives(directives, schema, variables, graphQLContext, locale)
5763
.values()
5864
);
5965
byField.put(field, resolvedDirectives);

‎src/main/java/graphql/execution/nextgen/ValueFetcher.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public CompletableFuture<FetchedValue> fetchValue(ExecutionContext executionCont
122122

123123
Supplier<Map<String, Object>> argumentValues = FpKit.intraThreadMemoize(() -> ValuesResolver.getArgumentValues(codeRegistry, fieldDef.getArguments(), field.getArguments(), executionContext.getCoercedVariables(), GraphQLContext.getDefault(), Locale.getDefault()));
124124

125-
QueryDirectivesImpl queryDirectives = new QueryDirectivesImpl(sameFields, executionContext.getGraphQLSchema(), executionContext.getVariables());
125+
QueryDirectivesImpl queryDirectives = new QueryDirectivesImpl(sameFields, executionContext.getGraphQLSchema(), executionContext.getCoercedVariables().toMap(), executionContext.getGraphQLContext(), executionContext.getLocale());
126126

127127
GraphQLOutputType fieldType = fieldDef.getType();
128128

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package graphql.execution.directives
22

3+
import graphql.GraphQLContext
34
import graphql.TestUtil
45
import graphql.execution.MergedField
56
import spock.lang.Specification
@@ -30,7 +31,7 @@ class QueryDirectivesImplTest extends Specification {
3031

3132
def mergedField = MergedField.newMergedField([f1, f2]).build()
3233

33-
def impl = new QueryDirectivesImpl(mergedField, schema, [var: 10])
34+
def impl = new QueryDirectivesImpl(mergedField, schema, [var: 10], GraphQLContext.getDefault(), Locale.getDefault())
3435

3536
when:
3637
def directives = impl.getImmediateDirectivesByName()

0 commit comments

Comments
 (0)