Sha256: a04b3ca4696d6891d96e8f3f63641d0508b2ed67670b737f14bc4df1034eb181

Contents?: true

Size: 1.13 KB

Versions: 2

Compression:

Stored size: 1.13 KB

Contents

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::ServerError.new("the server responded with status #{response.code}", 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'
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
graphlient-0.3.1 lib/graphlient/adapters/http/http_adapter.rb
graphlient-0.3.0 lib/graphlient/adapters/http/http_adapter.rb