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

graphql.validation.rules.ProvidedNonNullArguments Maven / Gradle / Ivy

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


import graphql.Internal;
import graphql.language.Argument;
import graphql.language.Directive;
import graphql.language.Field;
import graphql.language.Node;
import graphql.language.NullValue;
import graphql.language.Value;
import graphql.schema.GraphQLArgument;
import graphql.schema.GraphQLDirective;
import graphql.schema.GraphQLFieldDefinition;
import graphql.validation.AbstractRule;
import graphql.validation.ValidationContext;
import graphql.validation.ValidationErrorCollector;
import graphql.validation.ValidationErrorType;

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

import static graphql.schema.GraphQLTypeUtil.isNonNull;

@Internal
public class ProvidedNonNullArguments extends AbstractRule {

    public ProvidedNonNullArguments(ValidationContext validationContext, ValidationErrorCollector validationErrorCollector) {
        super(validationContext, validationErrorCollector);
    }

    @Override
    public void checkField(Field field) {
        GraphQLFieldDefinition fieldDef = getValidationContext().getFieldDef();
        if (fieldDef == null) return;
        Map argumentMap = argumentMap(field.getArguments());

        for (GraphQLArgument graphQLArgument : fieldDef.getArguments()) {
            Argument argument = argumentMap.get(graphQLArgument.getName());
            boolean nonNullType = isNonNull(graphQLArgument.getType());
            boolean noDefaultValue = graphQLArgument.getDefaultValue() == null;
            if (argument == null && nonNullType && noDefaultValue) {
                String message = String.format("Missing field argument %s", graphQLArgument.getName());
                addError(ValidationErrorType.MissingFieldArgument, field.getSourceLocation(), message);
            }

            if(argument!=null) {
                Value value = argument.getValue();
                if ((value == null || value instanceof NullValue) && nonNullType && noDefaultValue) {
                    String message = String.format("null value for non-null field argument %s", graphQLArgument.getName());
                    addError(ValidationErrorType.NullValueForNonNullArgument, field.getSourceLocation(), message);
                }
            }
        }
    }


    @Override
    public void checkDirective(Directive directive, List ancestors) {
        GraphQLDirective graphQLDirective = getValidationContext().getDirective();
        if (graphQLDirective == null) return;
        Map argumentMap = argumentMap(directive.getArguments());

        for (GraphQLArgument graphQLArgument : graphQLDirective.getArguments()) {
            Argument argument = argumentMap.get(graphQLArgument.getName());
            boolean nonNullType = isNonNull(graphQLArgument.getType());
            boolean noDefaultValue = graphQLArgument.getDefaultValue() == null;
            if (argument == null && nonNullType && noDefaultValue) {
                String message = String.format("Missing directive argument %s", graphQLArgument.getName());
                addError(ValidationErrorType.MissingDirectiveArgument, directive.getSourceLocation(), message);
            }
        }
    }

    private Map argumentMap(List arguments) {
        Map result = new LinkedHashMap<>();
        for (Argument argument : arguments) {
            result.put(argument.getName(), argument);
        }
        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy