Class: GraphQL::FancyLoader::QueryGenerator

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

Instance Method Summary collapse

Constructor Details

#initialize(model:, find_by:, sort:, keys:, before: nil, after: 0, first: nil, last: nil, where: nil, context: {}, modify_query: nil) ⇒ QueryGenerator

Returns a new instance of QueryGenerator.

Parameters:

  • model (ActiveRecord::Model)

    the model to load from

  • find_by (Symbol, String, Array<Symbol, String>)

    the key or keys to find by

  • sort (Array<{:column, :transform, :direction => Object}>)

    The sorts to apply

  • keys (Array)

    an array of values to find by

  • before (Integer) (defaults to: nil)

    Filter by rows less than this (one-indexed)

  • after (Integer) (defaults to: 0)

    Filter by rows greater than this (one-indexed)

  • first (Integer) (defaults to: nil)

    Filter for first N rows

  • last (Integer) (defaults to: nil)

    Filter for last N rows

  • where (Hash) (defaults to: nil)

    a filter to use when querying

  • context (Context) (defaults to: {})

    The context of the graphql query. Can be used inside of modify_query.

  • modify_query (Lambda) (defaults to: nil)

    An escape hatch to FancyLoader to allow modifying the base_query before it generates the rest of the query



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/graphql/fancy_loader/query_generator.rb', line 17

def initialize(
  model:, find_by:, sort:, keys:,
  before: nil, after: 0, first: nil, last: nil,
  where: nil, context: {}, modify_query: nil
)
  @model = model
  @find_by = find_by
  @sort = sort
  @keys = keys
  @before = before
  @after = after
  @first = first
  @last = last
  @where = where
  @context = context
  @modify_query = modify_query
end

Instance Method Details

#queryObject



35
36
37
38
39
40
41
42
# File 'lib/graphql/fancy_loader/query_generator.rb', line 35

def query
  # Finally, go *back* to the ActiveRecord model, and do the final select
  @model.unscoped
        .select(Arel.star)
        .from(subquery)
        .where(pagination_filter(subquery))
        .order(subquery[:row_number].asc)
end