Class: GraphQL::Stitching::Client
- Inherits:
-
Object
- Object
- GraphQL::Stitching::Client
- Defined in:
- lib/graphql/stitching/client.rb
Overview
Client is an out-of-the-box helper that assembles all stitching components into a workflow that executes requests.
Instance Attribute Summary collapse
-
#supergraph ⇒ Supergraph
readonly
Composed supergraph that services incoming requests.
Class Method Summary collapse
Instance Method Summary collapse
- #execute(raw_query = nil, query: nil, variables: nil, operation_name: nil, context: nil, validate: true) ⇒ Object
-
#initialize(locations: nil, supergraph: nil, composer_options: {}) ⇒ Client
constructor
Builds a new client instance.
- #on_cache_read(&block) ⇒ Object
- #on_cache_write(&block) ⇒ Object
- #on_error(&block) ⇒ Object
Constructor Details
#initialize(locations: nil, supergraph: nil, composer_options: {}) ⇒ Client
Builds a new client instance. Either supergraph
or locations
configuration is required.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/graphql/stitching/client.rb', line 23 def initialize(locations: nil, supergraph: nil, composer_options: {}) @supergraph = if locations && supergraph raise ArgumentError, "Cannot provide both locations and a supergraph." elsif supergraph && !supergraph.is_a?(Supergraph) raise ArgumentError, "Provided supergraph must be a GraphQL::Stitching::Supergraph instance." elsif supergraph && .any? raise ArgumentError, "Cannot provide composer options with a pre-built supergraph." elsif supergraph supergraph else composer = Composer.new(**) composer.perform(locations) end @on_cache_read = nil @on_cache_write = nil @on_error = nil end |
Instance Attribute Details
#supergraph ⇒ Supergraph (readonly)
Returns composed supergraph that services incoming requests.
17 18 19 |
# File 'lib/graphql/stitching/client.rb', line 17 def supergraph @supergraph end |
Class Method Details
.from_definition(schema, executables:) ⇒ Object
11 12 13 |
# File 'lib/graphql/stitching/client.rb', line 11 def from_definition(schema, executables:) new(supergraph: Supergraph.from_definition(schema, executables: executables)) end |
Instance Method Details
#execute(raw_query = nil, query: nil, variables: nil, operation_name: nil, context: nil, validate: true) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/graphql/stitching/client.rb', line 42 def execute(raw_query = nil, query: nil, variables: nil, operation_name: nil, context: nil, validate: true) request = Request.new( @supergraph, raw_query || query, # << for parity with GraphQL Ruby Schema.execute operation_name: operation_name, variables: variables, context: context, ) if validate validation_errors = request.validate return error_result(request, validation_errors) if validation_errors.any? end load_plan(request) request.execute rescue GraphQL::ParseError, GraphQL::ExecutionError => e error_result(request, [e]) rescue StandardError => e = @on_error.call(request, e) if @on_error error_result(request, [{ "message" => || "An unexpected error occured." }]) end |
#on_cache_read(&block) ⇒ Object
65 66 67 68 |
# File 'lib/graphql/stitching/client.rb', line 65 def on_cache_read(&block) raise ArgumentError, "A cache read block is required." unless block_given? @on_cache_read = block end |
#on_cache_write(&block) ⇒ Object
70 71 72 73 |
# File 'lib/graphql/stitching/client.rb', line 70 def on_cache_write(&block) raise ArgumentError, "A cache write block is required." unless block_given? @on_cache_write = block end |
#on_error(&block) ⇒ Object
75 76 77 78 |
# File 'lib/graphql/stitching/client.rb', line 75 def on_error(&block) raise ArgumentError, "An error handler block is required." unless block_given? @on_error = block end |