Class: Graphlient::Adapters::HTTP::FaradayAdapter

Inherits:
Adapter
  • Object
show all
Defined in:
lib/graphlient/adapters/http/faraday_adapter.rb

Instance Attribute Summary

Attributes inherited from Adapter

#options, #url

Instance Method Summary collapse

Methods inherited from Adapter

#headers, #http_options, #initialize

Constructor Details

This class inherits a constructor from Graphlient::Adapters::HTTP::Adapter

Instance Method Details

#connectionObject



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

def connection
  @connection ||= Faraday.new(url: url, headers: headers) do |c|
    c.use Faraday::Response::RaiseError
    c.request :json
    c.response :json

    configure_http_options(c.options)

    if block_given?
      yield c
    else
      c.adapter Faraday::Adapter::NetHttp
    end
  end
end

#execute(document:, operation_name:, variables:, context:) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/graphlient/adapters/http/faraday_adapter.rb', line 8

def execute(document:, operation_name:, variables:, context:)
  response = connection.post do |req|
    req.headers.merge!(context[:headers] || {})
    req.body = {
      query: document.to_query_string,
      operationName: operation_name,
      variables: variables
    }.to_json
  end

  parse_body(response.body)
rescue Faraday::ConnectionFailed => e
  raise Graphlient::Errors::ConnectionFailedError, e
rescue Faraday::TimeoutError => e
  raise Graphlient::Errors::TimeoutError, e
rescue Faraday::ClientError => e
  raise Graphlient::Errors::FaradayServerError, e
rescue Faraday::ServerError => e
  raise Graphlient::Errors::FaradayServerError, e
end