Module: Tapioca::Dsl::Helpers::GraphqlSorbetTypes

Extended by:
T::Sig
Defined in:
lib/tapioca/dsl/helpers/graphql_sorbet_types.rb

Overview

Translates a GraphQL type expression, as written in a document, into a Sorbet type string.

Nothing here knows about Fragment: the scalar table is supplied by the caller, and anything absent from it becomes T.untyped. Kept separate so it can move to a gem of its own if a second consumer appears -- no gem does this today, and Tapioca ships no GraphQL compiler.

translate('[SafeString!]!', scalars: { 'SafeString' => '::String' })
#=> "T::Array[::String]"
translate('[String]', scalars: { 'String' => '::String' })
#=> "T::Array[T.nilable(::String)]"

Top-level nullability is the caller's to apply, since only the caller knows whether an optional field is nilable, defaulted, or something else. Nullability inside a list is applied here, having nowhere else to go.

Constant Summary collapse

UNTYPED =
'T.untyped'

Class Method Summary collapse

Class Method Details

.translate(graphql_type, scalars:) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/tapioca/dsl/helpers/graphql_sorbet_types.rb', line 32

def translate(graphql_type, scalars:)
  type = graphql_type.delete_suffix('!')
  return scalars.fetch(type, UNTYPED) unless type.start_with?('[')

  element = type.delete_prefix('[').delete_suffix(']')
  "T::Array[#{list_element(element, scalars: scalars)}]"
end