Search in sources :

Example 1 with ScalarTypeAdapters

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

the class IntegrationTest method operationJsonWriter.

@Test
public void operationJsonWriter() throws Exception {
    String json = Utils.readFileToString(getClass(), "/OperationJsonWriter.json");
    AllPlanetsQuery query = new AllPlanetsQuery();
    Response<AllPlanetsQuery.Data> response = new OperationResponseParser<>(query, query.responseFieldMapper(), new ScalarTypeAdapters(Collections.EMPTY_MAP)).parse(new Buffer().writeUtf8(json));
    Buffer buffer = new Buffer();
    OperationJsonWriter writer = new OperationJsonWriter(response.data(), new ScalarTypeAdapters(Collections.EMPTY_MAP));
    JsonWriter jsonWriter = JsonWriter.of(buffer);
    jsonWriter.setIndent("  ");
    writer.write(jsonWriter);
    assertThat(buffer.readUtf8()).isEqualTo(json);
}
Also used : Buffer(okio.Buffer) OperationJsonWriter(com.apollographql.apollo.response.OperationJsonWriter) ScalarTypeAdapters(com.apollographql.apollo.response.ScalarTypeAdapters) AllPlanetsQuery(com.apollographql.apollo.integration.httpcache.AllPlanetsQuery) OperationJsonWriter(com.apollographql.apollo.response.OperationJsonWriter) JsonWriter(com.apollographql.apollo.internal.json.JsonWriter) Test(org.junit.Test)

Example 2 with ScalarTypeAdapters

use of com.apollographql.apollo.response.ScalarTypeAdapters 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);
}
Also used : ApolloLogger(com.apollographql.apollo.internal.ApolloLogger) ScalarTypeAdapters(com.apollographql.apollo.response.ScalarTypeAdapters) CustomTypeAdapter(com.apollographql.apollo.response.CustomTypeAdapter) Request(okhttp3.Request) ScalarType(com.apollographql.apollo.api.ScalarType) ApolloLogger(com.apollographql.apollo.internal.ApolloLogger) Logger(com.apollographql.apollo.Logger) Nullable(javax.annotation.Nullable) Predicate(com.google.common.base.Predicate) Test(org.junit.Test)

Example 3 with ScalarTypeAdapters

use of com.apollographql.apollo.response.ScalarTypeAdapters 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 4 with ScalarTypeAdapters

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

the class IntegrationTest method operationResponseParser.

@Test
public void operationResponseParser() throws Exception {
    String json = Utils.readFileToString(getClass(), "/HeroNameResponse.json");
    HeroNameQuery query = new HeroNameQuery();
    Response<HeroNameQuery.Data> response = new OperationResponseParser<>(query, query.responseFieldMapper(), new ScalarTypeAdapters(Collections.EMPTY_MAP)).parse(new Buffer().writeUtf8(json));
    assertThat(response.data().hero().name()).isEqualTo("R2-D2");
}
Also used : Buffer(okio.Buffer) ScalarTypeAdapters(com.apollographql.apollo.response.ScalarTypeAdapters) EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) HeroNameQuery(com.apollographql.apollo.integration.normalizer.HeroNameQuery) Test(org.junit.Test)

Example 5 with ScalarTypeAdapters

use of com.apollographql.apollo.response.ScalarTypeAdapters 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);
}
Also used : ApolloLogger(com.apollographql.apollo.internal.ApolloLogger) ScalarTypeAdapters(com.apollographql.apollo.response.ScalarTypeAdapters) CustomTypeAdapter(com.apollographql.apollo.response.CustomTypeAdapter) Request(okhttp3.Request) ScalarType(com.apollographql.apollo.api.ScalarType) ApolloLogger(com.apollographql.apollo.internal.ApolloLogger) Logger(com.apollographql.apollo.Logger) Nullable(javax.annotation.Nullable) Predicate(com.google.common.base.Predicate) Test(org.junit.Test)

Aggregations

ScalarTypeAdapters (com.apollographql.apollo.response.ScalarTypeAdapters)6 Test (org.junit.Test)5 Logger (com.apollographql.apollo.Logger)3 ScalarType (com.apollographql.apollo.api.ScalarType)3 ApolloLogger (com.apollographql.apollo.internal.ApolloLogger)3 CustomTypeAdapter (com.apollographql.apollo.response.CustomTypeAdapter)3 Nullable (javax.annotation.Nullable)3 Predicate (com.google.common.base.Predicate)2 Request (okhttp3.Request)2 Buffer (okio.Buffer)2 NamedCountDownLatch (com.apollographql.apollo.NamedCountDownLatch)1 CacheHeaders (com.apollographql.apollo.cache.CacheHeaders)1 CacheKey (com.apollographql.apollo.cache.normalized.CacheKey)1 NormalizedCache (com.apollographql.apollo.cache.normalized.NormalizedCache)1 Record (com.apollographql.apollo.cache.normalized.Record)1 AllPlanetsQuery (com.apollographql.apollo.integration.httpcache.AllPlanetsQuery)1 EpisodeHeroNameQuery (com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery)1 HeroNameQuery (com.apollographql.apollo.integration.normalizer.HeroNameQuery)1 MapFieldValueResolver (com.apollographql.apollo.internal.field.MapFieldValueResolver)1 JsonWriter (com.apollographql.apollo.internal.json.JsonWriter)1