Module: GraphitiGql::PaginateExtras

Defined in:
lib/graphiti_gql/graphiti_hax.rb

Instance Method Summary collapse

Instance Method Details

#applyObject



253
254
255
256
257
258
# File 'lib/graphiti_gql/graphiti_hax.rb', line 253

def apply
  if query_hash[:reverse] && (before_cursor || after_cursor)
    raise ::GraphitiGql::Errors::UnsupportedLast
  end
  super
end

#offsetObject



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/graphiti_gql/graphiti_hax.rb', line 260

def offset
  offset = 0

  if (value = page_param[:offset])
    offset = value.to_i
  end

  if before_cursor&.key?(:offset)
    if page_param.key?(:number)
      raise Errors::UnsupportedBeforeCursor
    end

    offset = before_cursor[:offset] - (size * number) - 1
    offset = 0 if offset.negative?
  end

  if after_cursor&.key?(:offset)
    offset = after_cursor[:offset]
  end

  offset
end

#sizeObject

TODO memoize



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/graphiti_gql/graphiti_hax.rb', line 284

def size
  size = super
  if before_cursor && after_cursor
    diff = before_cursor[:offset] - after_cursor[:offset] - 1
    size = [size, diff].min
  elsif before_cursor
    comparator = query_hash[:reverse] ? :>= : :<=
    if before_cursor[:offset].send(comparator, size)
      diff = before_cursor[:offset] - size
      size = [size, diff].min
      size = 1 if size.zero?
    end
  end
  size
end