Search in sources :

Example 6 with RealResponseReader

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

the class ResponseReaderTest method readScalarListWithNulls.

@Test
public void readScalarListWithNulls() throws Exception {
    ResponseField scalarList = ResponseField.forList("list", "list", null, false, NO_CONDITIONS);
    final Map<String, Object> recordSet = new HashMap<>();
    recordSet.put("list", asList(null, "item1", "item2", null, "item3", null));
    RealResponseReader<Map<String, Object>> responseReader = responseReader(recordSet);
    assertThat(responseReader.readList(scalarList, new ResponseReader.ListReader() {

        @Override
        public Object read(ResponseReader.ListItemReader reader) {
            return reader.readString();
        }
    })).isEqualTo(asList("item1", "item2", "item3"));
}
Also used : ResponseField(com.apollographql.apollo.api.ResponseField) HashMap(java.util.HashMap) ResponseReader(com.apollographql.apollo.api.ResponseReader) RealResponseReader(com.apollographql.apollo.internal.response.RealResponseReader) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 7 with RealResponseReader

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

the class ResponseReaderTest method readLongList.

@Test
public void readLongList() throws Exception {
    ResponseField successField = ResponseField.forList("successFieldResponseName", "successFieldName", null, false, NO_CONDITIONS);
    ResponseField classCastExceptionField = ResponseField.forList("classCastExceptionFieldResponseName", "classCastExceptionFieldName", null, false, NO_CONDITIONS);
    final Map<String, Object> recordSet = new HashMap<>();
    recordSet.put("successFieldResponseName", asList(BigDecimal.valueOf(1), BigDecimal.valueOf(2), BigDecimal.valueOf(3)));
    recordSet.put("successFieldName", asList(BigDecimal.valueOf(4), BigDecimal.valueOf(5)));
    recordSet.put("classCastExceptionFieldResponseName", "anything");
    RealResponseReader<Map<String, Object>> responseReader = responseReader(recordSet);
    assertThat(responseReader.readList(successField, new ResponseReader.ListReader() {

        @Override
        public Object read(ResponseReader.ListItemReader reader) {
            return reader.readInt();
        }
    })).isEqualTo(asList(1, 2, 3));
    try {
        responseReader.readList(classCastExceptionField, new ResponseReader.ListReader() {

            @Override
            public Object read(ResponseReader.ListItemReader reader) {
                return null;
            }
        });
        fail("expected ClassCastException");
    } catch (ClassCastException expected) {
    // expected
    }
}
Also used : ResponseField(com.apollographql.apollo.api.ResponseField) HashMap(java.util.HashMap) ResponseReader(com.apollographql.apollo.api.ResponseReader) RealResponseReader(com.apollographql.apollo.internal.response.RealResponseReader) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 8 with RealResponseReader

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

the class ResponseReaderTest method readCustomList.

@Test
public void readCustomList() throws Exception {
    ResponseField successField = ResponseField.forList("successFieldResponseName", "successFieldName", null, false, NO_CONDITIONS);
    ResponseField classCastExceptionField = ResponseField.forList("classCastExceptionFieldResponseName", "classCastExceptionFieldName", null, false, NO_CONDITIONS);
    final Map<String, Object> recordSet = new HashMap<>();
    recordSet.put("successFieldResponseName", asList("2017-04-16", "2017-04-17", "2017-04-18"));
    recordSet.put("successFieldName", asList("2017-04-19", "2017-04-20"));
    recordSet.put("classCastExceptionFieldResponseName", "anything");
    RealResponseReader<Map<String, Object>> responseReader = responseReader(recordSet);
    assertThat(responseReader.readList(successField, new ResponseReader.ListReader() {

        @Override
        public Object read(ResponseReader.ListItemReader reader) {
            return reader.readCustomType(DATE_CUSTOM_TYPE);
        }
    })).isEqualTo(asList(DATE_TIME_FORMAT.parse("2017-04-16"), DATE_TIME_FORMAT.parse("2017-04-17"), DATE_TIME_FORMAT.parse("2017-04-18")));
    try {
        responseReader.readList(classCastExceptionField, new ResponseReader.ListReader() {

            @Override
            public Object read(ResponseReader.ListItemReader reader) {
                return null;
            }
        });
        fail("expected ClassCastException");
    } catch (ClassCastException expected) {
    // expected
    }
}
Also used : ResponseField(com.apollographql.apollo.api.ResponseField) HashMap(java.util.HashMap) ResponseReader(com.apollographql.apollo.api.ResponseReader) RealResponseReader(com.apollographql.apollo.internal.response.RealResponseReader) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 9 with RealResponseReader

