Search in sources :

Example 11 with ResultPath

use of graphql.execution.ResultPath in project graphql-java-extended-validation by graphql-java.

the class TargetedValidationRules method walkObjectArg.

private List<GraphQLError> walkObjectArg(ValidationRule rule, ValidationEnvironment validationEnvironment, GraphQLInputObjectType argumentType, Map<String, Object> objectMap) {
    List<GraphQLError> errors = new ArrayList<>();
    // run them in a stable order
    List<GraphQLInputObjectField> fieldDefinitions = Util.sort(argumentType.getFieldDefinitions(), GraphQLInputObjectField::getName);
    for (GraphQLInputObjectField inputField : fieldDefinitions) {
        GraphQLInputType fieldType = inputField.getType();
        Object validatedValue = objectMap.getOrDefault(inputField.getName(), GraphQLInputObjectField.getInputFieldDefaultValue(inputField));
        if (validatedValue == null) {
            continue;
        }
        ResultPath newPath = validationEnvironment.getValidatedPath().segment(inputField.getName());
        ValidationEnvironment newValidationEnvironment = validationEnvironment.transform(builder -> builder.validatedPath(newPath).validatedValue(validatedValue).validatedType(fieldType).directives(inputField.getDirectives()).validatedElement(INPUT_OBJECT_FIELD));
        List<GraphQLError> ruleErrors = runValidationImpl(rule, newValidationEnvironment, fieldType, validatedValue);
        errors.addAll(ruleErrors);
    }
    return errors;
}
Also used : GraphQLInputType(graphql.schema.GraphQLInputType) GraphQLInputObjectField(graphql.schema.GraphQLInputObjectField) ArrayList(java.util.ArrayList) ResultPath(graphql.execution.ResultPath) GraphQLError(graphql.GraphQLError)

Example 12 with ResultPath

use of graphql.execution.ResultPath in project graphql-java-extended-validation by graphql-java.

the class TargetedValidationRules method runValidationRules.

/**
 * Runs the contained rules that match the currently executing field named by the {@link graphql.schema.DataFetchingEnvironment}
 *
 * @param env           the field being executed
 * @param interpolator  the message interpolator to use
 * @param defaultLocale the default locale in play
 *
 * @return a list of zero or more input data validation errors
 */
public List<GraphQLError> runValidationRules(DataFetchingEnvironment env, MessageInterpolator interpolator, Locale defaultLocale) {
    defaultLocale = LocaleUtil.determineLocale(env, defaultLocale);
    List<GraphQLError> errors = new ArrayList<>();
    GraphQLObjectType fieldContainer = env.getExecutionStepInfo().getObjectType();
    GraphQLFieldDefinition fieldDefinition = env.getFieldDefinition();
    ResultPath fieldPath = env.getExecutionStepInfo().getPath();
    // 
    // run the field specific rules
    ValidationCoordinates fieldCoords = ValidationCoordinates.newCoordinates(fieldContainer, fieldDefinition);
    List<ValidationRule> rules = rulesMap.getOrDefault(fieldCoords, Collections.emptyList());
    if (!rules.isEmpty()) {
        ValidationEnvironment ruleEnvironment = ValidationEnvironment.newValidationEnvironment().dataFetchingEnvironment(env).messageInterpolator(interpolator).locale(defaultLocale).validatedElement(FIELD).validatedPath(fieldPath).build();
        for (ValidationRule rule : rules) {
            List<GraphQLError> ruleErrors = rule.runValidation(ruleEnvironment);
            errors.addAll(ruleErrors);
        }
    }
    // 
    // run the argument specific rules next
    List<GraphQLArgument> sortedArgs = Util.sort(fieldDefinition.getArguments(), GraphQLArgument::getName);
    for (GraphQLArgument fieldArg : sortedArgs) {
        ValidationCoordinates argCoords = ValidationCoordinates.newCoordinates(fieldContainer, fieldDefinition, fieldArg);
        rules = rulesMap.getOrDefault(argCoords, Collections.emptyList());
        if (rules.isEmpty()) {
            continue;
        }
        Object argValue = env.getArgument(fieldArg.getName());
        GraphQLInputType inputType = fieldArg.getType();
        ValidationEnvironment ruleEnvironment = ValidationEnvironment.newValidationEnvironment().dataFetchingEnvironment(env).argument(fieldArg).validatedElement(ARGUMENT).validatedType(inputType).validatedValue(argValue).validatedPath(fieldPath.segment(fieldArg.getName())).directives(fieldArg.getDirectives()).messageInterpolator(interpolator).locale(defaultLocale).build();
        for (ValidationRule rule : rules) {
            List<GraphQLError> ruleErrors = runValidationImpl(rule, ruleEnvironment, inputType, argValue);
            errors.addAll(ruleErrors);
        }
    }
    return errors;
}
Also used : ArrayList(java.util.ArrayList) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) GraphQLArgument(graphql.schema.GraphQLArgument) GraphQLInputType(graphql.schema.GraphQLInputType) GraphQLObjectType(graphql.schema.GraphQLObjectType) ResultPath(graphql.execution.ResultPath) GraphQLError(graphql.GraphQLError)

Aggregations

ResultPath (graphql.execution.ResultPath)12 GraphQLError (graphql.GraphQLError)6 ArrayList (java.util.ArrayList)6 List (java.util.List)4 ExceptionWhileDataFetching (graphql.ExceptionWhileDataFetching)3 GraphQLInputType (graphql.schema.GraphQLInputType)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 ImmutableList (com.google.common.collect.ImmutableList)2 ExecutionStrategyInstrumentationContext (graphql.execution.instrumentation.ExecutionStrategyInstrumentationContext)2 Field (graphql.language.Field)2 SourceLocation (graphql.language.SourceLocation)2 GraphQLDirective (graphql.schema.GraphQLDirective)2 GraphQLFieldDefinition (graphql.schema.GraphQLFieldDefinition)2 Assert (graphql.Assert)1 ErrorClassification (graphql.ErrorClassification)1 ExecutionResult (graphql.ExecutionResult)1 Internal (graphql.Internal)1 QueryTraverser (graphql.analysis.QueryTraverser)1 QueryVisitorFieldEnvironment (graphql.analysis.QueryVisitorFieldEnvironment)1 QueryVisitorStub (graphql.analysis.QueryVisitorStub)1