fieldsOnCorrectTypeRule function

TypedVisitor fieldsOnCorrectTypeRule(
  1. ValidationCtx context
)

Fields on correct type

A GraphQL document is only valid if all fields selected are defined by the parent type, or are an allowed meta field such as __typename.

See https://spec.graphql.org/draft/#sec-Field-Selections

Implementation

TypedVisitor fieldsOnCorrectTypeRule(ValidationCtx context) {
  final visitor = TypedVisitor();
  final typeInfo = context.typeInfo;

  visitor.add<FieldNode>((node) {
    final type = typeInfo.getParentType();
    if (type != null) {
      final fieldDef = typeInfo.getFieldDef();
      if (fieldDef == null) {
        // This field doesn't exist, lets look for suggestions.
        // final schema = context.schema;
        final fieldName = node.name.value;

        // // First determine if there are any suggested types to condition on.
        // var suggestion = didYouMean(
        //   'to use an inline fragment on',
        //   getSuggestedTypeNames(schema, type, fieldName),
        // );

        // // If there are no suggested types, then perhaps this was a typo?
        // if (suggestion == '') {
        //   suggestion = didYouMean(getSuggestedFieldNames(type, fieldName));
        // }

        // Report an error, including helpful suggestions.
        context.reportError(
          GraphQLError(
            'Cannot query field "$fieldName" on type "${type.name}".',
            locations: GraphQLErrorLocation.listFromSource(
              node.span?.start ?? node.name.span?.start,
            ),
            extensions: _fieldsOnCorrectTypeSpec.extensions(),
          ),
        );
      }
    }
  });

  return visitor;
}