Class: GraphQL::FancyConnection

Inherits:
Pagination::RelationConnection
  • Object
show all
Defined in:
lib/graphql/fancy_connection.rb

Instance Method Summary collapse

Constructor Details

#initialize(loader, args, key, **super_args) ⇒ FancyConnection

Returns a new instance of FancyConnection.



3
4
5
6
7
8
9
10
# File 'lib/graphql/fancy_connection.rb', line 3

def initialize(loader, args, key, **super_args)
  @loader = loader
  @args = args
  @key = key
  @then = nil

  super(nil, **super_args)
end

Instance Method Details

#cursor_for(item) ⇒ Object



72
73
74
# File 'lib/graphql/fancy_connection.rb', line 72

def cursor_for(item)
  item && encode(item.attributes['row_number'].to_s)
end

#edgesObject



21
22
23
24
25
# File 'lib/graphql/fancy_connection.rb', line 21

def edges
  @edges ||= nodes.then do |nodes|
    nodes.map { |n| @edge_class.new(n, self) }
  end
end

#end_cursorObject



66
67
68
69
70
# File 'lib/graphql/fancy_connection.rb', line 66

def end_cursor
  base_nodes.then do |results|
    cursor_for(results.last)
  end
end

#has_next_pagePromise<Boolean>

Returns:

  • (Promise<Boolean>)


39
40
41
42
43
44
45
46
47
# File 'lib/graphql/fancy_connection.rb', line 39

def has_next_page # rubocop:disable Naming/PredicateName
  base_nodes.then do |results|
    if results.last
      results.last.attributes['row_number'] < results.last.attributes['total_count']
    else
      false
    end
  end
end

#has_previous_pagePromise<Boolean>

Returns:

  • (Promise<Boolean>)


50
51
52
53
54
55
56
57
58
# File 'lib/graphql/fancy_connection.rb', line 50

def has_previous_page # rubocop:disable Naming/PredicateName
  base_nodes.then do |results|
    if results.first
      results.first.attributes['row_number'] > 1
    else
      false
    end
  end
end

#nodesPromise<Array<ApplicationRecord>>

Returns:

  • (Promise<Array<ApplicationRecord>>)


13
14
15
16
17
18
19
# File 'lib/graphql/fancy_connection.rb', line 13

def nodes
  if @then
    base_nodes.then(@then)
  else
    base_nodes
  end
end

#start_cursorObject



60
61
62
63
64
# File 'lib/graphql/fancy_connection.rb', line 60

def start_cursor
  base_nodes.then do |results|
    cursor_for(results.first)
  end
end

#then(&block) ⇒ Object



76
77
78
79
# File 'lib/graphql/fancy_connection.rb', line 76

def then(&block)
  @then = block
  self
end

#total_countPromise<Integer>

Returns:

  • (Promise<Integer>)


28
29
30
31
32
33
34
35
36
# File 'lib/graphql/fancy_connection.rb', line 28

def total_count
  base_nodes.then do |results|
    if results.first
      results.first.attributes['total_count']
    else
      0
    end
  end
end