Class: ElasticGraph::Rack::GraphQLEndpoint
- Inherits:
-
Object
- Object
- ElasticGraph::Rack::GraphQLEndpoint
- Defined in:
- lib/elastic_graph/rack/graphql_endpoint.rb
Overview
A simple Rack wrapper around an ElasticGraph endpoint. This can be used for local development, or mounted in a Rails app, or run in any other rack-compatible context.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(graphql) ⇒ GraphQLEndpoint
constructor
A new instance of GraphQLEndpoint.
Constructor Details
#initialize(graphql) ⇒ GraphQLEndpoint
Returns a new instance of GraphQLEndpoint.
17 18 19 20 |
# File 'lib/elastic_graph/rack/graphql_endpoint.rb', line 17 def initialize(graphql) @logger = graphql.logger @graphql_http_endpoint = graphql.graphql_http_endpoint end |
Instance Method Details
#call(env) ⇒ Object
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 47 48 49 |
# File 'lib/elastic_graph/rack/graphql_endpoint.rb', line 22 def call(env) rack_request = ::Rack::Request.new(env) # Rack doesn't provide a nice method to provide all HTTP headers. In general, # HTTP headers are prefixed with `HTTP_` as per https://stackoverflow.com/a/6318491/16481862, # but `Content-Type`, as a "standard" header, isn't exposed that way, sadly. headers = env .select { |k, v| k.start_with?("HTTP_") } .to_h { |k, v| [k.delete_prefix("HTTP_"), v] } .merge("Content-Type" => rack_request.content_type) request = GraphQL::HTTPRequest.new( http_method: rack_request.request_method.downcase.to_sym, url: rack_request.url, headers: headers, body: rack_request.body&.read ) response = @graphql_http_endpoint.process(request) [response.status_code, response.headers.transform_keys(&:downcase), [response.body]] rescue => e raise if ENV["RACK_ENV"] == "test" @logger.error "Got an exception: #{e.class.name}: #{e.message}\n\n#{e.backtrace.join("\n")}" error = {message: e., exception_class: e.class, backtrace: e.backtrace} [500, {"content-type" => "application/json"}, [::JSON.generate(errors: [error])]] end |