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

Add back the deleted public methods as deprecated #165

Merged
merged 2 commits into from
Apr 30, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,20 @@ public List<Condition> conditions() {
return conditions;
}

/**
* @deprecated Since 2.8.1. This method will be removed in the next minor version.
* Please use {@link com.apollographql.apollo.internal.cache.normalized.CacheKeyBuilder} instead.
*
* @param variables
* @return
*/
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 @@ -276,12 +290,61 @@ public List<Condition> conditions() {
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();
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();
boolean isArgumentVariable = isArgumentValueVariableType(objectArg);
independentKey
.append(argument.getKey())
.append(":")
.append(isArgumentVariable ? "" : "[")
.append(orderIndependentKey(objectArg, variables))
.append(isArgumentVariable ? "" : "]");
} else {
independentKey.append(argument.getKey())
.append(":")
.append(argument.getValue().toString());
}
if (i < sortedArguments.size() - 1) {
independentKey.append(",");
}
}
return independentKey.toString();
}

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 resolvedVariable.toString();
}
}

/**
* An abstraction for the field types
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,24 @@ public final class CacheFieldValueResolver implements FieldValueResolver<Record>
private final CacheHeaders cacheHeaders;
private final CacheKeyBuilder cacheKeyBuilder;

/**
* @deprecated Since 2.8.1. This method will be removed in the next minor version.
* Please use {@link CacheFieldValueResolver(ReadableStore, Operation.Variables, CacheKeyResolver, CacheHeaders, CacheKeyBuilder)}
*
* @param readableCache
* @param variables
* @param cacheKeyResolver
* @param cacheHeaders
*/
public CacheFieldValueResolver(ReadableStore readableCache, Operation.Variables variables,
CacheKeyResolver cacheKeyResolver, CacheHeaders cacheHeaders) {
this.readableCache = readableCache;
this.variables = variables;
this.cacheKeyResolver = cacheKeyResolver;
this.cacheHeaders = cacheHeaders;
this.cacheKeyBuilder = null;
}

public CacheFieldValueResolver(ReadableStore readableCache, Operation.Variables variables,
CacheKeyResolver cacheKeyResolver, CacheHeaders cacheHeaders, CacheKeyBuilder cacheKeyBuilder) {
this.readableCache = readableCache;
Expand Down