Skip to content

Commit e7409ac

Browse files
authored
Adding Locale to Coercing and hence ValueResolver (#2912)
* A propose shape change on Coercing * deprecation * Added parser new signature * Changed ValueResolver so it took Locale * Changed calls to coerce to use new environment * Changed calls to coerce to NOT use new environment * Updated tests to use Locale * Formatting of ValuesResolver * Move to graphql context for coercing and also got the parsing error messages in place * Merged in master * Added Locale to Coercing and everywhere that calls it * now using Locale in the standard scalars * Break out legacy ValuesResolver code * Some renaming of methods * Refactoring ValueResolver so that conversion methods are in another class * DirectivesResolver should take context and locale from input * Reverting Parser changes - they can go into their own PR * PR feedback * PR feedback- fixed tests
1 parent 4652d3d commit e7409ac

57 files changed

Lines changed: 1840 additions & 918 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/main/java/graphql/ExecutionInput.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ private ExecutionInput(Builder builder) {
4444
this.dataLoaderRegistry = builder.dataLoaderRegistry;
4545
this.cacheControl = builder.cacheControl;
4646
this.executionId = builder.executionId;
47-
this.locale = builder.locale;
47+
this.locale = builder.locale != null ? builder.locale : Locale.getDefault(); // always have a locale in place
4848
this.localContext = builder.localContext;
4949
this.extensions = builder.extensions;
5050
}

src/main/java/graphql/GraphQLContext.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,13 @@ public static GraphQLContext of(Consumer<GraphQLContext.Builder> contextBuilderC
224224
return of(builder.map);
225225
}
226226

227+
/**
228+
* @return a new and empty graphql context object
229+
*/
230+
public static GraphQLContext getDefault() {
231+
return GraphQLContext.newContext().build();
232+
}
233+
227234
/**
228235
* Creates a new GraphqlContext builder
229236
*

src/main/java/graphql/analysis/NodeVisitorWithTypeTracking.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package graphql.analysis;
22

3+
import graphql.GraphQLContext;
34
import graphql.Internal;
45
import graphql.execution.CoercedVariables;
56
import graphql.execution.ConditionalNodes;
@@ -29,6 +30,7 @@
2930
import graphql.util.TraversalControl;
3031
import graphql.util.TraverserContext;
3132

33+
import java.util.Locale;
3234
import java.util.Map;
3335

3436
import static graphql.Assert.assertNotNull;
@@ -151,7 +153,12 @@ public TraversalControl visitField(Field field, TraverserContext<Node> context)
151153
boolean isTypeNameIntrospectionField = fieldDefinition == schema.getIntrospectionTypenameFieldDefinition();
152154
GraphQLFieldsContainer fieldsContainer = !isTypeNameIntrospectionField ? (GraphQLFieldsContainer) unwrapAll(parentEnv.getOutputType()) : null;
153155
GraphQLCodeRegistry codeRegistry = schema.getCodeRegistry();
154-
Map<String, Object> argumentValues = ValuesResolver.getArgumentValues(codeRegistry, fieldDefinition.getArguments(), field.getArguments(), CoercedVariables.of(variables));
156+
Map<String, Object> argumentValues = ValuesResolver.getArgumentValues(codeRegistry,
157+
fieldDefinition.getArguments(),
158+
field.getArguments(),
159+
CoercedVariables.of(variables),
160+
GraphQLContext.getDefault(),
161+
Locale.getDefault());
155162
QueryVisitorFieldEnvironment environment = new QueryVisitorFieldEnvironmentImpl(isTypeNameIntrospectionField,
156163
field,
157164
fieldDefinition,

src/main/java/graphql/analysis/QueryTraverser.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package graphql.analysis;
22

3+
import graphql.GraphQLContext;
34
import graphql.PublicApi;
45
import graphql.execution.CoercedVariables;
56
import graphql.execution.RawVariables;
@@ -20,6 +21,7 @@
2021
import java.util.Collections;
2122
import java.util.LinkedHashMap;
2223
import java.util.List;
24+
import java.util.Locale;
2325
import java.util.Map;
2426

2527
import static graphql.Assert.assertNotNull;
@@ -70,7 +72,7 @@ private QueryTraverser(GraphQLSchema schema,
7072
this.fragmentsByName = getOperationResult.fragmentsByName;
7173
this.roots = singletonList(getOperationResult.operationDefinition);
7274
this.rootParentType = getRootTypeFromOperation(getOperationResult.operationDefinition);
73-
this.coercedVariables = ValuesResolver.coerceVariableValues(schema, variableDefinitions, rawVariables);
75+
this.coercedVariables = ValuesResolver.coerceVariableValues(schema, variableDefinitions, rawVariables, GraphQLContext.getDefault(), Locale.getDefault());
7476
}
7577

7678
private QueryTraverser(GraphQLSchema schema,

src/main/java/graphql/execution/ConditionalNodes.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package graphql.execution;
22

33
import graphql.Assert;
4+
import graphql.GraphQLContext;
45
import graphql.Internal;
56
import graphql.language.Directive;
67
import graphql.language.NodeUtil;
78

89
import java.util.List;
10+
import java.util.Locale;
911
import java.util.Map;
1012

1113
import static graphql.Directives.IncludeDirective;
@@ -30,7 +32,7 @@ public boolean shouldInclude(Map<String, Object> variables, List<Directive> dire
3032
private boolean getDirectiveResult(Map<String, Object> variables, List<Directive> directives, String directiveName, boolean defaultValue) {
3133
Directive foundDirective = NodeUtil.findNodeByName(directives, directiveName);
3234
if (foundDirective != null) {
33-
Map<String, Object> argumentValues = ValuesResolver.getArgumentValues(SkipDirective.getArguments(), foundDirective.getArguments(), CoercedVariables.of(variables));
35+
Map<String, Object> argumentValues = ValuesResolver.getArgumentValues(SkipDirective.getArguments(), foundDirective.getArguments(), CoercedVariables.of(variables), GraphQLContext.getDefault(), Locale.getDefault());
3436
Object flag = argumentValues.get("if");
3537
Assert.assertTrue(flag instanceof Boolean, () -> String.format("The '%s' directive MUST have a value for the 'if' argument", directiveName));
3638
return (Boolean) flag;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public CompletableFuture<ExecutionResult> execute(Document document, GraphQLSche
6363

6464
CoercedVariables coercedVariables;
6565
try {
66-
coercedVariables = ValuesResolver.coerceVariableValues(graphQLSchema, variableDefinitions, inputVariables);
66+
coercedVariables = ValuesResolver.coerceVariableValues(graphQLSchema, variableDefinitions, inputVariables, executionInput.getGraphQLContext(), executionInput.getLocale());
6767
} catch (RuntimeException rte) {
6868
if (rte instanceof GraphQLError) {
6969
return completedFuture(new ExecutionResultImpl((GraphQLError) rte));

src/main/java/graphql/execution/ExecutionStepInfoFactory.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ public ExecutionStepInfo newExecutionStepInfoForSubField(ExecutionContext execut
2323
GraphQLOutputType fieldType = fieldDefinition.getType();
2424
List<Argument> fieldArgs = mergedField.getArguments();
2525
GraphQLCodeRegistry codeRegistry = executionContext.getGraphQLSchema().getCodeRegistry();
26-
Supplier<Map<String, Object>> argumentValues = FpKit.intraThreadMemoize(() -> ValuesResolver.getArgumentValues(codeRegistry, fieldDefinition.getArguments(), fieldArgs, executionContext.getCoercedVariables()));
26+
Supplier<Map<String, Object>> argumentValuesSupplier = () -> ValuesResolver.getArgumentValues(codeRegistry,
27+
fieldDefinition.getArguments(),
28+
fieldArgs,
29+
executionContext.getCoercedVariables(),
30+
executionContext.getGraphQLContext(),
31+
executionContext.getLocale());
32+
Supplier<Map<String, Object>> argumentValues = FpKit.intraThreadMemoize(argumentValuesSupplier);
2733

2834
ResultPath newPath = parentInfo.getPath().segment(mergedField.getResultKey());
2935

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

Lines changed: 13 additions & 3 deletions
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)
@@ -590,7 +594,7 @@ protected FieldValueInfo completeValueForList(ExecutionContext executionContext,
590594
protected CompletableFuture<ExecutionResult> completeValueForScalar(ExecutionContext executionContext, ExecutionStrategyParameters parameters, GraphQLScalarType scalarType, Object result) {
591595
Object serialized;
592596
try {
593-
serialized = scalarType.getCoercing().serialize(result);
597+
serialized = scalarType.getCoercing().serialize(result, executionContext.getGraphQLContext(), executionContext.getLocale());
594598
} catch (CoercingSerializeException e) {
595599
serialized = handleCoercionProblem(executionContext, parameters, e);
596600
}
@@ -820,7 +824,13 @@ protected ExecutionStepInfo createExecutionStepInfo(ExecutionContext executionCo
820824
if (!fieldArgDefs.isEmpty()) {
821825
List<Argument> fieldArgs = field.getArguments();
822826
GraphQLCodeRegistry codeRegistry = executionContext.getGraphQLSchema().getCodeRegistry();
823-
argumentValues = FpKit.intraThreadMemoize(() -> ValuesResolver.getArgumentValues(codeRegistry, fieldArgDefs, fieldArgs, executionContext.getCoercedVariables()));
827+
Supplier<Map<String, Object>> argValuesSupplier = () -> ValuesResolver.getArgumentValues(codeRegistry,
828+
fieldArgDefs,
829+
fieldArgs,
830+
executionContext.getCoercedVariables(),
831+
executionContext.getGraphQLContext(),
832+
executionContext.getLocale());
833+
argumentValues = FpKit.intraThreadMemoize(argValuesSupplier);
824834
}
825835

826836

0 commit comments

Comments
 (0)