class GraphQL::Relay::Walker::Queue

Attributes

max_size[RW]
queue[R]
random_idx[RW]
seen[R]

Public Class Methods

new(max_size: nil, random_idx: false) click to toggle source

Initialize a new Queue.

max_size: - The maximum size the queue can grow to. This helps when

walking a large graph by forcing us to walk deeper.

random_idx: - Add frames to the queue at random indicies. This helps when

walking a large graph by forcing us to walk deeper.

Returns nothing.

# File lib/graphql/relay/walker/queue.rb, line 14
def initialize(max_size: nil, random_idx: false)
  @max_size = max_size
  @random_idx = random_idx

  @queue = []
  @seen = Set.new
end

Public Instance Methods

add(frame) click to toggle source

Add a frame to the queue if its GID hasn't been seen already and the queue hasn't exceeded its max size.

frame - The Frame to add to the queue.

Returns true if the frame was added, false otherwise.

# File lib/graphql/relay/walker/queue.rb, line 28
def add(frame)
  return false if max_size && queue.length >= max_size
  return false if seen.include?(frame.gid)

  seen.add(frame.gid)
  idx = random_idx ? rand(queue.length + 1) : queue.length
  queue.insert(idx, frame)

  true
end
add_gid(gid, parent = nil) click to toggle source

Add a GID to the queue.

gid - The String GID to add to the queue. parent - The frame where this GID was discovered (optional).

Returns true if a frame was added, false otherwise.

# File lib/graphql/relay/walker/queue.rb, line 45
def add_gid(gid, parent = nil)
  frame = Frame.new(self, gid, parent)
  add(frame)
end
each_frame() { |frame| ... } click to toggle source

Iterate through the queue, yielding each frame.

Returns nothing.

# File lib/graphql/relay/walker/queue.rb, line 53
def each_frame
  return enum_for(:each_frame) unless block_given?

  while frame = queue.shift
    yield(frame)
  end
end