File: http_adapter.rb

package info (click to toggle)
ruby-graphlient 0.5.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 2,648 kB
  • sloc: ruby: 1,288; makefile: 4
file content (39 lines) | stat: -rw-r--r-- 1,148 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
require 'graphql/client/http'

module Graphlient
  module Adapters
    module HTTP
      class HTTPAdapter < Adapter
        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

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

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

            configure_http_options(client)
          end
        end
      end
    end
  end
end