public GraphQLDirective buildDirectiveFromDefinition(DirectiveDefinition directiveDefinition, Function<Type, GraphQLInputType> inputTypeFactory) { GraphQLDirective.Builder builder = GraphQLDirective.newDirective() .name(directiveDefinition.getName()) .description(buildDescription(directiveDefinition, directiveDefinition.getDescription())); List<DirectiveLocation> locations = buildLocations(directiveDefinition); locations.forEach(builder::validLocations); List<GraphQLArgument> arguments = directiveDefinition.getInputValueDefinitions().stream() .map(arg -> buildDirectiveArgumentFromDefinition(arg, inputTypeFactory)) .collect(Collectors.toList()); arguments.forEach(builder::argument); return builder.build(); }
private void checkDirectiveDefinitions(TypeDefinitionRegistry typeRegistry, List<GraphQLError> errors) { List<DirectiveDefinition> directiveDefinitions = new ArrayList<>(typeRegistry.getDirectiveDefinitions().values()); directiveDefinitions.forEach(directiveDefinition -> { List<InputValueDefinition> arguments = directiveDefinition.getInputValueDefinitions(); checkNamedUniqueness(errors, arguments, InputValueDefinition::getName, (name, arg) -> new NonUniqueNameError(directiveDefinition, arg)); List<Type> inputValueTypes = arguments.stream() .map(InputValueDefinition::getType) .collect(Collectors.toList()); inputValueTypes.forEach( checkTypeExists(typeRegistry, errors, "directive definition", directiveDefinition, directiveDefinition.getName()) ); directiveDefinition.getDirectiveLocations().forEach(directiveLocation -> { String locationName = directiveLocation.getName(); try { Introspection.DirectiveLocation.valueOf(locationName); } catch (IllegalArgumentException e) { errors.add(new DirectiveIllegalLocationError(directiveDefinition, locationName)); } }); }); }
private NodePrinter<DirectiveDefinition> directiveDefinition() { final String argSep = compactMode ? "," : ", "; return (out, node) -> { String arguments = wrap("(", join(node.getInputValueDefinitions(), argSep), ")"); String locations = join(node.getDirectiveLocations(), " | "); out.printf("directive @%s%s on %s", node.getName(), arguments, locations); }; }
@Override public TraversalControl visitDirectiveDefinition(DirectiveDefinition node, TraverserContext<Node> context) { DirectiveDefinition changedNode = node.transform(builder -> { builder.inputValueDefinitions(sort(node.getInputValueDefinitions(), comparing(InputValueDefinition::getName))); builder.directiveLocations(sort(node.getDirectiveLocations(), comparing(DirectiveLocation::getName))); }); return changeNode(context, changedNode); } };
private Builder(DirectiveDefinition existing) { this.sourceLocation = existing.getSourceLocation(); this.comments = existing.getComments(); this.name = existing.getName(); this.description = existing.getDescription(); this.inputValueDefinitions = existing.getInputValueDefinitions(); this.directiveLocations = existing.getDirectiveLocations(); this.ignoredChars = existing.getIgnoredChars(); }
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)); } } }); }
public GraphQLDirective buildDirectiveFromDefinition(DirectiveDefinition directiveDefinition, Function<Type, GraphQLInputType> inputTypeFactory) { GraphQLDirective.Builder builder = GraphQLDirective.newDirective() .name(directiveDefinition.getName()) .description(buildDescription(directiveDefinition, directiveDefinition.getDescription())); List<DirectiveLocation> locations = buildLocations(directiveDefinition); locations.forEach(builder::validLocations); List<GraphQLArgument> arguments = directiveDefinition.getInputValueDefinitions().stream() .map(arg -> buildDirectiveArgumentFromDefinition(arg, inputTypeFactory)) .collect(Collectors.toList()); arguments.forEach(builder::argument); return builder.build(); }
private void checkDirectiveDefinitions(TypeDefinitionRegistry typeRegistry, List<GraphQLError> errors) { List<DirectiveDefinition> directiveDefinitions = new ArrayList<>(typeRegistry.getDirectiveDefinitions().values()); directiveDefinitions.forEach(directiveDefinition -> { List<InputValueDefinition> arguments = directiveDefinition.getInputValueDefinitions(); checkNamedUniqueness(errors, arguments, InputValueDefinition::getName, (name, arg) -> new NonUniqueNameError(directiveDefinition, arg)); List<Type> inputValueTypes = arguments.stream() .map(InputValueDefinition::getType) .collect(Collectors.toList()); inputValueTypes.forEach( checkTypeExists(typeRegistry, errors, "directive definition", directiveDefinition, directiveDefinition.getName()) ); directiveDefinition.getDirectiveLocations().forEach(directiveLocation -> { String locationName = directiveLocation.getName(); try { Introspection.DirectiveLocation.valueOf(locationName); } catch (IllegalArgumentException e) { errors.add(new DirectiveIllegalLocationError(directiveDefinition, locationName)); } }); }); }
private NodePrinter<DirectiveDefinition> directiveDefinition() { final String argSep = compactMode ? "," : ", "; return (out, node) -> { String arguments = wrap("(", join(node.getInputValueDefinitions(), argSep), ")"); String locations = join(node.getDirectiveLocations(), " | "); out.printf("directive @%s%s on %s", node.getName(), arguments, locations); }; }
@Override public TraversalControl visitDirectiveDefinition(DirectiveDefinition node, TraverserContext<Node> context) { DirectiveDefinition changedNode = node.transform(builder -> { builder.inputValueDefinitions(sort(node.getInputValueDefinitions(), comparing(InputValueDefinition::getName))); builder.directiveLocations(sort(node.getDirectiveLocations(), comparing(DirectiveLocation::getName))); }); return changeNode(context, changedNode); } };
private Builder(DirectiveDefinition existing) { this.sourceLocation = existing.getSourceLocation(); this.comments = existing.getComments(); this.name = existing.getName(); this.description = existing.getDescription(); this.inputValueDefinitions = existing.getInputValueDefinitions(); this.directiveLocations = existing.getDirectiveLocations(); this.ignoredChars = existing.getIgnoredChars(); }
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)); } } }); }