11# frozen_string_literal: true
2+ # typed: true
23
34require "json"
45
56module GraphQL
67 module Stitching
7- # Client is an out-of-the-box helper that assembles all
8- # stitching components into a workflow that executes requests.
98 class Client
109 class << self
10+ #: (String | singleton(GraphQL::Schema) schema, executables: Hash[Location | Symbol, Executable]) -> Client
1111 def from_definition ( schema , executables :)
1212 new ( supergraph : Supergraph . from_definition ( schema , executables : executables ) )
1313 end
1414 end
1515
16- # @return [ Supergraph] composed supergraph that services incoming requests.
16+ #: Supergraph
1717 attr_reader :supergraph
1818
19- # Builds a new client instance. Either `supergraph` or `locations` configuration is required.
20- # @param supergraph [Supergraph] optional, a pre-composed supergraph that bypasses composer setup.
21- # @param locations [Hash<Symbol, Hash<Symbol, untyped>>] optional, composer configurations for each graph location.
22- # @param composer_options [Hash] optional, composer options for configuring composition.
19+ #: (?locations: untyped, ?supergraph: Supergraph?, ?composer_options: Hash[Symbol, untyped]) -> void
2320 def initialize ( locations : nil , supergraph : nil , composer_options : { } )
2421 @supergraph = if locations && supergraph
2522 raise ArgumentError , "Cannot provide both locations and a supergraph."
@@ -34,15 +31,26 @@ def initialize(locations: nil, supergraph: nil, composer_options: {})
3431 composer . perform ( locations )
3532 end
3633
37- @on_cache_read = nil
38- @on_cache_write = nil
39- @on_error = nil
34+ @on_cache_read = nil #: CacheReadHandler?
35+ @on_cache_write = nil #: CacheWriteHandler?
36+ @on_error = nil #: ErrorHandler?
4037 end
4138
39+ #: (
40+ #| ?String | DocumentNode | nil raw_query,
41+ #| ?query: String | DocumentNode | nil,
42+ #| ?variables: Variables?,
43+ #| ?operation_name: String?,
44+ #| ?context: untyped,
45+ #| ?validate: bool
46+ #| ) -> untyped
4247 def execute ( raw_query = nil , query : nil , variables : nil , operation_name : nil , context : nil , validate : true )
48+ source = raw_query || query
49+ raise ArgumentError , "A query string or document is required." unless source
50+
4351 request = Request . new (
4452 @supergraph ,
45- raw_query || query , # << for parity with GraphQL Ruby Schema.execute
53+ source ,
4654 operation_name : operation_name ,
4755 variables : variables ,
4856 context : context ,
@@ -62,23 +70,27 @@ def execute(raw_query = nil, query: nil, variables: nil, operation_name: nil, co
6270 error_result ( request , [ { "message" => custom_message || "An unexpected error occured." } ] )
6371 end
6472
73+ #: ?{ (Request) -> String? } -> CacheReadHandler
6574 def on_cache_read ( &block )
66- raise ArgumentError , "A cache read block is required." unless block_given?
75+ raise ArgumentError , "A cache read block is required." unless block
6776 @on_cache_read = block
6877 end
6978
79+ #: ?{ (Request, String) -> void } -> CacheWriteHandler
7080 def on_cache_write ( &block )
71- raise ArgumentError , "A cache write block is required." unless block_given?
81+ raise ArgumentError , "A cache write block is required." unless block
7282 @on_cache_write = block
7383 end
7484
85+ #: ?{ (Request?, StandardError) -> String? } -> ErrorHandler
7586 def on_error ( &block )
76- raise ArgumentError , "An error handler block is required." unless block_given?
87+ raise ArgumentError , "An error handler block is required." unless block
7788 @on_error = block
7889 end
7990
8091 private
8192
93+ #: (Request request) -> Plan
8294 def load_plan ( request )
8395 if @on_cache_read && plan_json = @on_cache_read . call ( request )
8496 plan = Plan . from_json ( JSON . parse ( plan_json ) )
@@ -98,6 +110,7 @@ def load_plan(request)
98110 plan
99111 end
100112
113+ #: (Request? request, Array[PublicErrorObject | PublicError] errors) -> GraphQL::Query::Result
101114 def error_result ( request , errors )
102115 public_errors = errors . map! do |e |
103116 e . is_a? ( Hash ) ? e : e . to_h
0 commit comments