Class: Graphlient::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/graphlient/client.rb

Defined Under Namespace

Classes: InvalidConfigurationError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, options = {}) {|_self| ... } ⇒ Client

Returns a new instance of Client.

Yields:

  • (_self)

Yield Parameters:



7
8
9
10
11
12
# File 'lib/graphlient/client.rb', line 7

def initialize(url, options = {}, &_block)
  @url = url
  @options = options.dup
  raise_error_if_invalid_configuration!
  yield self if block_given?
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/graphlient/client.rb', line 3

def options
  @options
end

#uriObject

Returns the value of attribute uri.



3
4
5
# File 'lib/graphlient/client.rb', line 3

def uri
  @uri
end

Instance Method Details

#execute(query, variables = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/graphlient/client.rb', line 29

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-community-projects/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) ⇒ Object



57
58
59
60
61
# File 'lib/graphlient/client.rb', line 57

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_classObject



53
54
55
# File 'lib/graphlient/client.rb', line 53

def http_adapter_class
  options[:http] || Adapters::HTTP::FaradayAdapter
end

#parse(query_str = nil, &block) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/graphlient/client.rb', line 14

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) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/graphlient/client.rb', line 45

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

#scalar(sym, graphql_type) ⇒ Object

Register a custom scalar type for use in variable declarations. Call inside the client initialiser block:

client = Graphlient::Client.new(url) do |c|
c.scalar :date,    'Date'
c.scalar :uuid,    'UUID'
c.scalar :decimal, 'Decimal'
end


70
71
72
# File 'lib/graphlient/client.rb', line 70

def scalar(sym, graphql_type)
  Query.scalar(sym, graphql_type)
end

#schemaObject



74
75
76
# File 'lib/graphlient/client.rb', line 74

def schema
  @schema ||= options[:schema] || Graphlient::Schema.new(http, schema_path)
end

#to_query_string(**_kargs, &block) ⇒ Object



23
24
25
26
27
# File 'lib/graphlient/client.rb', line 23

def to_query_string(**_kargs, &block)
  Graphlient::Query.new do
    instance_eval(&block)
  end.to_s
end