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

graphql.incremental.DeferPayload Maven / Gradle / Ivy

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

import graphql.ExecutionResult;
import graphql.ExperimentalApi;
import graphql.GraphQLError;
import org.jetbrains.annotations.Nullable;

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

/**
 * Represents a defer payload
 */
@ExperimentalApi
public class DeferPayload extends IncrementalPayload {
    private final Object data;

    private DeferPayload(Object data, List path, String label, List errors, Map extensions) {
        super(path, label, errors, extensions);
        this.data = data;
    }

    /**
     * @param  the type to cast the result to
     * @return the resolved data
     */
    @Nullable
    public  T getData() {
        // noinspection unchecked
        return (T) this.data;
    }

    /**
     * @return a map of this payload that strictly follows the spec
     */
    @Override
    public Map toSpecification() {
        Map map = new LinkedHashMap<>(super.toSpecification());
        map.put("data", data);
        return map;
    }

    /**
     * @return a {@link DeferPayload.Builder} that can be used to create an instance of {@link DeferPayload}
     */
    public static DeferPayload.Builder newDeferredItem() {
        return new DeferPayload.Builder();
    }

    public static class Builder extends IncrementalPayload.Builder {
        private Object data = null;

        public Builder data(Object data) {
            this.data = data;
            return this;
        }

        public Builder from(DeferPayload deferredItem) {
            super.from(deferredItem);
            this.data = deferredItem.data;
            return this;
        }

        public Builder from(ExecutionResult executionResult) {
            this.data = executionResult.getData();
            this.errors = executionResult.getErrors();
            this.extensions = executionResult.getExtensions();

            return this;
        }

        public DeferPayload build() {
            return new DeferPayload(data, this.path, this.label, this.errors, this.extensions);
        }
    }
}