getDirectiveLocationForASTPath function

DirectiveLocation? getDirectiveLocationForASTPath(
  1. List<Node> ancestors
)

Implementation

DirectiveLocation? getDirectiveLocationForASTPath(
  List<Node> ancestors, // Array<ASTNode | ReadonlyArray<ASTNode>>
) {
  final appliedTo = ancestors[ancestors.length - 1];
  // invariant('kind' in appliedTo);

  switch (appliedTo.kind) {
    case Kind.OperationDefinition:
      return _getDirectiveLocationForOperation(
          (appliedTo as OperationDefinitionNode).type);
    case Kind.Field:
      return DirectiveLocation.FIELD;
    case Kind.FragmentSpread:
      return DirectiveLocation.FRAGMENT_SPREAD;
    case Kind.InlineFragment:
      return DirectiveLocation.INLINE_FRAGMENT;
    case Kind.FragmentDefinition:
      return DirectiveLocation.FRAGMENT_DEFINITION;
    case Kind.VariableDefinition:
      return DirectiveLocation.VARIABLE_DEFINITION;
    case Kind.SchemaDefinition:
    case Kind.SchemaExtension:
      return DirectiveLocation.SCHEMA;
    case Kind.ScalarTypeDefinition:
    case Kind.ScalarTypeExtension:
      return DirectiveLocation.SCALAR;
    case Kind.ObjectTypeDefinition:
    case Kind.ObjectTypeExtension:
      return DirectiveLocation.OBJECT;
    case Kind.FieldDefinition:
      return DirectiveLocation.FIELD_DEFINITION;
    case Kind.InterfaceTypeDefinition:
    case Kind.InterfaceTypeExtension:
      return DirectiveLocation.INTERFACE;
    case Kind.UnionTypeDefinition:
    case Kind.UnionTypeExtension:
      return DirectiveLocation.UNION;
    case Kind.EnumTypeDefinition:
    case Kind.EnumTypeExtension:
      return DirectiveLocation.ENUM;
    case Kind.EnumValueDefinition:
      return DirectiveLocation.ENUM_VALUE;
    case Kind.InputObjectTypeDefinition:
    case Kind.InputObjectTypeExtension:
      return DirectiveLocation.INPUT_OBJECT;
    case Kind.InputValueDefinition:
      {
        var parentNode = ancestors[ancestors.length - 3];
        // TODO: 3I this should not be necessary check usage of ancestor
        if (parentNode is DocumentNode) {
          parentNode = ancestors[ancestors.length - 2];
        }
        // invariant('kind' in parentNode);
        return parentNode.kind == Kind.InputObjectTypeDefinition
            ? DirectiveLocation.INPUT_FIELD_DEFINITION
            : DirectiveLocation.ARGUMENT_DEFINITION;
      }
    default:
      return null;
  }
}