Class: GraphQL::RemoteLoader::Loader
- Inherits:
-
Batch::Loader
- Object
- Batch::Loader
- GraphQL::RemoteLoader::Loader
- Defined in:
- lib/graphql/remote_loader/loader.rb
Class Method Summary collapse
-
.load(query, context: {}, variables: {}) ⇒ Object
Delegates to GraphQL::Batch::Loader#load We include a unique id as part of the batch key to use as part of the alias on all fields.
-
.load_on_relay_node(node_id, type, selections, context: {}) ⇒ Object
Shorthand helper method for #load calls selecting fields off of a ‘Node` type E.g.
-
.load_value(*path, context: {}) ⇒ Object
Loads the value, then if the query was successful, fulfills promise with the leaf value instead of the full results hash.
- .reset_index ⇒ Object
- .store_context(context) ⇒ Object
Instance Method Summary collapse
-
#query(query_string) ⇒ Object
Given a query string, return a response JSON.
Class Method Details
.load(query, context: {}, variables: {}) ⇒ Object
Delegates to GraphQL::Batch::Loader#load We include a unique id as part of the batch key to use as part of the alias on all fields. This is used to a) Avoid name collisions in the generated query b) Determine which fields in the result JSON should be
handed fulfilled to each promise
15 16 17 18 19 20 21 22 |
# File 'lib/graphql/remote_loader/loader.rb', line 15 def self.load(query, context: {}, variables: {}) @index ||= 0 @index += 1 store_context(context) self.for.load([interpolate_variables(query, variables), @index, @context]) end |
.load_on_relay_node(node_id, type, selections, context: {}) ⇒ Object
Shorthand helper method for #load calls selecting fields off of a ‘Node` type E.g. `load_on_relay_node(“nodeid”, “Type”, “friends(first: 5) { totalCount }”)` is identical to load(“node(id: ”nodeid“) { … on Type { friends(first: 5) { totalCount } } }”)
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/graphql/remote_loader/loader.rb', line 40 def self.load_on_relay_node(node_id, type, selections, context: {}) query = <<-GRAPHQL node(id: \"#{node_id}\") { ... on #{type} { #{selections} } } GRAPHQL load(query, context: context) end |
.load_value(*path, context: {}) ⇒ Object
Loads the value, then if the query was successful, fulfills promise with the leaf value instead of the full results hash.
If errors are present, returns nil.
28 29 30 31 32 33 34 |
# File 'lib/graphql/remote_loader/loader.rb', line 28 def self.load_value(*path, context: {}) load(query_from_path(path), context: context).then do |results| next nil if results["errors"] && !results["errors"].empty? value_from_hash(results["data"]) end end |
.reset_index ⇒ Object
52 53 54 |
# File 'lib/graphql/remote_loader/loader.rb', line 52 def self.reset_index @index = nil end |
.store_context(context) ⇒ Object
56 57 58 59 |
# File 'lib/graphql/remote_loader/loader.rb', line 56 def self.store_context(context) @context ||= {} @context.merge!(context.to_h) end |
Instance Method Details
#query(query_string) ⇒ Object
Given a query string, return a response JSON
62 63 64 65 |
# File 'lib/graphql/remote_loader/loader.rb', line 62 def query(query_string) raise NotImplementedError, "RemoteLoader::Loader should be subclassed and #query must be defined" end |