<commit_before>require "graphql"

module GraphQL
  class Client
    # Public: Document type analyzer.
    module DocumentTypes
      # :nodoc:
      class Validation
        attr_reader :fields

        def initialize
          @fields = {}
        end

        def new
          self
        end

        def validate(context)
          context.visitor[GraphQL::Language::Nodes::FragmentDefinition] << ->(node, _parent) do
            @fields[node] = context.object_types[-1]
          end
          context.visitor[GraphQL::Language::Nodes::Field] << ->(_node, parent) do
            @fields[parent] = context.object_types[-2].unwrap
          end
          context.visitor[GraphQL::Language::Nodes::FragmentSpread] << ->(_node, parent) do
            @fields[parent] = context.object_types[-1].unwrap
          end
        end
      end

      def self.analyze_types(schema, document)
        unless schema.is_a?(GraphQL::Schema)
          raise TypeError, "expected schema to be a GraphQL::Schema, but was #{schema.class}"
        end

        validation = Validation.new
        validator = GraphQL::StaticValidation::Validator.new(schema: schema, rules: [validation])
        query = GraphQL::Query.new(schema, document: document)
        validator.validate(query)
        validation.fields
      end
    end
  end
end
<commit_msg>Use TypeStack directly<commit_after>require "graphql"

module GraphQL
  class Client
    # Public: Document type analyzer.
    module DocumentTypes
      def self.analyze_types(schema, document)
        unless schema.is_a?(GraphQL::Schema)
          raise TypeError, "expected schema to be a GraphQL::Schema, but was #{schema.class}"
        end

        visitor = GraphQL::Language::Visitor.new(document)
        type_stack = GraphQL::StaticValidation::TypeStack.new(schema, visitor)

        fields = {}

        visitor[GraphQL::Language::Nodes::OperationDefinition] << ->(node, _parent) do
          fields[node] = type_stack.object_types.last
        end
        visitor[GraphQL::Language::Nodes::FragmentDefinition] << ->(node, _parent) do
          fields[node] = type_stack.object_types.last
        end
        visitor[GraphQL::Language::Nodes::Field] << ->(node, _parent) do
          fields[node] = type_stack.field_definitions.last.type.unwrap
        end
        visitor.visit

        fields
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.10544074230282581, 'fraction_numerical': 0.0012652889076339097, 'mean_word_length': 2.4831130690161527, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2120761', 'n_tokens_mistral': 653, 'n_tokens_neox': 649, 'n_words': 148}