use of com.apollographql.apollo.api.ResponseField in project apollo-android by apollographql.
the class GitHuntApplication method onCreate.
@Override
public void onCreate() {
super.onCreate();
OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
ApolloSqlHelper apolloSqlHelper = new ApolloSqlHelper(this, SQL_CACHE_NAME);
NormalizedCacheFactory normalizedCacheFactory = new LruNormalizedCacheFactory(EvictionPolicy.NO_EVICTION).chain(new SqlNormalizedCacheFactory(apolloSqlHelper));
CacheKeyResolver cacheKeyResolver = new CacheKeyResolver() {
@Nonnull
@Override
public CacheKey fromFieldRecordSet(@Nonnull ResponseField field, @Nonnull Map<String, Object> recordSet) {
String typeName = (String) recordSet.get("__typename");
if ("User".equals(typeName)) {
String userKey = typeName + "." + recordSet.get("login");
return CacheKey.from(userKey);
}
if (recordSet.containsKey("id")) {
String typeNameAndIDKey = recordSet.get("__typename") + "." + recordSet.get("id");
return CacheKey.from(typeNameAndIDKey);
}
return CacheKey.NO_KEY;
}
// Use this resolver to customize the key for fields with variables: eg entry(repoFullName: $repoFullName).
// This is useful if you want to make query to be able to resolved, even if it has never been run before.
@Nonnull
@Override
public CacheKey fromFieldArguments(@Nonnull ResponseField field, @Nonnull Operation.Variables variables) {
return CacheKey.NO_KEY;
}
};
apolloClient = ApolloClient.builder().serverUrl(BASE_URL).okHttpClient(okHttpClient).normalizedCache(normalizedCacheFactory, cacheKeyResolver).subscriptionTransportFactory(new WebSocketSubscriptionTransport.Factory(SUBSCRIPTION_BASE_URL, okHttpClient)).build();
}
use of com.apollographql.apollo.api.ResponseField in project apollo-android by apollographql.
the class CacheKeyForFieldTest method testFieldWithNestedObjectAndVariables.
@Test
public void testFieldWithNestedObjectAndVariables() {
// noinspection unchecked
Map<String, Object> arguments = new UnmodifiableMapBuilder<String, Object>(1).put("episode", "JEDI").put("nested", new UnmodifiableMapBuilder<String, Object>(2).put("foo", new UnmodifiableMapBuilder<String, Object>(2).put("kind", "Variable").put("variableName", "stars").build()).put("bar", "2").build()).build();
ResponseField field = createResponseField("hero", "hero", arguments);
Operation.Variables variables = new Operation.Variables() {
@Nonnull
@Override
public Map<String, Object> valueMap() {
HashMap<String, Object> map = new HashMap<>();
map.put("stars", 1);
return map;
}
};
assertThat(field.cacheKey(variables)).isEqualTo("hero({\"episode\":\"JEDI\",\"nested\":{\"bar\":\"2\",\"foo\":1}})");
}
use of com.apollographql.apollo.api.ResponseField in project apollo-android by apollographql.
the class CacheKeyForFieldTest method testFieldWithArgumentAndAlias.
@Test
public void testFieldWithArgumentAndAlias() {
// noinspection unchecked
Map<String, Object> arguments = new UnmodifiableMapBuilder<String, Object>(1).put("episode", "JEDI").build();
ResponseField field = createResponseField("r2", "hero", arguments);
Operation.Variables variables = new Operation.Variables() {
@Nonnull
@Override
public Map<String, Object> valueMap() {
return super.valueMap();
}
};
assertThat(field.cacheKey(variables)).isEqualTo("hero({\"episode\":\"JEDI\"})");
}
use of com.apollographql.apollo.api.ResponseField in project apollo-android by apollographql.
the class CacheKeyForFieldTest method testFieldWithVariableArgument.
@Test
public void testFieldWithVariableArgument() {
// noinspection unchecked
UnmodifiableMapBuilder<String, Object> argument = new UnmodifiableMapBuilder<String, Object>(1).put("episode", new UnmodifiableMapBuilder<String, Object>(2).put("kind", "Variable").put("variableName", "episode").build());
ResponseField field = createResponseField("hero", "hero", argument.build());
Operation.Variables variables = new Operation.Variables() {
@Nonnull
@Override
public Map<String, Object> valueMap() {
HashMap<String, Object> map = new HashMap<>();
map.put("episode", Episode.JEDI);
return map;
}
};
assertThat(field.cacheKey(variables)).isEqualTo("hero({\"episode\":\"JEDI\"})");
}
use of com.apollographql.apollo.api.ResponseField in project apollo-android by apollographql.
the class CacheKeyForFieldTest method testFieldWithNestedObject.
@Test
public void testFieldWithNestedObject() {
// noinspection unchecked
Map<String, Object> arguments = new UnmodifiableMapBuilder<String, Object>(1).put("episode", "JEDI").put("nested", new UnmodifiableMapBuilder<String, Object>(2).put("foo", 1).put("bar", 2).build()).build();
ResponseField field = createResponseField("hero", "hero", arguments);
Operation.Variables variables = new Operation.Variables() {
@Nonnull
@Override
public Map<String, Object> valueMap() {
return super.valueMap();
}
};
assertThat(field.cacheKey(variables)).isEqualTo("hero({\"episode\":\"JEDI\",\"nested\":{\"bar\":2,\"foo\":1}})");
}
Aggregations