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

graphql.execution.DataFetcherResult Maven / Gradle / Ivy

package graphql.execution;

import graphql.GraphQLError;
import graphql.PublicApi;
import graphql.schema.DataFetcher;

import java.util.ArrayList;
import java.util.List;

import static graphql.Assert.assertNotNull;
import static java.util.Collections.unmodifiableList;


/**
 * An object that can be returned from a {@link DataFetcher} that contains both data and errors to be relativized and
 * added to the final result. This is a useful when your ``DataFetcher`` retrieves data from multiple sources
 * or from another GraphQL resource.
 *
 * @param  The type of the data fetched
 */
@PublicApi
public class DataFetcherResult {

    private final T data;
    private final List errors;

    public DataFetcherResult(T data, List errors) {
        this.data = data;
        this.errors = unmodifiableList(assertNotNull(errors));
    }

    /**
     * @return The data fetched. May be null.
     */
    public T getData() {
        return data;
    }

    /**
     * @return errors encountered when fetching data.  This will be non null but possibly empty.
     */
    public List getErrors() {
        return errors;
    }

    /**
     * Creates a new data fetcher result builder
     *
     * @param  the type of the result
     *
     * @return a new builder
     */
    public static  Builder newResult() {
        return new Builder<>();
    }

    public static class Builder {
        private T data;
        private final List errors = new ArrayList<>();

        public Builder(T data) {
            this.data = data;
        }

        public Builder() {
        }

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

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

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

        public DataFetcherResult build() {
            return new DataFetcherResult<>(data, errors);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy