Class: GraphQL::Client::HTTP
- Inherits:
-
Object
- Object
- GraphQL::Client::HTTP
- Defined in:
- lib/graphql/client/http.rb
Overview
Public: Basic HTTP network adapter.
GraphQL::Client.new(
execute: GraphQL::Client::HTTP.new("http://graphql-swapi.parseapp.com/")
)
Assumes GraphQL endpoint follows the express-graphql endpoint conventions.
https://github.com/graphql/express-graphql#http-usage
Production applications should consider implementing their own network adapter. This class exists for trivial stock usage and allows for minimal request header configuration.
Instance Attribute Summary collapse
-
#last_response ⇒ Object
readonly
Public: full reponse from last request.
-
#uri ⇒ Object
readonly
Public: Parsed endpoint URI.
Instance Method Summary collapse
-
#connection ⇒ Object
Public: Extension point for subclasses to customize the Net:HTTP client.
-
#execute(document:, operation_name: nil, variables: {}, context: {}) ⇒ Object
Public: Make an HTTP request for GraphQL query.
-
#headers(_context) ⇒ Object
Public: Extension point for subclasses to set custom request headers.
-
#initialize(uri, &block) ⇒ HTTP
constructor
Public: Create HTTP adapter instance for a single GraphQL endpoint.
Constructor Details
#initialize(uri, &block) ⇒ HTTP
31 32 33 34 |
# File 'lib/graphql/client/http.rb', line 31 def initialize(uri, &block) @uri = URI.parse(uri) singleton_class.class_eval(&block) if block_given? end |
Instance Attribute Details
#last_response ⇒ Object (readonly)
Public: full reponse from last request
Returns Hash.
51 52 53 |
# File 'lib/graphql/client/http.rb', line 51 def last_response @last_response end |
#uri ⇒ Object (readonly)
Public: Parsed endpoint URI
Returns URI.
39 40 41 |
# File 'lib/graphql/client/http.rb', line 39 def uri @uri end |
Instance Method Details
#connection ⇒ Object
Public: Extension point for subclasses to customize the Net:HTTP client
Returns a Net::HTTP object
91 92 93 94 95 |
# File 'lib/graphql/client/http.rb', line 91 def connection Net::HTTP.new(uri.host, uri.port).tap do |client| client.use_ssl = uri.scheme == "https" end end |
#execute(document:, operation_name: nil, variables: {}, context: {}) ⇒ Object
Public: Make an HTTP request for GraphQL query.
Implements Client’s “execute” adapter interface.
document - The Query GraphQL::Language::Nodes::Document operation_name - The String operation definition name variables - Hash of query variables context - An arbitrary Hash of values which you can access
Returns { “data” => … , “errors” => … } Hash.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/graphql/client/http.rb', line 63 def execute(document:, operation_name: nil, variables: {}, context: {}) request = Net::HTTP::Post.new(uri.request_uri) request.basic_auth(uri.user, uri.password) if uri.user || uri.password request["Accept"] = "application/json" request["Content-Type"] = "application/json" headers(context).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) @last_response = response.to_hash case response when Net::HTTPOK, Net::HTTPBadRequest JSON.parse(response.body) else { "errors" => [{ "message" => "#{response.code} #{response.}" }] } end end |
#headers(_context) ⇒ Object
Public: Extension point for subclasses to set custom request headers.
Returns Hash of String header names and values.
44 45 46 |
# File 'lib/graphql/client/http.rb', line 44 def headers(_context) {} end |