Skip to content

Conversation

@bbakerman
Copy link
Member

@bbakerman bbakerman commented Aug 5, 2022

We would like to support Locale when coer-ercing values or parsing document text. So we need that passed in.

This has chosen to pass in a GraphQLContext and a Locale to a scalar Coercing so that :

  1. a Locale can be present
  2. the Scalar can use the context object for other purposes if it wants to

There are stacks of time where need to use ValueResolver and its methods to resolve values. We need a defaulted locale here and default GraphqlContext to make this work.

The ValueResolver has been substantially re-arranged - none of its logic has changed but the graphql context and locale have been passed on through it.

The class was 1000 lines long. Why too much code in it.

I have split it into 2 - one with the legacy value code into its own class (and then accessed via the ValueResolver) and one with code for value conversions (external to literal etc...)

But the code itself really has not changed. The tests show that - only graphql context and locale have been added

Originally I put the Parser code here (passing in Locale to it) in this PR but it was too big - this is plenty big enough already - so I will make another PR for that

/**
* Allows you to build a new coercing environment
*
* @param <T> for two
Copy link
Member

Choose a reason for hiding this comment

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

lol

ValueMode valueMode) {
GraphQLInputObjectType inputObjectType,
Object inputValue,
ValueMode valueMode, Locale locale) {
Copy link
Member

Choose a reason for hiding this comment

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

Super nit: add new line before Locale

List<VariableDefinition> variableDefinitions,
RawVariables rawVariables) {
List<VariableDefinition> variableDefinitions,
RawVariables rawVariables, Locale locale) {
Copy link
Member

Choose a reason for hiding this comment

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

Super nit: add new line before Locale

GraphQLType graphQLType,
Object value) throws NonNullableValueCoercedAsNullException, CoercingParseValueException {
GraphQLType graphQLType,
Object value, Locale locale) throws NonNullableValueCoercedAsNullException, CoercingParseValueException {
Copy link
Member

Choose a reason for hiding this comment

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

Super nit: add new line before Locale

*
* @param <T> for two
*/
public interface CoercingEnvironment<T> {
Copy link
Member Author

Choose a reason for hiding this comment

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

Feedback - kill it - its not an overhead we want to add in terms of object allocation

@bbakerman bbakerman changed the title A propose shape change on Coercing Adding Locale to Parser, Coercing and hence ValueResolver Aug 10, 2022
@bbakerman bbakerman added this to the 20.0 milestone Aug 12, 2022
CoercedVariables.of(variables),
GraphQLContext.getDefault(),
Locale.getDefault());
QueryVisitorFieldEnvironment environment = new QueryVisitorFieldEnvironmentImpl(isTypeNameIntrospectionField,
Copy link
Member Author

Choose a reason for hiding this comment

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

This entry point come not from a graphql request and hence uses a default graphql context and default locale. Perhaps in some future change these are passed in.

this.roots = singletonList(getOperationResult.operationDefinition);
this.rootParentType = getRootTypeFromOperation(getOperationResult.operationDefinition);
this.coercedVariables = ValuesResolver.coerceVariableValues(schema, variableDefinitions, rawVariables);
this.coercedVariables = ValuesResolver.coerceVariableValues(schema, variableDefinitions, rawVariables, GraphQLContext.getDefault(), Locale.getDefault());
Copy link
Member Author

Choose a reason for hiding this comment

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

This entry point come not from a graphql request and hence uses a default graphql context and default locale. Perhaps in some future change these are passed in.

Directive foundDirective = NodeUtil.findNodeByName(directives, directiveName);
if (foundDirective != null) {
Map<String, Object> argumentValues = ValuesResolver.getArgumentValues(SkipDirective.getArguments(), foundDirective.getArguments(), CoercedVariables.of(variables));
Map<String, Object> argumentValues = ValuesResolver.getArgumentValues(SkipDirective.getArguments(), foundDirective.getArguments(), CoercedVariables.of(variables), GraphQLContext.getDefault(), Locale.getDefault());
Copy link
Member Author

Choose a reason for hiding this comment

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

This entry point come not from a graphql request and hence uses a default graphql context and default locale. Perhaps in some future change these are passed in.

CoercedVariables coercedVariables;
try {
coercedVariables = ValuesResolver.coerceVariableValues(graphQLSchema, variableDefinitions, inputVariables);
coercedVariables = ValuesResolver.coerceVariableValues(graphQLSchema, variableDefinitions, inputVariables, executionInput.getGraphQLContext(), executionInput.getLocale());
Copy link
Member Author

Choose a reason for hiding this comment

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

context and locale take from ExecutionInput

}
return serialized == null;
}
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Legacy code moved


protected Object serializeScalarValue(Object toAnalyze, GraphQLScalarType scalarType) throws CoercingSerializeException {
return scalarType.getCoercing().serialize(toAnalyze);
return scalarType.getCoercing().serialize(toAnalyze, GraphQLContext.getDefault(), Locale.getDefault());
Copy link
Member Author

Choose a reason for hiding this comment

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

code will die - here only to compile

*/
public enum BundleType {
Parsing,
Scalars,
Copy link
Member Author

Choose a reason for hiding this comment

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

Our new i18n areas

private static String printDefaultValue(InputValueWithState inputValueWithState, GraphQLInputType type) {
return AstPrinter.printAst(ValuesResolver.valueToLiteral(DEFAULT_FIELD_VISIBILITY, inputValueWithState, type));
private static String printDefaultValue(InputValueWithState inputValueWithState, GraphQLInputType type, GraphQLContext graphQLContext, Locale locale) {
return AstPrinter.printAst(ValuesResolver.valueToLiteral(inputValueWithState, type, graphQLContext, locale));
Copy link
Member Author

Choose a reason for hiding this comment

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

The method defaults this. Used a better method

@bbakerman bbakerman changed the title Adding Locale to Parser, Coercing and hence ValueResolver Adding Locale to Coercing and hence ValueResolver Aug 15, 2022
…coercing

# Conflicts:
#	src/main/java/graphql/execution/nextgen/ExecutionHelper.java
#	src/main/java/graphql/execution/nextgen/FetchedValueAnalyzer.java
#	src/main/java/graphql/execution/nextgen/ValueFetcher.java
#	src/main/java/graphql/schema/idl/FetchSchemaDirectiveWiring.java
Int.notInt=Expected a value that can be converted to type 'Int' but it was a ''{0}''
#
ID.unexpectedAstType=Expected an AST type of ''IntValue'' or ''StringValue'' but it was a ''{0}''.
ID.notId=Expected a value that can be converted to type 'ID' but it was a ''{0}''
Copy link
Member

Choose a reason for hiding this comment

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

Nit: could remove the single quotes around 'ID', they won't be rendered

ID.unexpectedAstType=Expected an AST type of ''IntValue'' or ''StringValue'' but it was a ''{0}''.
ID.notId=Expected a value that can be converted to type 'ID' but it was a ''{0}''
#
Float.notFloat=Expected a value that can be converted to type 'Float' but it was a ''{0}''
Copy link
Member

Choose a reason for hiding this comment

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

Nit: could remove the single quotes around 'Float', they won't be rendered

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

Float.notFloat=Expected a value that can be converted to type 'Float' but it was a ''{0}''
Float.unexpectedAstType=Expected an AST type of ''IntValue'' or ''FloatValue'' but it was a ''{0}''.
#
Boolean.notBoolean=Expected a value that can be converted to type 'Boolean' but it was a ''{0}''
Copy link
Member

Choose a reason for hiding this comment

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

Nit: could remove the single quotes around 'Boolean', they won't be rendered

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

* @param graphQLContext the graphql context in place
* @param locale the locale to use
*
* @return a parsed value which is never null

Choose a reason for hiding this comment

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

Since this method doesn't carry the @NotNull return annotation anymore, should this comment be updated?

Copy link
Member

Choose a reason for hiding this comment

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

Thanks, that's a good catch. I'll fix it up

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants