Class: Graphql::HydrateQuery

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

Instance Method Summary collapse

Constructor Details

#initialize(model, context, order_by: nil, filter: nil, check_visibility: true, id: nil, user: nil, page: nil, per_page: nil) ⇒ HydrateQuery

Returns a new instance of HydrateQuery.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/graphql/hydrate_query.rb', line 6

def initialize(model, context, order_by: nil, filter: nil, check_visibility: true, id: nil, user: nil, page: nil, per_page: nil)
  @context = context
  @filter = filter
  @order_by = order_by
  @model = model
  @check_visibility = check_visibility

  if id.present? && !valid_id?(id)
    raise GraphQL::ExecutionError, "Invalid id: #{id}"
  end

  @id = id
  @user = user
  @page = page&.to_i || 1
  @per_page = per_page&.to_i || 1000
  @per_page = 1000 if @per_page > 1000
end

Instance Method Details

#paginated_runObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/graphql/hydrate_query.rb', line 39

def paginated_run
  transform_filter if @filter.present?
  transform_order if @order_by.present?

  @total = @model.count
  @model = @model.limit(@per_page)
  @model = @model.offset(@per_page * (@page - 1))

  OpenStruct.new(
    data: deep_pluck_to_structs(irep_node(model_name.pluralize, paginated: true)),
    total_count: @total,
    per_page: @per_page,
    page: @page
  )
end

#runObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/graphql/hydrate_query.rb', line 24

def run
  if @id
    @model = @model.where(id: @id)
    deep_pluck_to_structs(irep_node(model_name.singularize)).first
  else
    @model = @model.limit(@per_page)
    @model = @model.offset(@per_page * (@page - 1))

    transform_filter if @filter.present?
    transform_order if @order_by.present?

    deep_pluck_to_structs(irep_node(model_name.pluralize))
  end
end