-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathapp.rb
More file actions
executable file
·68 lines (53 loc) · 1.29 KB
/
app.rb
File metadata and controls
executable file
·68 lines (53 loc) · 1.29 KB
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
#! /usr/bin/env ruby
require 'bundler'
Bundler.require
$: << File.expand_path('../', __FILE__)
database_url = ENV['DATABASE_URL'] ||
'postgres://localhost/ruby_graphql_server_example'
set :database, database_url
require 'json'
require 'logger'
require 'app/models'
require 'app/schema'
configure :development, :staging do
database.loggers << Logger.new(STDOUT)
end
before do
content_type :json
end
get '/' do
content_type :html
erb :graphiql
end
get '/graphql' do
query = params['query']
variables = params['variables']
result = Schema.execute(query, variables: variables)
result.to_json
end
post '/graphql' do
pass unless request.content_type =~ /application\/graphql/
query = request.body.read
result = Schema.execute(query)
result.to_json
end
post '/graphql' do
params = JSON.parse(request.body.read)
query = params['query']
variables = params['variables']
result = Schema.execute(query, variables: variables)
result.to_json
end
def respond_with_error(status_code, message)
status status_code
{ error: { message: message, status: status } }.to_json
end
not_found do
respond_with_error(404, 'Not Found')
end
error do
respond_with_error(500, env['sinatra.error'].message)
end
error ArgumentError do
respond_with_error(400, env['sinatra.error'].message)
end