Search in sources :

Example 1 with CustomTypeValue

use of com.apollographql.apollo.response.CustomTypeValue in project apollo-android by apollographql.

the class IntegrationTest method setUp.

@Before
public void setUp() {
    dateCustomTypeAdapter = new CustomTypeAdapter<Date>() {

        @Override
        public Date decode(CustomTypeValue value) {
            try {
                return DATE_FORMAT.parse(value.value.toString());
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public CustomTypeValue encode(Date value) {
            return new CustomTypeValue.GraphQLString(DATE_FORMAT.format(value));
        }
    };
    apolloClient = ApolloClient.builder().serverUrl(server.url("/")).okHttpClient(new OkHttpClient.Builder().dispatcher(new Dispatcher(Utils.immediateExecutorService())).build()).addCustomTypeAdapter(CustomType.DATE, dateCustomTypeAdapter).normalizedCache(new LruNormalizedCacheFactory(EvictionPolicy.NO_EVICTION), new IdFieldCacheKeyResolver()).defaultResponseFetcher(ApolloResponseFetchers.NETWORK_ONLY).dispatcher(Utils.immediateExecutor()).build();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) LruNormalizedCacheFactory(com.apollographql.apollo.cache.normalized.lru.LruNormalizedCacheFactory) CustomTypeValue(com.apollographql.apollo.response.CustomTypeValue) ParseException(java.text.ParseException) Dispatcher(okhttp3.Dispatcher) Date(java.util.Date) Before(org.junit.Before)

Example 2 with CustomTypeValue

use of com.apollographql.apollo.response.CustomTypeValue in project apollo-android by apollographql.

the class ResponseWriteTestCase method setUp.

@Before
public void setUp() {
    OkHttpClient okHttpClient = new OkHttpClient.Builder().dispatcher(new Dispatcher(Utils.immediateExecutorService())).build();
    apolloClient = ApolloClient.builder().serverUrl(server.url("/")).okHttpClient(okHttpClient).normalizedCache(new LruNormalizedCacheFactory(EvictionPolicy.NO_EVICTION), new IdFieldCacheKeyResolver()).dispatcher(Utils.immediateExecutor()).addCustomTypeAdapter(CustomType.DATE, new CustomTypeAdapter<Date>() {

        @Override
        public Date decode(CustomTypeValue value) {
            try {
                return DATE_TIME_FORMAT.parse(value.value.toString());
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public CustomTypeValue encode(Date value) {
            return new CustomTypeValue.GraphQLString(DATE_TIME_FORMAT.format(value));
        }
    }).build();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) LruNormalizedCacheFactory(com.apollographql.apollo.cache.normalized.lru.LruNormalizedCacheFactory) CustomTypeValue(com.apollographql.apollo.response.CustomTypeValue) ParseException(java.text.ParseException) Dispatcher(okhttp3.Dispatcher) Date(java.util.Date) Before(org.junit.Before)

Example 3 with CustomTypeValue

use of com.apollographql.apollo.response.CustomTypeValue in project apollo-android by apollographql.

the class InputFieldJsonWriter method writeCustom.

@SuppressWarnings("unchecked")
@Override
public void writeCustom(@Nonnull String fieldName, ScalarType scalarType, Object value) throws IOException {
    checkNotNull(fieldName, "fieldName == null");
    if (value != null) {
        CustomTypeAdapter customTypeAdapter = scalarTypeAdapters.adapterFor(scalarType);
        CustomTypeValue customTypeValue = customTypeAdapter.encode(value);
        if (customTypeValue instanceof CustomTypeValue.GraphQLString) {
            writeString(fieldName, ((CustomTypeValue.GraphQLString) customTypeValue).value);
        } else if (customTypeValue instanceof CustomTypeValue.GraphQLBoolean) {
            writeBoolean(fieldName, ((CustomTypeValue.GraphQLBoolean) customTypeValue).value);
        } else if (customTypeValue instanceof CustomTypeValue.GraphQLNumber) {
            writeNumber(fieldName, ((CustomTypeValue.GraphQLNumber) customTypeValue).value);
        } else if (customTypeValue instanceof CustomTypeValue.GraphQLJsonString) {
            writeString(fieldName, ((CustomTypeValue.GraphQLJsonString) customTypeValue).value);
        } else {
            throw new IllegalArgumentException("Unsupported custom value type: " + customTypeValue);
        }
    } else {
        writeString(fieldName, null);
    }
}
Also used : CustomTypeAdapter(com.apollographql.apollo.response.CustomTypeAdapter) CustomTypeValue(com.apollographql.apollo.response.CustomTypeValue)

Example 4 with CustomTypeValue

use of com.apollographql.apollo.response.CustomTypeValue in project apollo-android by apollographql.

the class ResponseReaderTest method responseReader.

@SuppressWarnings("unchecked")
private static RealResponseReader<Map<String, Object>> responseReader(Map<String, Object> recordSet) {
    Map<ScalarType, CustomTypeAdapter> customTypeAdapters = new HashMap<>();
    customTypeAdapters.put(DATE_CUSTOM_TYPE, new CustomTypeAdapter() {

        @Override
        public Object decode(CustomTypeValue value) {
            try {
                return DATE_TIME_FORMAT.parse(value.value.toString());
            } catch (ParseException e) {
                throw new ClassCastException();
            }
        }

        @Override
        public CustomTypeValue encode(Object value) {
            return null;
        }
    });
    customTypeAdapters.put(URL_CUSTOM_TYPE, new CustomTypeAdapter() {

        @Override
        public Object decode(CustomTypeValue value) {
            return null;
        }

        @Override
        public CustomTypeValue encode(Object value) {
            return null;
        }
    });
    customTypeAdapters.put(OBJECT_CUSTOM_TYPE, new CustomTypeAdapter() {

        @Override
        public Object decode(CustomTypeValue value) {
            return value.value.toString();
        }

        @Override
        public CustomTypeValue encode(Object value) {
            return null;
        }
    });
    return new RealResponseReader<>(EMPTY_OPERATION.variables(), recordSet, new MapFieldValueResolver(), new ScalarTypeAdapters(customTypeAdapters), NO_OP_NORMALIZER);
}
Also used : RealResponseReader(com.apollographql.apollo.internal.response.RealResponseReader) ScalarTypeAdapters(com.apollographql.apollo.response.ScalarTypeAdapters) CustomTypeAdapter(com.apollographql.apollo.response.CustomTypeAdapter) HashMap(java.util.HashMap) CustomTypeValue(com.apollographql.apollo.response.CustomTypeValue) ScalarType(com.apollographql.apollo.api.ScalarType) MapFieldValueResolver(com.apollographql.apollo.internal.field.MapFieldValueResolver) ParseException(java.text.ParseException)

Example 5 with CustomTypeValue

use of com.apollographql.apollo.response.CustomTypeValue in project apollo-android by apollographql.

the class HttpCacheTest method setUp.

@Before
public void setUp() {
    CustomTypeAdapter<Date> dateCustomTypeAdapter = new CustomTypeAdapter<Date>() {

        @Override
        public Date decode(CustomTypeValue value) {
            try {
                return DATE_FORMAT.parse(value.value.toString());
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public CustomTypeValue encode(Date value) {
            return new CustomTypeValue.GraphQLString(DATE_FORMAT.format(value));
        }
    };
    cacheStore = new MockHttpCacheStore();
    cacheStore.delegate = new DiskLruHttpCacheStore(inMemoryFileSystem, new File("/cache/"), Integer.MAX_VALUE);
    HttpCache cache = new ApolloHttpCache(cacheStore, null);
    okHttpClient = new OkHttpClient.Builder().addInterceptor(new TrackingInterceptor()).addInterceptor(cache.interceptor()).dispatcher(new Dispatcher(Utils.immediateExecutorService())).readTimeout(2, TimeUnit.SECONDS).writeTimeout(2, TimeUnit.SECONDS).build();
    apolloClient = ApolloClient.builder().serverUrl(server.url("/")).okHttpClient(okHttpClient).dispatcher(Utils.immediateExecutor()).addCustomTypeAdapter(CustomType.DATE, dateCustomTypeAdapter).httpCache(cache).build();
}
Also used : ApolloHttpCache(com.apollographql.apollo.cache.http.ApolloHttpCache) OkHttpClient(okhttp3.OkHttpClient) ApolloHttpCache(com.apollographql.apollo.cache.http.ApolloHttpCache) HttpCache(com.apollographql.apollo.api.cache.http.HttpCache) Dispatcher(okhttp3.Dispatcher) Date(java.util.Date) CustomTypeAdapter(com.apollographql.apollo.response.CustomTypeAdapter) CustomTypeValue(com.apollographql.apollo.response.CustomTypeValue) DiskLruHttpCacheStore(com.apollographql.apollo.cache.http.DiskLruHttpCacheStore) ParseException(java.text.ParseException) File(java.io.File) Before(org.junit.Before)

Aggregations

CustomTypeValue (com.apollographql.apollo.response.CustomTypeValue)5 ParseException (java.text.ParseException)4 CustomTypeAdapter (com.apollographql.apollo.response.CustomTypeAdapter)3 Date (java.util.Date)3 Dispatcher (okhttp3.Dispatcher)3 OkHttpClient (okhttp3.OkHttpClient)3 Before (org.junit.Before)3 LruNormalizedCacheFactory (com.apollographql.apollo.cache.normalized.lru.LruNormalizedCacheFactory)2 ScalarType (com.apollographql.apollo.api.ScalarType)1 HttpCache (com.apollographql.apollo.api.cache.http.HttpCache)1 ApolloHttpCache (com.apollographql.apollo.cache.http.ApolloHttpCache)1 DiskLruHttpCacheStore (com.apollographql.apollo.cache.http.DiskLruHttpCacheStore)1 MapFieldValueResolver (com.apollographql.apollo.internal.field.MapFieldValueResolver)1 RealResponseReader (com.apollographql.apollo.internal.response.RealResponseReader)1 ScalarTypeAdapters (com.apollographql.apollo.response.ScalarTypeAdapters)1 File (java.io.File)1 HashMap (java.util.HashMap)1