Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This introduces an input interceptor #3188

Merged
merged 2 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
219 changes: 156 additions & 63 deletions src/main/java/graphql/execution/ValuesResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import graphql.GraphQLContext;
import graphql.Internal;
import graphql.collect.ImmutableKit;
import graphql.execution.values.InputInterceptor;
Copy link
Member Author

Choose a reason for hiding this comment

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

There is a LOT of reformatting going on here.

This class AND ValuesResolverCoercion are a mess of inter call dependency

I found it really hard to navigate what method is being called and so on.

So I made the code multi line in parameter terms to help make it more readable

What you will see it InputInterceptor being passed around everywhere. Some of the code (literal coercing say) don't need it BUT because they can in theory go into value coercion code, it needs to be put in place

Rather than refactor these two classes - I just passed it around everywhere so that it asked for once and then used. For performance reasons I do NOT do a graphqlContext.get() at the inner method since I could do that per time its called. Rather I asked for it once on the outer layers and then pass it everywhere.

import graphql.language.Argument;
import graphql.language.ArrayValue;
import graphql.language.NullValue;
Expand Down Expand Up @@ -78,7 +79,14 @@ public static CoercedVariables coerceVariableValues(GraphQLSchema schema,
GraphQLContext graphqlContext,
Locale locale) throws CoercingParseValueException, NonNullableValueCoercedAsNullException {

return ValuesResolverConversion.externalValueToInternalValueForVariables(schema, variableDefinitions, rawVariables, graphqlContext, locale);
InputInterceptor inputInterceptor = graphqlContext.get(InputInterceptor.class);
return ValuesResolverConversion.externalValueToInternalValueForVariables(
inputInterceptor,
schema,
variableDefinitions,
rawVariables,
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.

See multi line parameters with extra inputInterceptor passed in.



Expand All @@ -93,11 +101,13 @@ public static CoercedVariables coerceVariableValues(GraphQLSchema schema,
*
* @return a map of the normalised values
*/
public static Map<String, NormalizedInputValue> getNormalizedVariableValues(GraphQLSchema schema,
List<VariableDefinition> variableDefinitions,
RawVariables rawVariables,
GraphQLContext graphqlContext,
Locale locale) {
public static Map<String, NormalizedInputValue> getNormalizedVariableValues(
GraphQLSchema schema,
List<VariableDefinition> variableDefinitions,
RawVariables rawVariables,
GraphQLContext graphqlContext,
Locale locale
) {
GraphqlFieldVisibility fieldVisibility = schema.getCodeRegistry().getFieldVisibility();
Map<String, NormalizedInputValue> result = new LinkedHashMap<>();
for (VariableDefinition variableDefinition : variableDefinitions) {
Expand Down Expand Up @@ -138,12 +148,15 @@ public static Map<String, NormalizedInputValue> getNormalizedVariableValues(Grap
*
* @return a map of named argument values
*/
public static Map<String, Object> getArgumentValues(List<GraphQLArgument> argumentTypes,
List<Argument> arguments,
CoercedVariables coercedVariables,
GraphQLContext graphqlContext,
Locale locale) {
return getArgumentValuesImpl(DEFAULT_FIELD_VISIBILITY, argumentTypes, arguments, coercedVariables, graphqlContext, locale);
public static Map<String, Object> getArgumentValues(
List<GraphQLArgument> argumentTypes,
List<Argument> arguments,
CoercedVariables coercedVariables,
GraphQLContext graphqlContext,
Locale locale
) {
InputInterceptor inputInterceptor = graphqlContext.get(InputInterceptor.class);
return getArgumentValuesImpl(inputInterceptor, DEFAULT_FIELD_VISIBILITY, argumentTypes, arguments, coercedVariables, graphqlContext, locale);
}

/**
Expand All @@ -155,9 +168,11 @@ public static Map<String, Object> getArgumentValues(List<GraphQLArgument> argume
*
* @return a map of named normalised values
*/
public static Map<String, NormalizedInputValue> getNormalizedArgumentValues(List<GraphQLArgument> argumentTypes,
List<Argument> arguments,
Map<String, NormalizedInputValue> normalizedVariables) {
public static Map<String, NormalizedInputValue> getNormalizedArgumentValues(
List<GraphQLArgument> argumentTypes,
List<Argument> arguments,
Map<String, NormalizedInputValue> normalizedVariables
) {
if (argumentTypes.isEmpty()) {
return ImmutableKit.emptyMap();
}
Expand All @@ -183,13 +198,16 @@ public static Map<String, NormalizedInputValue> getNormalizedArgumentValues(List
return result;
}

public static Map<String, Object> getArgumentValues(GraphQLCodeRegistry codeRegistry,
List<GraphQLArgument> argumentTypes,
List<Argument> arguments,
CoercedVariables coercedVariables,
GraphQLContext graphqlContext,
Locale locale) {
return getArgumentValuesImpl(codeRegistry.getFieldVisibility(), argumentTypes, arguments, coercedVariables, graphqlContext, locale);
public static Map<String, Object> getArgumentValues(
GraphQLCodeRegistry codeRegistry,
List<GraphQLArgument> argumentTypes,
List<Argument> arguments,
CoercedVariables coercedVariables,
GraphQLContext graphqlContext,
Locale locale
) {
InputInterceptor inputInterceptor = graphqlContext.get(InputInterceptor.class);
return getArgumentValuesImpl(inputInterceptor, codeRegistry.getFieldVisibility(), argumentTypes, arguments, coercedVariables, graphqlContext, locale);
}

/**
Expand All @@ -205,26 +223,50 @@ public static Map<String, Object> getArgumentValues(GraphQLCodeRegistry codeRegi
*
* @return a value converted to a literal
*/
public static Value<?> valueToLiteral(@NotNull GraphqlFieldVisibility fieldVisibility,
@NotNull InputValueWithState inputValueWithState,
@NotNull GraphQLType type,
GraphQLContext graphqlContext,
Locale locale) {
return (Value<?>) ValuesResolverConversion.valueToLiteralImpl(fieldVisibility, inputValueWithState, type, ValueMode.LITERAL, graphqlContext, locale);
public static Value<?> valueToLiteral(
@NotNull GraphqlFieldVisibility fieldVisibility,
@NotNull InputValueWithState inputValueWithState,
@NotNull GraphQLType type,
GraphQLContext graphqlContext,
Locale locale
) {
return (Value<?>) ValuesResolverConversion.valueToLiteralImpl(
fieldVisibility,
inputValueWithState,
type,
ValueMode.LITERAL,
graphqlContext,
locale);
}

public static Value<?> valueToLiteral(@NotNull InputValueWithState inputValueWithState,
@NotNull GraphQLType type,
GraphQLContext graphqlContext,
Locale locale) {
return (Value<?>) ValuesResolverConversion.valueToLiteralImpl(DEFAULT_FIELD_VISIBILITY, inputValueWithState, type, ValueMode.LITERAL, graphqlContext, locale);
public static Value<?> valueToLiteral(
@NotNull InputValueWithState inputValueWithState,
@NotNull GraphQLType type,
GraphQLContext graphqlContext,
Locale locale
) {
return (Value<?>) ValuesResolverConversion.valueToLiteralImpl(
DEFAULT_FIELD_VISIBILITY,
inputValueWithState,
type,
ValueMode.LITERAL,
graphqlContext,
locale);
}

public static Object valueToInternalValue(InputValueWithState inputValueWithState,
GraphQLType type,
GraphQLContext graphqlContext,
Locale locale) throws CoercingParseValueException, CoercingParseLiteralException {
return ValuesResolverConversion.valueToInternalValueImpl(inputValueWithState, type, graphqlContext, locale);
public static Object valueToInternalValue(
InputValueWithState inputValueWithState,
GraphQLInputType inputType,
GraphQLContext graphqlContext,
Locale locale
) throws CoercingParseValueException, CoercingParseLiteralException {
InputInterceptor inputInterceptor = graphqlContext.get(InputInterceptor.class);
return ValuesResolverConversion.valueToInternalValueImpl(
inputInterceptor,
inputValueWithState,
inputType,
graphqlContext,
locale);
}

/**
Expand All @@ -238,34 +280,52 @@ public static Object valueToInternalValue(InputValueWithState inputValueWithStat
*
* @return a value converted to an internal value
*/
public static Object externalValueToInternalValue(GraphqlFieldVisibility fieldVisibility,
Object externalValue,
GraphQLInputType type,
GraphQLContext graphqlContext,
Locale locale) {
return externalValueToInternalValueImpl(fieldVisibility, type, externalValue, graphqlContext, locale);
public static Object externalValueToInternalValue(
GraphqlFieldVisibility fieldVisibility,
Object externalValue,
GraphQLInputType type,
GraphQLContext graphqlContext,
Locale locale
) {
InputInterceptor inputInterceptor = graphqlContext.get(InputInterceptor.class);
return externalValueToInternalValueImpl(
inputInterceptor,
fieldVisibility,
type,
externalValue,
graphqlContext,
locale);
}


@Nullable
@SuppressWarnings("unchecked")
public static <T> T getInputValueImpl(GraphQLInputType inputType,
InputValueWithState inputValue,
GraphQLContext graphqlContext,
Locale locale) {
public static <T> T getInputValueImpl(
GraphQLInputType inputType,
InputValueWithState inputValue,
GraphQLContext graphqlContext,
Locale locale
) {
if (inputValue.isNotSet()) {
return null;
}
return (T) valueToInternalValue(inputValue, inputType, graphqlContext, locale);
return (T) valueToInternalValue(
inputValue,
inputType,
graphqlContext,
locale);
}


private static Map<String, Object> getArgumentValuesImpl(GraphqlFieldVisibility fieldVisibility,
List<GraphQLArgument> argumentTypes,
List<Argument> arguments,
CoercedVariables coercedVariables,
GraphQLContext graphqlContext,
Locale locale) {
private static Map<String, Object> getArgumentValuesImpl(
InputInterceptor inputInterceptor,
GraphqlFieldVisibility fieldVisibility,
List<GraphQLArgument> argumentTypes,
List<Argument> arguments,
CoercedVariables coercedVariables,
GraphQLContext graphqlContext,
Locale locale
) {
if (argumentTypes.isEmpty()) {
return ImmutableKit.emptyMap();
}
Expand All @@ -289,10 +349,12 @@ private static Map<String, Object> getArgumentValuesImpl(GraphqlFieldVisibility
}
if (!hasValue && argumentDefinition.hasSetDefaultValue()) {
Object coercedDefaultValue = ValuesResolverConversion.defaultValueToInternalValue(
inputInterceptor,
fieldVisibility,
defaultValue,
argumentType,
graphqlContext, locale);
graphqlContext,
locale);
coercedValues.put(argumentName, coercedDefaultValue);
} else if (isNonNull(argumentType) && (!hasValue || ValuesResolverConversion.isNullValue(value))) {
throw new NonNullableValueCoercedAsNullException(argumentDefinition);
Expand All @@ -302,7 +364,13 @@ private static Map<String, Object> getArgumentValuesImpl(GraphqlFieldVisibility
} else if (argumentValue instanceof VariableReference) {
coercedValues.put(argumentName, value);
} else {
value = ValuesResolverConversion.literalToInternalValue(fieldVisibility, argumentType, argument.getValue(), coercedVariables, graphqlContext, locale);
value = ValuesResolverConversion.literalToInternalValue(inputInterceptor,
fieldVisibility,
argumentType,
argument.getValue(),
coercedVariables,
graphqlContext,
locale);
coercedValues.put(argumentName, value);
}
}
Expand Down Expand Up @@ -337,16 +405,28 @@ public static Object literalToNormalizedValue(GraphqlFieldVisibility fieldVisibi
return inputValue;
}
if (isNonNull(type)) {
return literalToNormalizedValue(fieldVisibility, unwrapOne(type), inputValue, normalizedVariables);
return literalToNormalizedValue(
fieldVisibility,
unwrapOne(type),
inputValue,
normalizedVariables);
}
if (type instanceof GraphQLInputObjectType) {
return literalToNormalizedValueForInputObject(fieldVisibility, (GraphQLInputObjectType) type, (ObjectValue) inputValue, normalizedVariables);
return literalToNormalizedValueForInputObject(
fieldVisibility,
(GraphQLInputObjectType) type,
(ObjectValue) inputValue,
normalizedVariables);
}
if (type instanceof GraphQLEnumType) {
return inputValue;
}
if (isList(type)) {
return literalToNormalizedValueForList(fieldVisibility, (GraphQLList) type, inputValue, normalizedVariables);
return literalToNormalizedValueForList(
fieldVisibility,
(GraphQLList) type,
inputValue,
normalizedVariables);
}
return null;
}
Expand All @@ -364,7 +444,11 @@ private static Object literalToNormalizedValueForInputObject(GraphqlFieldVisibil
}

GraphQLInputType fieldType = type.getField(field.getName()).getType();
Object fieldValue = literalToNormalizedValue(fieldVisibility, fieldType, field.getValue(), normalizedVariables);
Object fieldValue = literalToNormalizedValue(
fieldVisibility,
fieldType,
field.getValue(),
normalizedVariables);
result.put(field.getName(), new NormalizedInputValue(simplePrint(fieldType), fieldValue));
}
return result;
Expand All @@ -377,11 +461,20 @@ private static List<Object> literalToNormalizedValueForList(GraphqlFieldVisibili
if (value instanceof ArrayValue) {
List<Object> result = new ArrayList<>();
for (Value valueInArray : ((ArrayValue) value).getValues()) {
result.add(literalToNormalizedValue(fieldVisibility, type.getWrappedType(), valueInArray, normalizedVariables));
Object normalisedValue = literalToNormalizedValue(
fieldVisibility,
type.getWrappedType(),
valueInArray,
normalizedVariables);
result.add(normalisedValue);
}
return result;
} else {
return Collections.singletonList(literalToNormalizedValue(fieldVisibility, type.getWrappedType(), value, normalizedVariables));
return Collections.singletonList(literalToNormalizedValue(
fieldVisibility,
type.getWrappedType(),
value,
normalizedVariables));
}
}

Expand Down