-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
import graphql.GraphQLContext; | ||
import graphql.Internal; | ||
import graphql.collect.ImmutableKit; | ||
import graphql.execution.values.InputInterceptor; | ||
import graphql.language.Argument; | ||
import graphql.language.ArrayValue; | ||
import graphql.language.NullValue; | ||
|
@@ -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); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See multi line parameters with extra |
||
|
||
|
||
|
@@ -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) { | ||
|
@@ -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); | ||
} | ||
|
||
/** | ||
|
@@ -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(); | ||
} | ||
|
@@ -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); | ||
} | ||
|
||
/** | ||
|
@@ -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); | ||
} | ||
|
||
/** | ||
|
@@ -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(); | ||
} | ||
|
@@ -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); | ||
|
@@ -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); | ||
} | ||
} | ||
|
@@ -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; | ||
} | ||
|
@@ -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; | ||
|
@@ -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)); | ||
} | ||
} | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 dependencyI 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 placeRather 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.