validateElement method

  1. @override
void validateElement(
  1. SchemaValidationContext context,
  2. GraphQLElement element
)
override

Validates the provided element following the configuration of this attachment

Implementation

@override
void validateElement(
  SchemaValidationContext context,
  GraphQLElement element,
) {
  if (element is! GraphQLObjectType) {
    context.reportError(
      'KeyAttachment should only be used in $GraphQLObjectType.'
      ' Was used in $element.',
      [],
    );
  }
  try {
    final document =
        gql.parseString('fragment F on ${element.name} {$fields}');

    final errors = validateDocument(context.schema, document);
    for (final e in errors
        .where((error) => error.message != 'Fragment "F" is never used.')) {
      context.reportError(
        'KeyAttachment(fields: "$fields") error for'
        ' element ${element.name}. ${e.message}',
        [],
      );
    }
  } catch (_) {
    context.reportError(
      'KeyAttachment.fields should be a selection'
      ' set of the annotated Object. Found $fields'
      ' for element ${element.name}.',
      [],
    );
  }
}