All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.rapidgraphql.client.extractor.AbstractResultExtractor Maven / Gradle / Ivy

There is a newer version: 2.4.0
Show newest version
package org.rapidgraphql.client.extractor;

import com.fasterxml.jackson.databind.ObjectMapper;
import kong.unirest.core.JsonNode;
import kong.unirest.core.json.JSONObject;
import org.rapidgraphql.client.exceptions.GraphQLError;
import org.rapidgraphql.client.exceptions.GraphQLErrorException;

import java.util.List;

public abstract class AbstractResultExtractor implements ResultExtractor {
    private static final ValueExtractor errorsExtractor = new ListValueExtractor(
            new ObjectValueExtractor(GraphQLError.class, new ObjectMapper())
    );
    @Override
    public Object extract(JsonNode jsonNode) {
        if (jsonNode.getObject().has("errors")) {
            List errors = (List)errorsExtractor.extract(jsonNode.getObject(), "errors");
            throw new GraphQLErrorException(errors);
        }
        JSONObject data = jsonNode.getObject().optJSONObject("data");
        return applyOnData(data);
    }

    protected abstract Object applyOnData(JSONObject data);
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy