Search in sources :

Example 1 with MapFieldValueResolver

use of com.apollographql.apollo.internal.field.MapFieldValueResolver 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 2 with MapFieldValueResolver

use of com.apollographql.apollo.internal.field.MapFieldValueResolver 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)

Example 3 with MapFieldValueResolver

use of com.apollographql.apollo.internal.field.MapFieldValueResolver in project apollo-android by apollographql.

the class OperationResponseParser method parse.

@SuppressWarnings("unchecked")
public Response<W> parse(@Nonnull Map<String, Object> payload) {
    checkNotNull(payload, "payload == null");
    D data = null;
    if (payload.containsKey("data")) {
        Map<String, Object> buffer = (Map<String, Object>) payload.get("data");
        RealResponseReader<Map<String, Object>> realResponseReader = new RealResponseReader<>(operation.variables(), buffer, new MapFieldValueResolver(), scalarTypeAdapters, responseNormalizer);
        data = (D) responseFieldMapper.map(realResponseReader);
    }
    List<Error> errors = null;
    if (payload.containsKey("errors")) {
        List<Map<String, Object>> errorPayloads = (List<Map<String, Object>>) payload.get("errors");
        if (errorPayloads != null) {
            errors = new ArrayList<>();
            for (Map<String, Object> errorPayload : errorPayloads) {
                errors.add(readError(errorPayload));
            }
        }
    }
    return Response.<W>builder(operation).data(operation.wrapData(data)).errors(errors).dependentKeys(responseNormalizer.dependentKeys()).build();
}
Also used : MapFieldValueResolver(com.apollographql.apollo.internal.field.MapFieldValueResolver) Error(com.apollographql.apollo.api.Error) RealResponseReader(com.apollographql.apollo.internal.response.RealResponseReader) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

MapFieldValueResolver (com.apollographql.apollo.internal.field.MapFieldValueResolver)3 RealResponseReader (com.apollographql.apollo.internal.response.RealResponseReader)3 HashMap (java.util.HashMap)3 Error (com.apollographql.apollo.api.Error)2 Map (java.util.Map)2 ScalarType (com.apollographql.apollo.api.ScalarType)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 List (java.util.List)1