Class: Graphlient::Adapters::HTTP::HTTPAdapter

Inherits:
Adapter
  • Object
show all
Defined in:
lib/graphlient/adapters/http/http_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
# File 'lib/graphlient/adapters/http/http_adapter.rb', line 29

def connection
  Net::HTTP.new(uri.host, uri.port).tap do |client|
    client.use_ssl = uri.scheme == 'https'

    configure_http_options(client)
  end
end

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



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

def execute(document:, operation_name: nil, variables: {}, context: {})
  request = Net::HTTP::Post.new(url)

  request['Accept'] = 'application/json'
  request['Content-Type'] = 'application/json'
  headers && headers.each { |name, value| request[name] = value }

  body = {}
  body['query'] = document.to_query_string
  body['variables'] = variables if variables.any?
  body['operationName'] = operation_name if operation_name
  request.body = JSON.generate(body)

  response = connection.request(request)
  raise Graphlient::Errors::HttpServerError, response unless response.is_a?(Net::HTTPOK)
  JSON.parse(response.body)
end

#uriObject



25
26
27
# File 'lib/graphlient/adapters/http/http_adapter.rb', line 25

def uri
  @uri ||= URI(url)
end