use of com.apollographql.apollo.internal.response.RealResponseReader 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 10 with RealResponseReader

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

the class OperationResponseParser method parse.

public Response<W> parse(BufferedSource source) throws IOException {
    responseNormalizer.willResolveRootQuery(operation);
    BufferedSourceJsonReader jsonReader = null;
    try {
        jsonReader = new BufferedSourceJsonReader(source);
        jsonReader.beginObject();
        D data = null;
        List<Error> errors = null;
        ResponseJsonStreamReader responseStreamReader = responseJsonStreamReader(jsonReader);
        while (responseStreamReader.hasNext()) {
            String name = responseStreamReader.nextName();
            if ("data".equals(name)) {
                // noinspection unchecked
                data = (D) responseStreamReader.nextObject(true, new ResponseJsonStreamReader.ObjectReader<Object>() {

                    @Override
                    public Object read(ResponseJsonStreamReader reader) throws IOException {
                        Map<String, Object> buffer = reader.toMap();
                        RealResponseReader<Map<String, Object>> realResponseReader = new RealResponseReader<>(operation.variables(), buffer, new MapFieldValueResolver(), scalarTypeAdapters, responseNormalizer);
                        return responseFieldMapper.map(realResponseReader);
                    }
                });
            } else if ("errors".equals(name)) {
                errors = readResponseErrors(responseStreamReader);
            } else {
                responseStreamReader.skipNext();
            }
        }
        jsonReader.endObject();
        return Response.<W>builder(operation).data(operation.wrapData(data)).errors(errors).dependentKeys(responseNormalizer.dependentKeys()).build();
    } finally {
        if (jsonReader != null) {
            jsonReader.close();
        }
    }
}
Also used : Error(com.apollographql.apollo.api.Error) MapFieldValueResolver(com.apollographql.apollo.internal.field.MapFieldValueResolver) IOException(java.io.IOException) ResponseJsonStreamReader(com.apollographql.apollo.internal.json.ResponseJsonStreamReader) RealResponseReader(com.apollographql.apollo.internal.response.RealResponseReader) BufferedSourceJsonReader(com.apollographql.apollo.internal.json.BufferedSourceJsonReader) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

RealResponseReader (com.apollographql.apollo.internal.response.RealResponseReader)16 HashMap (java.util.HashMap)16 Map (java.util.Map)15 ResponseField (com.apollographql.apollo.api.ResponseField)13 ResponseReader (com.apollographql.apollo.api.ResponseReader)13 Test (org.junit.Test)13 List (java.util.List)4 MapFieldValueResolver (com.apollographql.apollo.internal.field.MapFieldValueResolver)3 Arrays.asList (java.util.Arrays.asList)3 Error (com.apollographql.apollo.api.Error)2 ScalarType (com.apollographql.apollo.api.ScalarType)1 UnmodifiableMapBuilder (com.apollographql.apollo.api.internal.UnmodifiableMapBuilder)1 BufferedSourceJsonReader (com.apollographql.apollo.internal.json.BufferedSourceJsonReader)1 ResponseJsonStreamReader (com.apollographql.apollo.internal.json.ResponseJsonStreamReader)1 CustomTypeAdapter (com.apollographql.apollo.response.CustomTypeAdapter)1 CustomTypeValue (com.apollographql.apollo.response.CustomTypeValue)1 ScalarTypeAdapters (com.apollographql.apollo.response.ScalarTypeAdapters)1 IOException (java.io.IOException)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1