-
Notifications
You must be signed in to change notification settings - Fork 3
/
gateway.rb
49 lines (42 loc) · 1.27 KB
/
gateway.rb
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
47
48
49
# frozen_string_literal: true
require 'rackup'
require 'json'
require 'graphql'
require_relative '../../lib/graphql/stitching'
require_relative '../../test/schemas/example'
class StitchedApp
def initialize
file = File.open("#{__dir__}/graphiql.html")
@graphiql = file.read
file.close
@client = GraphQL::Stitching::Client.new(locations: {
products: {
schema: Schemas::Example::Products,
},
storefronts: {
schema: Schemas::Example::Storefronts,
executable: GraphQL::Stitching::HttpExecutable.new(url: "http://localhost:3001"),
},
manufacturers: {
schema: Schemas::Example::Manufacturers,
executable: GraphQL::Stitching::HttpExecutable.new(url: "http://localhost:3002"),
}
})
end
def call(env)
req = Rack::Request.new(env)
case req.path_info
when /graphql/
params = JSON.parse(req.body.read)
result = @client.execute(
query: params["query"],
variables: params["variables"],
operation_name: params["operationName"],
)
[200, {"content-type" => "application/json"}, [JSON.generate(result)]]
else
[200, {"content-type" => "text/html"}, [@graphiql]]
end
end
end
Rackup::Handler.default.run(StitchedApp.new, :Port => 3000)