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

Fix issue with toString as a cache key for GraphQL input types #997

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.apollographql.apollo.api;

import org.jetbrains.annotations.NotNull;

public interface InputType {
@NotNull InputFieldMarshaller marshaller();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

Expand All @@ -24,9 +23,9 @@ public class ResponseField {
private final boolean optional;
private final List<Condition> conditions;

private static final String VARIABLE_IDENTIFIER_KEY = "kind";
private static final String VARIABLE_IDENTIFIER_VALUE = "Variable";
private static final String VARIABLE_NAME_KEY = "variableName";
public static final String VARIABLE_IDENTIFIER_KEY = "kind";
public static final String VARIABLE_IDENTIFIER_VALUE = "Variable";
public static final String VARIABLE_NAME_KEY = "variableName";

/**
* Factory method for creating a Field instance representing {@link Type#STRING}.
Expand Down Expand Up @@ -233,13 +232,6 @@ public List<Condition> conditions() {
return conditions;
}

public String cacheKey(Operation.Variables variables) {
if (arguments.isEmpty()) {
return fieldName();
}
return String.format("%s(%s)", fieldName(), orderIndependentKey(arguments, variables));
}

/**
* Resolve field argument value by name. If argument represents a references to the variable, it will be resolved from
* provided operation variables values.
Expand All @@ -266,71 +258,12 @@ public String cacheKey(Operation.Variables variables) {
return argumentValue;
}

private String orderIndependentKey(Map<String, Object> objectMap, Operation.Variables variables) {
if (isArgumentValueVariableType(objectMap)) {
return orderIndependentKeyForVariableArgument(objectMap, variables);
}
List<Map.Entry<String, Object>> sortedArguments = new ArrayList<>(objectMap.entrySet());
Collections.sort(sortedArguments, new Comparator<Map.Entry<String, Object>>() {
@Override public int compare(Map.Entry<String, Object> argumentOne, Map.Entry<String, Object> argumentTwo) {
return argumentOne.getKey().compareTo(argumentTwo.getKey());
}
});
StringBuilder independentKey = new StringBuilder();
independentKey.append("{");
for (int i = 0; i < sortedArguments.size(); i++) {
Map.Entry<String, Object> argument = sortedArguments.get(i);
if (argument.getValue() instanceof Map) {
//noinspection unchecked
final Map<String, Object> objectArg = (Map<String, Object>) argument.getValue();
independentKey
.append("\"")
.append(argument.getKey())
.append("\":")
.append(orderIndependentKey(objectArg, variables));
} else {
independentKey
.append("\"")
.append(argument.getKey())
.append("\":")
.append(asJsonValue(argument.getValue()));
}
if (i < sortedArguments.size() - 1) {
independentKey.append(",");
}
}
independentKey.append("}");
return independentKey.toString();
}

private String asJsonValue(Object o) {
if (o instanceof Boolean || o instanceof Number) {
return o.toString();
} else {
return "\"" + o.toString() + "\"";
}
}

private boolean isArgumentValueVariableType(Map<String, Object> objectMap) {
public static boolean isArgumentValueVariableType(Map<String, Object> objectMap) {
return objectMap.containsKey(VARIABLE_IDENTIFIER_KEY)
&& objectMap.get(VARIABLE_IDENTIFIER_KEY).equals(VARIABLE_IDENTIFIER_VALUE)
&& objectMap.containsKey(VARIABLE_NAME_KEY);
}

private String orderIndependentKeyForVariableArgument(Map<String, Object> objectMap, Operation.Variables variables) {
Object variable = objectMap.get(VARIABLE_NAME_KEY);
//noinspection SuspiciousMethodCalls
Object resolvedVariable = variables.valueMap().get(variable);
if (resolvedVariable == null) {
return null;
} else if (resolvedVariable instanceof Map) {
//noinspection unchecked
return orderIndependentKey((Map<String, Object>) resolvedVariable, variables);
} else {
return asJsonValue(resolvedVariable);
}
}

/**
* An abstraction for the field types
*/
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class BuilderTypeSpecBuilder(
}

private fun inputFieldSetterMethodSpecs(): List<MethodSpec> {
return fields.filter { (_, fieldType) -> fieldType.isOptional(ClassNames.INPUT_TYPE) }
return fields.filter { (_, fieldType) -> fieldType.isOptional(ClassNames.INPUT) }
.map { (fieldName, fieldType) ->
val javaDoc = fieldJavaDocs[fieldName]
inputFieldSetterMethodSpec(fieldName, fieldType, javaDoc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ object ClassNames {
val JAVA_OPTIONAL: ClassName = ClassName.get("java.util", "Optional")
val API_UTILS: ClassName = ClassName.get(Utils::class.java)
val FRAGMENT: ClassName = ClassName.get(GraphqlFragment::class.java)
val INPUT_TYPE: ClassName = ClassName.get(Input::class.java)
val INPUT: ClassName = ClassName.get(Input::class.java)
val BUILDER: ClassName = ClassName.get("", "Builder")
val MUTATOR: ClassName = ClassName.get(Mutator::class.java)
val INPUT_TYPE: ClassName = ClassName.get(InputType::class.java)

fun <K : Any> parameterizedListOf(type: Class<K>): TypeName =
ParameterizedTypeName.get(LIST, ClassName.get(type))
Expand Down Expand Up @@ -65,6 +66,6 @@ object ClassNames {
ParameterizedTypeName.get(JAVA_OPTIONAL, type)

fun parameterizedInputType(type: TypeName): TypeName =
ParameterizedTypeName.get(INPUT_TYPE, type)
ParameterizedTypeName.get(INPUT, type)

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class InputTypeSpecBuilder(
TypeSpec.classBuilder(objectClassName)
.addAnnotation(Annotations.GENERATED_BY_APOLLO)
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.addSuperinterface(ClassNames.INPUT_TYPE)
.addConstructor()
.addFields()
.addBuilder()
Expand Down Expand Up @@ -99,6 +100,7 @@ class InputTypeSpecBuilder(
.addMethod(methodSpec)
.build()
return MethodSpec.methodBuilder(MARSHALLER_PARAM_NAME)
.addAnnotation(Annotations.OVERRIDE)
.addModifiers(Modifier.PUBLIC)
.returns(InputFieldMarshaller::class.java)
.addStatement("return \$L", marshallerType)
Expand Down
Loading