Class: Tapioca::Dsl::Compilers::FragmentTypedEntries

Inherits:
Compiler
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/tapioca/dsl/compilers/fragment_typed_entries.rb

Overview

Declares the typed payload classes FragmentClient::TypedEntries.load builds at load time, giving each a real initialize signature and typed readers.

bundle exec tapioca dsl writes them to sorbet/rbi/dsl/fragment_client/entries/. The classes are only visible if whatever calls TypedEntries.load runs when Tapioca loads the application; it needs no credentials, so an initializer works.

Constant Summary collapse

ConstantType =
type_member { { fixed: T.class_of(FragmentClient::TypedLedgerEntry) } }
GRAPHQL_SCALARS =

Sorbet types for the scalars a Fragment Schema binds parameters to. Anything absent -- enums, input objects, newer scalars -- falls through to T.untyped rather than a guess that could reject a valid call.

T.let(
  {
    'String' => '::String',
    'ID' => '::String',
    'SafeString' => '::String',
    'ParameterizedString' => '::String',
    # ISO 8601 strings, not Ruby date objects.
    'Date' => '::String',
    'DateTime' => '::String',
    'FirstMoment' => '::String',
    'LastMoment' => '::String',
    'Period' => '::String',
    'PeriodFilter' => '::String',
    'UTCOffset' => '::String',
    # Fragment's big integers are strings on the wire.
    'Int96' => '::String',
    'Int' => '::Integer',
    'Float' => '::Float',
    'Boolean' => 'T::Boolean',
    'JSON' => 'T.untyped',
    'Parameters' => 'T.untyped'
  }.freeze,
  T::Hash[String, String]
)
COMMON_OPTIONAL_FIELDS =

The optional common fields, in the order the base class declares them.

T.let(
  {
    posted: '::String',
    description: '::String',
    tags: 'T::Array[T.untyped]',
    groups: 'T::Array[T.untyped]',
    conditions: 'T::Array[T.untyped]'
  }.freeze,
  T::Hash[Symbol, String]
)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.gather_constantsObject



69
70
71
72
73
# File 'lib/tapioca/dsl/compilers/fragment_typed_entries.rb', line 69

def gather_constants
  # `name_of` is nil for a payload in an anonymous namespace, whose name
  # (`#<Module:0x...>::AuthCaptureV1`) Sorbet cannot resolve anyway.
  descendants_of(FragmentClient::TypedLedgerEntry).select { |klass| name_of(klass) }
end

Instance Method Details

#decorateObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/tapioca/dsl/compilers/fragment_typed_entries.rb', line 77

def decorate
  spec = constant.spec

  # `create_path` would omit the superclass, leaving Sorbet unaware that a
  # payload inherits `to_entry_input`, `set?` and the common-field readers.
  root.create_class(constant.to_s,
                    superclass_name: '::FragmentClient::TypedLedgerEntry') do |payload|
    payload.create_method('initialize', parameters: initialize_parameters(spec),
                                        return_type: 'void')

    spec.parameters.each do |parameter|
      payload.create_method(parameter.name.to_s, return_type: reader_type(parameter),
                                                 comments: parameter_comments(parameter))
    end

    payload.create_method('entry_type', return_type: '::String', class_method: true,
                                        comments: [comment(spec.entry_type.inspect)])
    payload.create_method('type_version', return_type: '::Integer', class_method: true,
                                          comments: [comment(spec.type_version.to_s)])
  end
end