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

graphql.incremental.IncrementalPayload Maven / Gradle / Ivy

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

import graphql.ExperimentalApi;
import graphql.GraphQLError;
import graphql.execution.ResultPath;
import org.jetbrains.annotations.Nullable;

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

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

/**
 * Represents a payload that can be resolved after the initial response.
 */
@ExperimentalApi
public abstract class IncrementalPayload {
    private final List path;
    private final String label;
    private final List errors;
    private final transient Map extensions;

    protected IncrementalPayload(List path, String label, List errors, Map extensions) {
        this.path = path;
        this.errors = errors;
        this.label = label;
        this.extensions = extensions;
    }

    /**
     * @return list of field names and indices from root to the location of the corresponding `@defer` or `@stream` directive.
     */
    public List getPath() {
        return this.path;
    }

    /**
     * @return value derived from the corresponding `@defer` or `@stream` directive.
     */
    @Nullable
    public String getLabel() {
        return label;
    }

    /**
     * @return a list of field errors encountered during execution.
     */
    @Nullable
    public List getErrors() {
        return this.errors;
    }

    /**
     * @return a map of extensions or null if there are none
     */
    @Nullable
    public Map getExtensions() {
        return this.extensions;
    }

    public Map toSpecification() {
        Map result = new LinkedHashMap<>();

        result.put("path", path);

        if (label != null) {
            result.put("label", label);
        }

        if (errors != null && !errors.isEmpty()) {
            result.put("errors", errorsToSpec(errors));
        }
        if (extensions != null) {
            result.put("extensions", extensions);
        }
        return result;
    }

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


    protected static abstract class Builder> {
        protected List path;
        protected String label;
        protected List errors = new ArrayList<>();
        protected Map extensions;

        public T from(IncrementalPayload incrementalPayload) {
            this.path = incrementalPayload.getPath();
            this.label = incrementalPayload.getLabel();
            if (incrementalPayload.getErrors() != null) {
                this.errors = new ArrayList<>(incrementalPayload.getErrors());
            }
            this.extensions = incrementalPayload.getExtensions();
            return (T) this;
        }

        public T path(ResultPath path) {
            if (path != null) {
                this.path = path.toList();
            }
            return (T) this;
        }

        public T path(List path) {
            this.path = path;
            return (T) this;
        }

        public T label(String label) {
            this.label = label;
            return (T) this;
        }

        public T errors(List errors) {
            this.errors = errors;
            return (T) this;
        }

        public T addErrors(List errors) {
            this.errors.addAll(errors);
            return (T) this;
        }

        public T addError(GraphQLError error) {
            this.errors.add(error);
            return (T) this;
        }

        public T extensions(Map extensions) {
            this.extensions = extensions;
            return (T) this;
        }

        public T addExtension(String key, Object value) {
            this.extensions = (this.extensions == null ? new LinkedHashMap<>() : this.extensions);
            this.extensions.put(key, value);
            return (T) this;
        }
    }
}