use of com.apollographql.apollo.response.CustomTypeAdapter in project apollo-android by apollographql.
the class ApolloServerInterceptorTest method testDefaultHttpCall.
@Test
public void testDefaultHttpCall() throws Exception {
Predicate<Request> requestAssertPredicate = new Predicate<Request>() {
@Override
public boolean apply(@Nullable Request request) {
assertThat(request).isNotNull();
assertDefaultRequestHeaders(request);
assertThat(request.header(HttpCache.CACHE_KEY_HEADER)).isNull();
assertThat(request.header(HttpCache.CACHE_FETCH_STRATEGY_HEADER)).isNull();
assertThat(request.header(HttpCache.CACHE_EXPIRE_TIMEOUT_HEADER)).isNull();
assertThat(request.header(HttpCache.CACHE_EXPIRE_AFTER_READ_HEADER)).isNull();
assertThat(request.header(HttpCache.CACHE_PREFETCH_HEADER)).isNull();
assertRequestBody(request);
return true;
}
};
ApolloServerInterceptor interceptor = new ApolloServerInterceptor(serverUrl, new AssertHttpCallFactory(requestAssertPredicate), null, false, new ScalarTypeAdapters(Collections.<ScalarType, CustomTypeAdapter>emptyMap()), new ApolloLogger(Optional.<Logger>absent()), false);
interceptor.httpCall(query);
}
use of com.apollographql.apollo.response.CustomTypeAdapter 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);
}
}
use of com.apollographql.apollo.response.CustomTypeAdapter 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);
}
use of com.apollographql.apollo.response.CustomTypeAdapter 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();
}
use of com.apollographql.apollo.response.CustomTypeAdapter in project apollo-android by apollographql.
the class ApolloServerInterceptorTest method testCachedHttpCall.
@Test
public void testCachedHttpCall() throws Exception {
Predicate<Request> requestAssertPredicate = new Predicate<Request>() {
@Override
public boolean apply(@Nullable Request request) {
assertThat(request).isNotNull();
assertDefaultRequestHeaders(request);
assertThat(request.header(HttpCache.CACHE_KEY_HEADER)).isEqualTo("1f2f23bb27630f5795166091713e18a1");
assertThat(request.header(HttpCache.CACHE_FETCH_STRATEGY_HEADER)).isEqualTo("NETWORK_FIRST");
assertThat(request.header(HttpCache.CACHE_EXPIRE_TIMEOUT_HEADER)).isEqualTo("10000");
assertThat(request.header(HttpCache.CACHE_EXPIRE_AFTER_READ_HEADER)).isEqualTo("false");
assertThat(request.header(HttpCache.CACHE_PREFETCH_HEADER)).isEqualTo("false");
assertRequestBody(request);
return true;
}
};
ApolloServerInterceptor interceptor = new ApolloServerInterceptor(serverUrl, new AssertHttpCallFactory(requestAssertPredicate), HttpCachePolicy.NETWORK_FIRST.expireAfter(10, TimeUnit.SECONDS), false, new ScalarTypeAdapters(Collections.<ScalarType, CustomTypeAdapter>emptyMap()), new ApolloLogger(Optional.<Logger>absent()), false);
interceptor.httpCall(query);
}
Aggregations