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

graphql.ExecutionResultImpl Maven / Gradle / Ivy

There is a newer version: 230521-nf-execution
Show newest version
package graphql;


import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static java.util.stream.Collectors.toList;

@Internal
public class ExecutionResultImpl implements ExecutionResult {

    private final Object data;
    private final List errors;
    private final transient boolean dataPresent;
    private final transient Map extensions;

    public ExecutionResultImpl(GraphQLError error) {
        this(false, null, Collections.singletonList(error), null);
    }

    public ExecutionResultImpl(List errors) {
        this(false, null, errors, null);
    }

    public ExecutionResultImpl(Object data, List errors) {
        this(true, data, errors, null);
    }

    public ExecutionResultImpl(Object data, List errors, Map extensions) {
        this(true, data, errors, extensions);
    }

    private ExecutionResultImpl(boolean dataPresent, Object data, List errors, Map extensions) {
        this.dataPresent = dataPresent;
        this.data = data;

        if (errors != null && !errors.isEmpty()) {
            this.errors = Collections.unmodifiableList(new ArrayList<>(errors));
        } else {
            this.errors = Collections.emptyList();
        }

        this.extensions = extensions;
    }

    @Override
    public  T getData() {
        //noinspection unchecked
        return (T) data;
    }

    @Override
    public List getErrors() {
        return errors;
    }

    @Override
    public Map getExtensions() {
        return extensions;
    }

    @Override
    public Map toSpecification() {
        Map result = new LinkedHashMap<>();
        if (dataPresent) {
            result.put("data", data);
        }
        if (errors != null && !errors.isEmpty()) {
            result.put("errors", errorsToSpec(errors));
        }
        if (extensions != null) {
            result.put("extensions", extensions);
        }
        return result;
    }

    private Object errorsToSpec(List errors) {
        return errors.stream().map(GraphQLError::toSpecification).collect(toList());
    }

    @Override
    public String toString() {
        return "ExecutionResultImpl{" +
                "data=" + data +
                ", errors=" + errors +
                ", dataPresent=" + dataPresent +
                ", extensions=" + extensions +
                '}';
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy