Search in sources :

Example 16 with ResponseField

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();
}
Also used : SqlNormalizedCacheFactory(com.apollographql.apollo.cache.normalized.sql.SqlNormalizedCacheFactory) OkHttpClient(okhttp3.OkHttpClient) ApolloSqlHelper(com.apollographql.apollo.cache.normalized.sql.ApolloSqlHelper) Nonnull(javax.annotation.Nonnull) WebSocketSubscriptionTransport(com.apollographql.apollo.subscription.WebSocketSubscriptionTransport) ResponseField(com.apollographql.apollo.api.ResponseField) LruNormalizedCacheFactory(com.apollographql.apollo.cache.normalized.lru.LruNormalizedCacheFactory) SqlNormalizedCacheFactory(com.apollographql.apollo.cache.normalized.sql.SqlNormalizedCacheFactory) NormalizedCacheFactory(com.apollographql.apollo.cache.normalized.NormalizedCacheFactory) LruNormalizedCacheFactory(com.apollographql.apollo.cache.normalized.lru.LruNormalizedCacheFactory) CacheKeyResolver(com.apollographql.apollo.cache.normalized.CacheKeyResolver) Map(java.util.Map)

Example 17 with ResponseField

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}})");
}
Also used : ResponseField(com.apollographql.apollo.api.ResponseField) HashMap(java.util.HashMap) Operation(com.apollographql.apollo.api.Operation) UnmodifiableMapBuilder(com.apollographql.apollo.api.internal.UnmodifiableMapBuilder) Test(org.junit.Test)

Example 18 with ResponseField

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\"})");
}
Also used : ResponseField(com.apollographql.apollo.api.ResponseField) Operation(com.apollographql.apollo.api.Operation) UnmodifiableMapBuilder(com.apollographql.apollo.api.internal.UnmodifiableMapBuilder) Test(org.junit.Test)

Example 19 with ResponseField

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\"})");
}
Also used : ResponseField(com.apollographql.apollo.api.ResponseField) HashMap(java.util.HashMap) Operation(com.apollographql.apollo.api.Operation) UnmodifiableMapBuilder(com.apollographql.apollo.api.internal.UnmodifiableMapBuilder) Test(org.junit.Test)

Example 20 with ResponseField

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}})");
}
Also used : ResponseField(com.apollographql.apollo.api.ResponseField) Operation(com.apollographql.apollo.api.Operation) Test(org.junit.Test)

Aggregations

ResponseField (com.apollographql.apollo.api.ResponseField)34 Test (org.junit.Test)33 HashMap (java.util.HashMap)25 Map (java.util.Map)23 ResponseReader (com.apollographql.apollo.api.ResponseReader)13 RealResponseReader (com.apollographql.apollo.internal.response.RealResponseReader)13 Operation (com.apollographql.apollo.api.Operation)11 UnmodifiableMapBuilder (com.apollographql.apollo.api.internal.UnmodifiableMapBuilder)6 Arrays.asList (java.util.Arrays.asList)3 List (java.util.List)3 CacheKeyResolver (com.apollographql.apollo.cache.normalized.CacheKeyResolver)1 NormalizedCacheFactory (com.apollographql.apollo.cache.normalized.NormalizedCacheFactory)1 LruNormalizedCacheFactory (com.apollographql.apollo.cache.normalized.lru.LruNormalizedCacheFactory)1 ApolloSqlHelper (com.apollographql.apollo.cache.normalized.sql.ApolloSqlHelper)1 SqlNormalizedCacheFactory (com.apollographql.apollo.cache.normalized.sql.SqlNormalizedCacheFactory)1 WebSocketSubscriptionTransport (com.apollographql.apollo.subscription.WebSocketSubscriptionTransport)1 Nonnull (javax.annotation.Nonnull)1 OkHttpClient (okhttp3.OkHttpClient)1