File: faraday_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 (46 lines) | stat: -rw-r--r-- 1,369 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
40
41
42
43
44
45
46
require 'faraday'
require 'faraday_middleware'

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

        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
      end
    end
  end
end