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 1 commit
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
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
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ class OperationTypeSpecBuilder(
.map { (name, type) ->
ParameterSpec.builder(type, name)
.apply {
if (type.isOptional(ClassNames.INPUT_TYPE)) {
if (type.isOptional(ClassNames.INPUT)) {
addAnnotation(Annotations.NONNULL)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ fun TypeName.isOptional(expectedOptionalType: ClassName? = null): Boolean {
val rawType = (this as? ParameterizedTypeName)?.rawType ?: this
return if (expectedOptionalType == null) {
rawType == ClassNames.OPTIONAL || rawType == ClassNames.GUAVA_OPTIONAL || rawType == ClassNames.JAVA_OPTIONAL
|| rawType == ClassNames.INPUT_TYPE
|| rawType == ClassNames.INPUT
} else {
rawType == expectedOptionalType
}
Expand All @@ -269,7 +269,7 @@ fun TypeName.unwrapOptionalType(withoutAnnotations: Boolean = false): TypeName {
fun TypeName.unwrapOptionalValue(varName: String, checkIfPresent: Boolean = true,
transformation: ((CodeBlock) -> CodeBlock)? = null): CodeBlock {
return if (isOptional() && this is ParameterizedTypeName) {
if (rawType == ClassNames.INPUT_TYPE) {
if (rawType == ClassNames.INPUT) {
val valueCode = CodeBlock.of("\$L.value", varName)
if (checkIfPresent) {
CodeBlock.of("\$L != null ? \$L : null", valueCode, transformation?.invoke(valueCode) ?: valueCode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.apollographql.apollo.api.Input;
import com.apollographql.apollo.api.InputFieldMarshaller;
import com.apollographql.apollo.api.InputFieldWriter;
import com.apollographql.apollo.api.InputType;
import com.apollographql.apollo.api.internal.Utils;
import java.io.IOException;
import java.lang.Double;
Expand All @@ -13,7 +14,7 @@
import org.jetbrains.annotations.Nullable;

@Generated("Apollo GraphQL")
public final class ColorInput {
public final class ColorInput implements InputType {
private final int red;

private final Input<Double> green;
Expand Down Expand Up @@ -65,6 +66,7 @@ public static Builder builder() {
return new Builder();
}

@Override
public InputFieldMarshaller marshaller() {
return new InputFieldMarshaller() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.apollographql.apollo.api.Input;
import com.apollographql.apollo.api.InputFieldMarshaller;
import com.apollographql.apollo.api.InputFieldWriter;
import com.apollographql.apollo.api.InputType;
import com.apollographql.apollo.api.internal.Utils;
import java.io.IOException;
import java.lang.Boolean;
Expand All @@ -18,7 +19,7 @@
import org.jetbrains.annotations.Nullable;

@Generated("Apollo GraphQL")
public final class ReviewInput {
public final class ReviewInput implements InputType {
private final int stars;

private final Input<Integer> nullableIntFieldWithDefaultValue;
Expand Down Expand Up @@ -197,6 +198,7 @@ public static Builder builder() {
return new Builder();
}

@Override
public InputFieldMarshaller marshaller() {
return new InputFieldMarshaller() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.apollographql.apollo.api.Input;
import com.apollographql.apollo.api.InputFieldMarshaller;
import com.apollographql.apollo.api.InputFieldWriter;
import com.apollographql.apollo.api.InputType;
import com.apollographql.apollo.api.internal.Utils;
import java.io.IOException;
import java.lang.Double;
Expand All @@ -13,7 +14,7 @@
import org.jetbrains.annotations.Nullable;

@Generated("Apollo GraphQL")
public final class ColorInput {
public final class ColorInput implements InputType {
private final int red;

private final Input<Double> green;
Expand Down Expand Up @@ -65,6 +66,7 @@ public static Builder builder() {
return new Builder();
}

@Override
public InputFieldMarshaller marshaller() {
return new InputFieldMarshaller() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.apollographql.apollo.api.Input;
import com.apollographql.apollo.api.InputFieldMarshaller;
import com.apollographql.apollo.api.InputFieldWriter;
import com.apollographql.apollo.api.InputType;
import com.apollographql.apollo.api.internal.Utils;
import java.io.IOException;
import java.lang.Boolean;
Expand All @@ -18,7 +19,7 @@
import org.jetbrains.annotations.Nullable;

@Generated("Apollo GraphQL")
public final class ReviewInput {
public final class ReviewInput implements InputType {
private final int stars;

private final Input<Integer> nullableIntFieldWithDefaultValue;
Expand Down Expand Up @@ -197,6 +198,7 @@ public static Builder builder() {
return new Builder();
}

@Override
public InputFieldMarshaller marshaller() {
return new InputFieldMarshaller() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.apollographql.apollo.api.Input;
import com.apollographql.apollo.api.InputFieldMarshaller;
import com.apollographql.apollo.api.InputFieldWriter;
import com.apollographql.apollo.api.InputType;
import com.apollographql.apollo.api.internal.Utils;
import java.io.IOException;
import java.lang.Double;
Expand All @@ -13,7 +14,7 @@
import org.jetbrains.annotations.Nullable;

@Generated("Apollo GraphQL")
public final class ColorInput {
public final class ColorInput implements InputType {
private final int red;

private final Input<Double> green;
Expand Down Expand Up @@ -65,6 +66,7 @@ public static Builder builder() {
return new Builder();
}

@Override
public InputFieldMarshaller marshaller() {
return new InputFieldMarshaller() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.apollographql.apollo.api.Input;
import com.apollographql.apollo.api.InputFieldMarshaller;
import com.apollographql.apollo.api.InputFieldWriter;
import com.apollographql.apollo.api.InputType;
import com.apollographql.apollo.api.internal.Utils;
import java.io.IOException;
import java.lang.Boolean;
Expand All @@ -17,7 +18,7 @@
import org.jetbrains.annotations.Nullable;

@Generated("Apollo GraphQL")
public final class ReviewInput {
public final class ReviewInput implements InputType {
private final int stars;

private final Input<Integer> nullableIntFieldWithDefaultValue;
Expand Down Expand Up @@ -196,6 +197,7 @@ public static Builder builder() {
return new Builder();
}

@Override
public InputFieldMarshaller marshaller() {
return new InputFieldMarshaller() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.apollographql.apollo.api.Input;
import com.apollographql.apollo.api.InputFieldMarshaller;
import com.apollographql.apollo.api.InputFieldWriter;
import com.apollographql.apollo.api.InputType;
import com.apollographql.apollo.api.internal.Utils;
import java.io.IOException;
import java.lang.Boolean;
Expand All @@ -13,7 +14,7 @@
import org.jetbrains.annotations.Nullable;

@Generated("Apollo GraphQL")
public final class TestInputType {
public final class TestInputType implements InputType {
private final Input<Boolean> private_;

private volatile int $hashCode;
Expand All @@ -32,6 +33,7 @@ public static Builder builder() {
return new Builder();
}

@Override
public InputFieldMarshaller marshaller() {
return new InputFieldMarshaller() {
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.apollographql.apollo.internal.cache.normalized;

import com.apollographql.apollo.api.Operation;
import com.apollographql.apollo.api.ResponseField;

import org.jetbrains.annotations.NotNull;

public interface CacheKeyBuilder {
@NotNull String build(@NotNull ResponseField field, @NotNull Operation.Variables variables);
}
Loading