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

graphql.execution.instrumentation.fieldvalidation.SimpleFieldValidation Maven / Gradle / Ivy

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

import com.google.common.collect.ImmutableList;
import graphql.GraphQLError;
import graphql.PublicApi;
import graphql.execution.ResultPath;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiFunction;

/**
 * This very simple field validation will run the supplied function for a given field path and if it returns an error
 * it will be added to the list of problems.
 *
 * Use {@link #addRule(ResultPath, java.util.function.BiFunction)} to supply the rule callbacks where
 * you implement your specific business logic
 */
@PublicApi
public class SimpleFieldValidation implements FieldValidation {

    private final Map>> rules = new LinkedHashMap<>();

    /**
     * Adds the rule against the field address path.  If the rule returns an error, it will be added to the list of errors
     *
     * @param fieldPath the path to the field
     * @param rule      the rule function
     *
     * @return this validator
     */
    public SimpleFieldValidation addRule(ResultPath fieldPath, BiFunction> rule) {
        rules.put(fieldPath, rule);
        return this;
    }

    @Override
    public List validateFields(FieldValidationEnvironment validationEnvironment) {
        List errors = new ArrayList<>();
        for (Map.Entry>> entry : rules.entrySet()) {
            ResultPath fieldPath = entry.getKey();
            BiFunction> ruleFunction = entry.getValue();

            List fieldAndArguments = validationEnvironment.getFieldsByPath().get(fieldPath);
            if (fieldAndArguments != null) {
                for (FieldAndArguments fieldAndArgument : fieldAndArguments) {
                    Optional graphQLError = ruleFunction.apply(fieldAndArgument, validationEnvironment);
                    graphQLError.ifPresent(errors::add);
                }
            }
        }
        return ImmutableList.copyOf(errors);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy