Class: Tapioca::Dsl::Compilers::FragmentQueryMethods

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

Overview

Declares the query and mutation methods FragmentClient answers to.

define_method_from_queries creates one singleton method per operation on each client instance, so without this Sorbet reports Method 'create_ledger' does not exist for every one of them and a consumer has to write T.unsafe(client).

Covers the operations shipped in queries.graphql, plus any document passed to FragmentClient.load_queries. Operations in FragmentClient::WRAPPED_OPERATIONS are skipped: those have hand-written methods with real signatures already.

variables is the operation's variables hash and stays untyped. Its keys are the GraphQL variable names verbatim, as the rest of this SDK passes them. The return type comes from FragmentResponseTypes.

Constant Summary collapse

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.gather_constantsObject



35
36
37
# File 'lib/tapioca/dsl/compilers/fragment_query_methods.rb', line 35

def gather_constants
  [FragmentClient]
end

Instance Method Details

#decorateObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/tapioca/dsl/compilers/fragment_query_methods.rb', line 41

def decorate
  methods = FragmentGraphQl.operations.keys.to_h do |operation|
    [FragmentGraphQl.method_name_for(operation), operation]
  end
  methods.reject! { |name, _| FragmentClient::WRAPPED_OPERATIONS.include?(name) }
  return if methods.empty?

  root.create_path(constant) do |klass|
    methods.keys.sort.each do |name|
      klass.create_method(
        name,
        parameters: [create_param('variables', type: 'T::Hash[Symbol, T.untyped]')],
        return_type: "::FragmentClient::Responses::#{methods.fetch(name)}"
      )
    end
  end
end