Class: Hai::GraphQL::ListQueries

Inherits:
Object
  • Object
show all
Defined in:
lib/hai/graphql/list_queries.rb

Class Method Summary collapse

Class Method Details

.add(query_type, model) ⇒ Object



7
8
9
10
# File 'lib/hai/graphql/list_queries.rb', line 7

def add(query_type, model)
  add_field(query_type, model)
  define_list_method(query_type, model)
end

.add_field(query_type, model) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/hai/graphql/list_queries.rb', line 12

def add_field(query_type, model)
  query_type.field "list_#{model.name.pluralize.downcase}",
                   ["Types::#{model}Type".constantize] do
    query_type.description "List of #{model}."
    argument :filter,
             "#{model}FilterInputType".constantize,
             required: false
    argument :limit, ::GraphQL::Types::Int, required: false
    argument :offset, ::GraphQL::Types::Int, required: false
    argument :sort, Types::SortInputType, required: false

    (model.try(:arguments) || []).each do |name, type, kwargs|
      argument(name, type, **kwargs)
    end
  end
end

.define_list_method(query_type, model) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/hai/graphql/list_queries.rb', line 29

def define_list_method(query_type, model)
  query_type.define_method(
    "list_#{model.name.pluralize.downcase}"
  ) do |**args|
    Hai::Read.new(model, context).list(
      **args.transform_values do |v|
        [Integer, String].include?(v.class) ? v : v.to_h
      end
    )
  end
end