Sha256: 3fc439a41a142109b931588b657ff279437729256553d20e70aeee0adc4519f1

Contents?: true

Size: 1.5 KB

Versions: 2

Compression:

Stored size: 1.5 KB

Contents

# typed: strict
# frozen_string_literal: true

module Yogurt
  class CodeGenerator
    class FieldAccessPath < T::Struct
      extend T::Sig
      extend Utils
      include Utils
      include Memoize

      # Name of the method
      const :name, Symbol

      # Sorbet signature for the value of the field
      const :signature, String

      # Expression for converting the value of the field
      const :expression, String

      # GraphQL schema for the query that this FieldAccessPath was derived from
      const :schema, GRAPHQL_SCHEMA

      # The types of all of the fragments leading to this field
      const :fragment_types, T::Array[String]

      sig {returns(T.self_type)}
      def freeze
        compatible_object_types
        fragment_types.each(&:freeze)
        fragment_types.freeze
        expression.freeze
        signature.freeze
        super
        self
      end

      # This field access path will only be evaluated if the object in the query
      # is one of the objects in this set.
      sig {returns(T::Set[String])}
      def compatible_object_types
        memoize_as(:compatible_object_types) do
          result = schema.possible_types(schema.types[fragment_types.fetch(0)]).to_set

          if fragment_types.size > 1
            T.must(fragment_types[1..-1]).each do |next_type|
              result = result.intersection(schema.possible_types(schema.types[next_type]))
            end
          end

          result.map(&:graphql_name).to_set.freeze
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
yogurt-0.2.0 lib/yogurt/code_generator/field_access_path.rb
yogurt-0.1.1 lib/yogurt/code_generator/field_access_path.rb