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

graphql.validation.ValidationError Maven / Gradle / Ivy

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


import graphql.ErrorType;
import graphql.GraphQLError;
import graphql.GraphqlErrorHelper;
import graphql.PublicApi;
import graphql.language.SourceLocation;

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

@PublicApi
public class ValidationError implements GraphQLError {

    private final String message;
    private final List locations = new ArrayList<>();
    private final String description;
    private final ValidationErrorType validationErrorType;
    private final List queryPath;
    private final Map extensions;

    public ValidationError(ValidationErrorType validationErrorType) {
        this(newValidationError()
                .validationErrorType(validationErrorType));
    }

    public ValidationError(ValidationErrorType validationErrorType, SourceLocation sourceLocation, String description) {
        this(newValidationError()
                .validationErrorType(validationErrorType)
                .sourceLocation(sourceLocation)
                .description(description));
    }

    public ValidationError(ValidationErrorType validationErrorType, SourceLocation sourceLocation, String description, List queryPath) {
        this(newValidationError()
                .validationErrorType(validationErrorType)
                .sourceLocation(sourceLocation)
                .description(description)
                .queryPath(queryPath));
    }

    public ValidationError(ValidationErrorType validationErrorType, List sourceLocations, String description) {
        this(newValidationError()
                .validationErrorType(validationErrorType)
                .sourceLocations(sourceLocations)
                .description(description));
    }

    public ValidationError(ValidationErrorType validationErrorType, List sourceLocations, String description, List queryPath) {
        this(newValidationError()
                .validationErrorType(validationErrorType)
                .sourceLocations(sourceLocations)
                .description(description)
                .queryPath(queryPath));
    }

    private ValidationError(Builder builder) {
        this.validationErrorType = builder.validationErrorType;
        if (builder.sourceLocations != null) {
            this.locations.addAll(builder.sourceLocations);
        }
        this.description = builder.description;
        this.message = mkMessage(builder.validationErrorType, builder.description, builder.queryPath);
        this.queryPath = builder.queryPath;
        this.extensions = builder.extensions;
    }

    private String mkMessage(ValidationErrorType validationErrorType, String description, List queryPath) {
        return String.format("Validation error of type %s: %s%s", validationErrorType, description, toPath(queryPath));
    }

    private String toPath(List queryPath) {
        if (queryPath == null) {
            return "";
        }
        return String.format(" @ '%s'", String.join("/", queryPath));
    }

    public ValidationErrorType getValidationErrorType() {
        return validationErrorType;
    }

    @Override
    public String getMessage() {
        return message;
    }

    public String getDescription() {
        return description;
    }

    @Override
    public List getLocations() {
        return locations;
    }

    @Override
    public ErrorType getErrorType() {
        return ErrorType.ValidationError;
    }

    public List getQueryPath() {
        return queryPath;
    }

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

    @Override
    public String toString() {
        return "ValidationError{" +
                "validationErrorType=" + validationErrorType +
                ", queryPath=" + queryPath +
                ", message=" + message +
                ", locations=" + locations +
                ", description='" + description + '\'' +
                '}';
    }

    @SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
    @Override
    public boolean equals(Object o) {
        return GraphqlErrorHelper.equals(this, o);
    }

    @Override
    public int hashCode() {
        return GraphqlErrorHelper.hashCode(this);
    }


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

    public static class Builder {
        private List sourceLocations;
        private Map extensions;
        private String description;
        private ValidationErrorType validationErrorType;
        private List queryPath;


        public Builder validationErrorType(ValidationErrorType validationErrorType) {
            this.validationErrorType = validationErrorType;
            return this;
        }

        public Builder description(String description) {
            this.description = description;
            return this;
        }

        public Builder queryPath(List queryPath) {
            this.queryPath = queryPath;
            return this;
        }

        public Builder sourceLocation(SourceLocation sourceLocation) {
            this.sourceLocations = sourceLocation == null ? null : Collections.singletonList(sourceLocation);
            return this;
        }

        public Builder sourceLocations(List sourceLocations) {
            this.sourceLocations = sourceLocations;
            return this;
        }

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

        public ValidationError build() {
            return new ValidationError(this);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy