Class: GraphQL::Client::HTTP

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, &block) ⇒ HTTP

GraphQL::Client::HTTP.new(“graphql-swapi.parseapp.com/”)



11
12
13
14
# File 'lib/graphql/client/http.rb', line 11

def initialize(uri, &block)
  @uri = ::URI.parse(uri)
  class_eval(&block) if block_given?
end

Instance Attribute Details

#uriObject (readonly)

Returns the value of attribute uri.



8
9
10
# File 'lib/graphql/client/http.rb', line 8

def uri
  @uri
end

Instance Method Details

#call(query) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/graphql/client/http.rb', line 20

def call(query)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == "https"

  request = Net::HTTP::Post.new(uri.request_uri)

  request["Accept"] = "application/json"
  request["Content-Type"] = "application/json"
  headers(query).each { |name, value| request[name] = value }

  body = {}
  body["query"] = query.to_s
  body["variables"] = JSON.generate(query.variables) if query.variables.any?
  body["operationName"] = query.operation_name if query.operation_name
  request.body = JSON.generate(body)

  response = http.request(request)
  JSON.parse(response.body)
end

#headers(query) ⇒ Object



16
17
18
# File 'lib/graphql/client/http.rb', line 16

def headers(query)
  {}
end