Class: GraphQL::Dataloader

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/dataloader.rb,
lib/graphql/dataloader/source.rb,
lib/graphql/dataloader/request.rb,
lib/graphql/dataloader/request_all.rb,
lib/graphql/dataloader/null_dataloader.rb

Overview

This plugin supports Fiber-based concurrency, along with Source.

Examples:

Installing Dataloader


class MySchema < GraphQL::Schema
  use GraphQL::Dataloader
end

Waiting for batch-loaded data in a GraphQL field


field :team, Types::Team, null: true

def team
  dataloader.with(Sources::Record, Team).load(object.team_id)
end

Direct Known Subclasses

NullDataloader

Defined Under Namespace

Classes: NullDataloader, Request, RequestAll, Source

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(multiplex_context) ⇒ Dataloader

Returns a new instance of Dataloader.



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/graphql/dataloader.rb', line 30

def initialize(multiplex_context)
  @context = multiplex_context
  @source_cache = Hash.new { |h, source_class| h[source_class] = Hash.new { |h2, batch_parameters|
      source = source_class.new(*batch_parameters)
      source.setup(self)
      h2[batch_parameters] = source
    }
  }
  @waiting_fibers = []
  @yielded_fibers = {}
end

Instance Attribute Details

#contextHash (readonly)

Returns the Multiplex context.

Returns:

  • (Hash)

    the Multiplex context



43
44
45
# File 'lib/graphql/dataloader.rb', line 43

def context
  @context
end

#current_runtimeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



144
145
146
# File 'lib/graphql/dataloader.rb', line 144

def current_runtime
  @current_runtime
end

#yielded_fibersObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



46
47
48
# File 'lib/graphql/dataloader.rb', line 46

def yielded_fibers
  @yielded_fibers
end

Class Method Details

.use(schema) ⇒ Object



26
27
28
# File 'lib/graphql/dataloader.rb', line 26

def self.use(schema)
  schema.dataloader_class = self
end

Instance Method Details

#enqueue(&block) ⇒ void

This method returns an undefined value.

Add some work to this dataloader to be scheduled later.

Parameters:

  • block

    Some work to enqueue



51
52
53
54
55
56
57
58
59
60
# File 'lib/graphql/dataloader.rb', line 51

def enqueue(&block)
  @waiting_fibers << Fiber.new {
    begin
      yield
    rescue StandardError => exception
      exception
    end
  }
  nil
end

#runvoid

This method returns an undefined value.

Run all Fibers until they’re all done

Each cycle works like this:

  • Run each pending execution fiber (@waiting_fibers),
  • Then run each pending Source, preparing more data for those fibers.
    • Run each pending Source again (if one Source requested more data from another Source)
    • Continue until there are no pending sources
  • Repeat: run execution fibers again …


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/graphql/dataloader.rb', line 89

def run
  # Start executing Fibers. This will run until all the Fibers are done.
  already_run_fibers = []
  while (current_fiber = @waiting_fibers.pop)
    # Run each execution fiber, enqueuing it in `already_run_fibers`
    # if it's still `.alive?`.
    # Any spin-off continuations will be enqueued in `@waiting_fibers` (via {#enqueue})
    resume_fiber_and_enqueue_continuation(current_fiber, already_run_fibers)

    if @waiting_fibers.empty?
      # Now, run all Sources which have become pending _before_ resuming GraphQL execution.
      # Sources might queue up other Sources, which is fine -- those will also run before resuming execution.
      #
      # This is where an evented approach would be even better -- can we tell which
      # fibers are ready to continue, and continue execution there?
      #
      source_fiber_stack = if (first_source_fiber = create_source_fiber)
        [first_source_fiber]
      else
        nil
      end

      if source_fiber_stack
        while (outer_source_fiber = source_fiber_stack.pop)
          resume_fiber_and_enqueue_continuation(outer_source_fiber, source_fiber_stack)

          # If this source caused more sources to become pending, run those before running this one again:
          next_source_fiber = create_source_fiber
          if next_source_fiber
            source_fiber_stack << next_source_fiber
          end
        end
      end

      # We ran all the first round of execution fibers,
      # and we ran all the pending sources.
      # So pick up any paused execution fibers and repeat.
      @waiting_fibers.concat(already_run_fibers)
      already_run_fibers.clear
    end
  end
  nil
end

#with(source_class, *batch_parameters) ⇒ GraphQL::Dataloader::Source

Get a Source instance from this dataloader, for calling .load(...) or .request(...) on.

Parameters:

  • source_class (Class<GraphQL::Dataloader::Source])

    ource_class [Class<GraphQL::Dataloader::Source]

  • batch_parameters (Array<Object>)

Returns:

  • (GraphQL::Dataloader::Source)

    An instance of source_class, initialized with self, *batch_parameters, and cached for the lifetime of this Multiplex.



139
140
141
# File 'lib/graphql/dataloader.rb', line 139

def with(source_class, *batch_parameters)
  @source_cache[source_class][batch_parameters]
end

#yieldvoid

This method returns an undefined value.

Tell the dataloader that this fiber is waiting for data.

Dataloader will resume the fiber after the requested data has been loaded (by another Fiber).



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

def yield
  Fiber.yield
  nil
end

#yielded?(path) ⇒ Boolean

Returns True if the current Fiber has yielded once via Dataloader at path.

Parameters:

  • path (Array<String, Integer>)

    A graphql response path

Returns:

  • (Boolean)

    True if the current Fiber has yielded once via Dataloader at path



74
75
76
# File 'lib/graphql/dataloader.rb', line 74

def yielded?(path)
  @yielded_fibers[Fiber.current] == path
end