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

graphql.DeferredExecutionResultImpl Maven / Gradle / Ivy

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

import graphql.execution.ExecutionPath;

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

import static graphql.Assert.assertNotNull;

/**
 * Results that come back from @defer fields have an extra path property that tells you where
 * that deferred result came in the original query
 */
@PublicApi
public class DeferredExecutionResultImpl extends ExecutionResultImpl implements DeferredExecutionResult {

    private final List path;

    private DeferredExecutionResultImpl(List path, ExecutionResultImpl executionResult) {
        super(executionResult);
        this.path = assertNotNull(path);
    }

    /**
     * @return the execution path of this deferred result in the original query
     */
    public List getPath() {
        return path;
    }

    @Override
    public Map toSpecification() {
        Map map = new LinkedHashMap<>(super.toSpecification());
        map.put("path", path);
        return map;
    }

    public static Builder newDeferredExecutionResult() {
        return new Builder();
    }

    public static class Builder {
        private List path = Collections.emptyList();
        private ExecutionResultImpl.Builder builder = ExecutionResultImpl.newExecutionResult();

        public Builder path(ExecutionPath path) {
            this.path = assertNotNull(path).toList();
            return this;
        }

        public Builder from(ExecutionResult executionResult) {
            builder.from((ExecutionResultImpl) executionResult);
            return this;
        }

        public Builder addErrors(List errors) {
            builder.addErrors(errors);
            return this;
        }

        public DeferredExecutionResult build() {
            ExecutionResultImpl build = builder.build();
            return new DeferredExecutionResultImpl(path, build);
        }
    }
}