Class: GraphQL::Stitching::Client
- Inherits:
-
Object
- Object
- GraphQL::Stitching::Client
- Defined in:
- lib/graphql/stitching/client.rb
Defined Under Namespace
Classes: ClientError
Instance Attribute Summary collapse
-
#supergraph ⇒ Object
readonly
Returns the value of attribute supergraph.
Instance Method Summary collapse
- #execute(query:, variables: nil, operation_name: nil, context: nil, validate: true) ⇒ Object
-
#initialize(locations: nil, supergraph: nil, composer: nil) ⇒ Client
constructor
A new instance of Client.
- #on_cache_read(&block) ⇒ Object
- #on_cache_write(&block) ⇒ Object
- #on_error(&block) ⇒ Object
Constructor Details
#initialize(locations: nil, supergraph: nil, composer: nil) ⇒ Client
Returns a new instance of Client.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/graphql/stitching/client.rb', line 12 def initialize(locations: nil, supergraph: nil, composer: nil) @supergraph = if locations && supergraph raise ClientError, "Cannot provide both locations and a supergraph." elsif supergraph && !supergraph.is_a?(GraphQL::Stitching::Supergraph) raise ClientError, "Provided supergraph must be a GraphQL::Stitching::Supergraph instance." elsif supergraph supergraph else composer ||= GraphQL::Stitching::Composer.new composer.perform(locations) end @on_cache_read = nil @on_cache_write = nil @on_error = nil end |
Instance Attribute Details
#supergraph ⇒ Object (readonly)
Returns the value of attribute supergraph.
10 11 12 |
# File 'lib/graphql/stitching/client.rb', line 10 def supergraph @supergraph end |
Instance Method Details
#execute(query:, variables: nil, operation_name: nil, context: nil, validate: true) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/graphql/stitching/client.rb', line 29 def execute(query:, variables: nil, operation_name: nil, context: nil, validate: true) request = GraphQL::Stitching::Request.new( query, operation_name: operation_name, variables: variables, context: context, ) if validate validation_errors = @supergraph.schema.validate(request.document) return error_result(validation_errors) if validation_errors.any? end request.prepare! plan = fetch_plan(request) do GraphQL::Stitching::Planner.new( supergraph: @supergraph, request: request, ).perform end GraphQL::Stitching::Executor.new( supergraph: @supergraph, request: request, plan: plan, ).perform rescue GraphQL::ParseError, GraphQL::ExecutionError => e error_result([e]) rescue StandardError => e = @on_error.call(request, e) if @on_error error_result([{ "message" => || "An unexpected error occured." }]) end |
#on_cache_read(&block) ⇒ Object
63 64 65 66 |
# File 'lib/graphql/stitching/client.rb', line 63 def on_cache_read(&block) raise ClientError, "A cache read block is required." unless block_given? @on_cache_read = block end |
#on_cache_write(&block) ⇒ Object
68 69 70 71 |
# File 'lib/graphql/stitching/client.rb', line 68 def on_cache_write(&block) raise ClientError, "A cache write block is required." unless block_given? @on_cache_write = block end |
#on_error(&block) ⇒ Object
73 74 75 76 |
# File 'lib/graphql/stitching/client.rb', line 73 def on_error(&block) raise ClientError, "An error handler block is required." unless block_given? @on_error = block end |