Search in sources :

Example 1 with ResponseJsonStreamReader

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

use of com.apollographql.apollo.internal.json.ResponseJsonStreamReader in project apollo-android by apollographql.

the class OperationServerMessage method readFromJson.

private static OperationServerMessage readFromJson(@Nonnull JsonReader reader) throws IOException {
    checkNotNull(reader, "reader == null");
    ResponseJsonStreamReader responseJsonStreamReader = new ResponseJsonStreamReader(reader);
    Map<String, Object> messageData = responseJsonStreamReader.toMap();
    String id = (String) messageData.get(JSON_KEY_ID);
    String type = (String) messageData.get(JSON_KEY_TYPE);
    switch(type) {
        case ConnectionError.TYPE:
            return new ConnectionError(messagePayload(messageData));
        case ConnectionAcknowledge.TYPE:
            return new ConnectionAcknowledge();
        case Data.TYPE:
            return new Data(id, messagePayload(messageData));
        case Error.TYPE:
            return new Error(id, messagePayload(messageData));
        case Complete.TYPE:
            return new Complete(id);
        default:
            throw new IOException("Unsupported message");
    }
}
Also used : IOException(java.io.IOException) ResponseJsonStreamReader(com.apollographql.apollo.internal.json.ResponseJsonStreamReader)

Aggregations

ResponseJsonStreamReader (com.apollographql.apollo.internal.json.ResponseJsonStreamReader)2 IOException (java.io.IOException)2 Error (com.apollographql.apollo.api.Error)1 MapFieldValueResolver (com.apollographql.apollo.internal.field.MapFieldValueResolver)1 BufferedSourceJsonReader (com.apollographql.apollo.internal.json.BufferedSourceJsonReader)1 RealResponseReader (com.apollographql.apollo.internal.response.RealResponseReader)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1