Module: RailsQuery::Adapter::ClassMethods

Defined in:
lib/rails_query/adapter.rb

Overview

Class methods for defining base URL, client, queries, and mutations

Instance Method Summary collapse

Instance Method Details

#base_url(url = nil) ⇒ Object



12
13
14
15
16
# File 'lib/rails_query/adapter.rb', line 12

def base_url(url = nil)
  return @base_url if url.nil?

  @base_url = url
end

#client(&block) ⇒ Object



18
19
20
21
22
# File 'lib/rails_query/adapter.rb', line 18

def client(&block)
  return @client_block if block.nil?

  @client_block = block
end

#mutation(name, &block) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rails_query/adapter.rb', line 44

def mutation(name, &block)
  define_method(name) do |*args, **opts|
    opts_with_context = inject_context(**opts)

    if block
      instance_exec(*args, **opts_with_context, &block)
    else
      mutation_class = resolve_mutation_class(name)
      mutation_class.call(*args, **opts_with_context)
    end
  end

  define_singleton_method(name) do |*args, **opts|
    instance.public_send(name, *args, **opts)
  end
end

#query(name, ttl: nil, &block) ⇒ Object

rubocop:disable Metrics/MethodLength



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rails_query/adapter.rb', line 25

def query(name, ttl: nil, &block)
  define_method(name) do |*args, **opts|
    opts_with_context = inject_context(**opts)

    if block
      execute_with_cache(name, ttl, args) do
        instance_exec(*args, **opts_with_context, &block)
      end
    else
      query_class = resolve_query_class(name)
      query_class.call(*args, **opts_with_context)
    end
  end

  define_singleton_method(name) do |*args, **opts|
    instance.public_send(name, *args, **opts)
  end
end