Class: Hai::GraphQL::ReadQueries

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

Class Method Summary collapse

Class Method Details

.add(query_type, model) ⇒ Object



5
6
7
8
# File 'lib/hai/graphql/read_queries.rb', line 5

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

.add_field(query_type, model) ⇒ Object



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

def add_field(query_type, model)
  query_type.field(
    "read_#{model.name.downcase}",
    "Types::#{model}Type".constantize
  ) do
    query_type.description("List a single #{model}.")
    model.attribute_types.each do |attr, type|
      argument(
        attr,
        Hai::GraphQL::AREL_TYPE_CAST[type.class] ||
          Hai::GraphQL::Types::Arel::IntInputType,
        required: false
      )
    end
  end
end

.define_read_method(query_type, model) ⇒ Object



27
28
29
30
31
# File 'lib/hai/graphql/read_queries.rb', line 27

def define_read_method(query_type, model)
  query_type.define_method("read_#{model.name.downcase}") do |**args|
    Hai::Read.new(model, context).read(args.transform_values(&:to_h))
  end
end