providedRequiredArgumentsRule function

Visitor providedRequiredArgumentsRule(
  1. ValidationCtx context
)

Provided required arguments

A field or directive is only valid if all required (non-null without a default value) field arguments have been provided.

See https://spec.graphql.org/draft/#sec-Required-Arguments

Implementation

Visitor providedRequiredArgumentsRule(
  ValidationCtx context,
) {
  final visitor = TypedVisitor();
  // eslint-disable-next-line new-cap
  visitor.mergeInPlace(providedRequiredArgumentsOnDirectivesRule(context));
  visitor.add<FieldNode>((_) {},
      // Validate on leave to allow for deeper errors to appear first.
      leave: (fieldNode) {
    final fieldDef = context.typeInfo.getFieldDef();
    if (fieldDef == null) {
      return VisitBehavior.skipTree;
    }

    final providedArgs =
        // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
        fieldNode.arguments.map((arg) => arg.name.value).toSet();
    for (final argDef in fieldDef.inputs) {
      if (!providedArgs.contains(argDef.name) && argDef.isRequired) {
        final argTypeStr = inspect(argDef.type);
        context.reportError(
          GraphQLError(
            'Field "${fieldDef.name}" argument "${argDef.name}" of type "${argTypeStr}" is required, but it was not provided.',
            locations: GraphQLErrorLocation.firstFromNodes(
                [fieldNode, fieldNode.name]),
            extensions: _providedRequiredArgumentsSpec.extensions(),
          ),
        );
      }
    }
  });

  return visitor;
}