public ArgumentValidationUtil(Argument argument) { argumentName = argument.getName(); argumentValue = argument.getValue(); }
private boolean sameArguments(List<Argument> arguments1, List<Argument> arguments2) { if (arguments1.size() != arguments2.size()) { return false; } for (Argument argument : arguments1) { Argument matchedArgument = findArgumentByName(argument.getName(), arguments2); if (matchedArgument == null) { return false; } if (!sameValue(argument.getValue(), matchedArgument.getValue())) { return false; } } return true; }
public String buildDeprecationReason(List<Directive> directives) { directives = directives == null ? emptyList() : directives; Optional<Directive> directive = directives.stream().filter(d -> "deprecated".equals(d.getName())).findFirst(); if (directive.isPresent()) { Map<String, String> args = directive.get().getArguments().stream().collect(toMap( Argument::getName, arg -> ((StringValue) arg.getValue()).getValue() )); if (args.isEmpty()) { return NO_LONGER_SUPPORTED; // default value from spec } else { // pre flight checks have ensured its valid return args.get("reason"); } } return null; }
/** * A special check for the magic @deprecated directive * * @param errors the list of errors * @param directive the directive to check * @param errorSupplier the error supplier function */ static void checkDeprecatedDirective(List<GraphQLError> errors, Directive directive, Supplier<InvalidDeprecationDirectiveError> errorSupplier) { if ("deprecated".equals(directive.getName())) { // it can have zero args List<Argument> arguments = directive.getArguments(); if (arguments.size() == 0) { return; } // but if has more than it must have 1 called "reason" of type StringValue if (arguments.size() == 1) { Argument arg = arguments.get(0); if ("reason".equals(arg.getName()) && arg.getValue() instanceof StringValue) { return; } } // not valid errors.add(errorSupplier.get()); } }
private Map<String, Object> getArgumentValuesImpl(GraphQLCodeRegistry codeRegistry, List<GraphQLArgument> argumentTypes, List<Argument> arguments, Map<String, Object> variables) { if (argumentTypes.isEmpty()) { return Collections.emptyMap(); } Map<String, Object> result = new LinkedHashMap<>(); Map<String, Argument> argumentMap = argumentMap(arguments); for (GraphQLArgument fieldArgument : argumentTypes) { String argName = fieldArgument.getName(); Argument argument = argumentMap.get(argName); Object value; if (argument != null) { value = coerceValueAst(codeRegistry.getFieldVisibility(), fieldArgument.getType(), argument.getValue(), variables); } else { value = fieldArgument.getDefaultValue(); } // only put an arg into the result IF they specified a variable at all or // the default value ended up being something non null if (argumentMap.containsKey(argName) || value != null) { result.put(argName, value); } } return result; }
private Builder(Argument existing) { this.sourceLocation = existing.getSourceLocation(); this.comments = existing.getComments(); this.name = existing.getName(); this.value = existing.getValue(); this.ignoredChars = existing.getIgnoredChars(); }
private GraphQLArgument buildDirectiveArgument(Argument arg, Optional<GraphQLDirective> directiveDefinition) { Optional<GraphQLArgument> directiveDefArgument = directiveDefinition.map(dd -> dd.getArgument(arg.getName())); GraphQLArgument.Builder builder = GraphQLArgument.newArgument(); builder.name(arg.getName()); GraphQLInputType inputType; Object defaultValue = null; if (directiveDefArgument.isPresent()) { inputType = directiveDefArgument.get().getType(); defaultValue = directiveDefArgument.get().getDefaultValue(); } else { inputType = buildDirectiveInputType(arg.getValue()); } builder.type(inputType); builder.defaultValue(defaultValue); Object value = buildValue(arg.getValue(), inputType); // // we put the default value in if the specified is null builder.value(value == null ? defaultValue : value); return builder.build(); }
private void checkDirectiveArguments(List<GraphQLError> errors, TypeDefinitionRegistry typeRegistry, Node element, String elementName, Directive directive, DirectiveDefinition directiveDefinition) { Map<String, InputValueDefinition> allowedArgs = getByName(directiveDefinition.getInputValueDefinitions(), (InputValueDefinition::getName), mergeFirst()); Map<String, Argument> providedArgs = getByName(directive.getArguments(), (Argument::getName), mergeFirst()); directive.getArguments().forEach(argument -> { InputValueDefinition allowedArg = allowedArgs.get(argument.getName()); if (allowedArg == null) { errors.add(new DirectiveUnknownArgumentError(element, elementName, directive.getName(), argument.getName())); } else { ArgValueOfAllowedTypeChecker argValueOfAllowedTypeChecker = new ArgValueOfAllowedTypeChecker(directive, element, elementName, argument, typeRegistry, runtimeWiring); argValueOfAllowedTypeChecker.checkArgValueMatchesAllowedType(errors, argument.getValue(), allowedArg.getType()); } }); allowedArgs.forEach((argName, definitionArgument) -> { if (isNoNullArgWithoutDefaultValue(definitionArgument)) { if (!providedArgs.containsKey(argName)) { errors.add(new DirectiveMissingNonNullArgumentError(element, elementName, directive.getName(), argName)); } } }); }
@Override public void checkArgument(Argument argument) { GraphQLArgument fieldArgument = getValidationContext().getArgument(); if (fieldArgument == null) return; ArgumentValidationUtil validationUtil = new ArgumentValidationUtil(argument); if (!validationUtil.isValidLiteralValue(argument.getValue(), fieldArgument.getType(), getValidationContext().getSchema())) { addError(ValidationErrorType.WrongType, argument.getSourceLocation(), validationUtil.getMessage()); } } }
@SuppressWarnings( "unchecked" ) private <R extends Value> R getValue(Argument argument) { return (R) argument.getValue(); }
@SuppressWarnings( "unchecked" ) protected final <R> R getArgumentValue(Argument argument) { return (R) argument.getValue(); }
public ArgumentValidationUtil(Argument argument) { argumentName = argument.getName(); argumentValue = argument.getValue(); }
public static Optional<String> getDirectiveArgument(GraphQLObjectType parentType, String directiveName, String argumentName) { return Optional.ofNullable(parentType.getDefinition().getDirective(directiveName)) .map(d -> d.getArgument(argumentName)) .map(v -> (StringValue) v.getValue()) .map(StringValue::getValue); }
public static Optional<String> getDirectiveArgument(GraphQLFieldDefinition field, String directiveName, String argumentName) { return Optional.ofNullable(field.getDefinition().getDirective(directiveName)) .map(d -> d.getArgument(argumentName)) .map(v -> (StringValue) v.getValue()) .map(StringValue::getValue); } }
private static String getPackageName(List<Directive> directives, String defaultPackageName) { String packageName = null; for ( Directive directive : directives ) { if ( ! "java".equals(directive.getName()) ) continue; for ( Argument arg : directive.getArguments() ) { if ( ! "package".equals(arg.getName()) ) continue; packageName = (String)Scalars.GraphQLString.getCoercing().parseLiteral(arg.getValue()); break; } break; } return ( null == packageName ) ? defaultPackageName : packageName; }
private Builder(Argument existing) { this.sourceLocation = existing.getSourceLocation(); this.comments = existing.getComments(); this.name = existing.getName(); this.value = existing.getValue(); this.ignoredChars = existing.getIgnoredChars(); }
@Override public void checkArgument(Argument argument) { GraphQLArgument fieldArgument = getValidationContext().getArgument(); if (fieldArgument == null) return; ArgumentValidationUtil validationUtil = new ArgumentValidationUtil(argument); if (!validationUtil.isValidLiteralValue(argument.getValue(), fieldArgument.getType(), getValidationContext().getSchema())) { addError(ValidationErrorType.WrongType, argument.getSourceLocation(), validationUtil.getMessage()); } } }