class Graphlient::Client
Attributes
options[RW]
uri[RW]
Public Class Methods
new(url, options = {}) { |self| ... }
click to toggle source
# File lib/graphlient/client.rb, line 5 def initialize(url, options = {}, &_block) @url = url @options = options.dup yield self if block_given? end
Public Instance Methods
execute(query, variables = nil)
click to toggle source
# File lib/graphlient/client.rb, line 20 def execute(query, variables = nil) query_params = {} query_params[:context] = @options if @options query_params[:variables] = variables if variables query = client.parse(query) if query.is_a?(String) rc = client.query(query, **query_params) raise Graphlient::Errors::GraphQLError, rc if rc.errors.any? # see https://github.com/github/graphql-client/pull/132 # see https://github.com/exAspArk/graphql-errors/issues/2 raise Graphlient::Errors::ExecutionError, rc if errors_in_result?(rc) rc rescue GraphQL::Client::Error => e raise Graphlient::Errors::ClientError, e.message end
http(&block)
click to toggle source
# File lib/graphlient/client.rb, line 47 def http(&block) adapter_options = { headers: @options[:headers], http_options: @options[:http_options] } @http ||= http_adapter_class.new(@url, adapter_options, &block) end
http_adapter_class()
click to toggle source
# File lib/graphlient/client.rb, line 43 def http_adapter_class options[:http] || Adapters::HTTP::FaradayAdapter end
parse(query_str = nil, &block)
click to toggle source
# File lib/graphlient/client.rb, line 11 def parse(query_str = nil, &block) query_str ||= Graphlient::Query.new do instance_eval(&block) end client.parse(query_str.to_s) rescue GraphQL::Client::Error => e raise Graphlient::Errors::ClientError, e.message end
query(query_or_variables = nil, variables = nil, &block)
click to toggle source
# File lib/graphlient/client.rb, line 35 def query(query_or_variables = nil, variables = nil, &block) if block_given? execute(parse(&block), query_or_variables) else execute(query_or_variables, variables) end end
schema()
click to toggle source
# File lib/graphlient/client.rb, line 53 def schema @schema ||= Graphlient::Schema.new(http, schema_path) end
Private Instance Methods
client()
click to toggle source
# File lib/graphlient/client.rb, line 63 def client @client ||= GraphQL::Client.new(schema: schema.graphql_schema, execute: http).tap do |client| client.allow_dynamic_queries = @options.key?(:allow_dynamic_queries) ? options[:allow_dynamic_queries] : true end end
errors_in_result?(response)
click to toggle source
# File lib/graphlient/client.rb, line 69 def errors_in_result?(response) response.data && response.data.errors && response.data.errors.any? end
schema_path()
click to toggle source
# File lib/graphlient/client.rb, line 59 def schema_path return options[:schema_path].to_s if options[:schema_path] end