knownArgumentNamesOnDirectivesRule function

TypedVisitor knownArgumentNamesOnDirectivesRule(
  1. SDLValidationCtx context
)

@internal

Implementation

TypedVisitor knownArgumentNamesOnDirectivesRule(
  SDLValidationCtx context,
) {
  final visitor = TypedVisitor();
  final directiveArgs = <String, Set<String>>{};

  final schema = context.schema;
  final definedDirectives =
      schema != null ? schema.directives : GraphQLDirective.specifiedDirectives;
  for (final directive in definedDirectives) {
    directiveArgs[directive.name] =
        directive.inputs.map((arg) => arg.name).toSet();
  }

  final astDefinitions =
      context.document.definitions.whereType<DirectiveDefinitionNode>();
  for (final def in astDefinitions) {
    // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
    final argsNodes = def.args;

    directiveArgs[def.name.value] =
        argsNodes.map((arg) => arg.name.value).toSet();
  }

  visitor.add<DirectiveNode>((directiveNode) {
    final directiveName = directiveNode.name.value;
    final knownArgs = directiveArgs[directiveName];

    if (knownArgs != null) {
      for (final argNode in directiveNode.arguments) {
        final argName = argNode.name.value;
        if (!knownArgs.contains(argName)) {
          // final suggestions = suggestionList(argName, knownArgs);
          context.reportError(
            GraphQLError(
              'Unknown argument "${argName}" on directive "@${directiveName}".'
              //  + didYouMean(suggestions)
              ,
              locations: GraphQLErrorLocation.firstFromNodes(
                [argNode, argNode.name],
              ),
              extensions: _knownArgumentNamesOnDirectivesSpec.extensions(),
            ),
          );
        }
      }
    }

    return VisitBehavior.skipTree;
  });
  return visitor;
}