-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
71 lines (60 loc) · 1.44 KB
/
app.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
require "sinatra/base"
require "graphql"
module Messages
def messages
[{message: "You are all breathtaking!"}]
end
module Mutations
def sendMessage
{message: "No! YOU are breathtaking!"}
end
end
end
module Contacts
def contacts
[{name: "Keanu Reeves"}]
end
end
class Query
include Messages
include Contacts
def viewer
nil
end
end
class Mutation
include Messages::Mutations
end
module TestAPI
module Resolver
def self.call(type, field, obj, args, ctx)
s = field.name.to_sym
case type.to_s
when 'Mutation'
x = Mutation.new
x.public_send(s)
when 'Query'
x = Query.new
x.public_send(s)
else
return obj[s] if obj.is_a? Hash
obj.public_send(s)
end
end
end
class App < Sinatra::Base
post "/graphql" do
body = JSON.parse(request.body.read)
variables = body["variables"]
query = body["query"]
operation_name = body["operationName"]
context = {}
schema = Dir["#{File.dirname(__FILE__)}/protocols/**/*.graphql","#{File.dirname(__FILE__)}/graphql/schema.graphql"].map { |x| File.read(x) }.join
puts schema
built_schema = GraphQL::Schema.from_definition(schema, default_resolve: Resolver)
result = built_schema.execute(query, variables: variables, context: context, operation_name: operation_name)
content_type :json
result.to_json
end
end
end