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
|
# File 'lib/legion/api/graphql.rb', line 11
def self.registered(app)
app.post '/api/graphql' do
content_type :json
Legion::Logging.debug "API: POST /api/graphql params=#{params.keys}" if defined?(Legion::Logging)
body_str = request.body.read
payload = body_str.empty? ? {} : Legion::JSON.load(body_str)
payload = payload.transform_keys(&:to_sym) if payload.is_a?(Hash)
query = payload[:query]
variables = payload[:variables] || {}
operation_name = payload[:operationName]
if query.nil? || query.strip.empty?
Legion::Logging.warn 'API POST /api/graphql returned 400: query is required' if defined?(Legion::Logging)
status 400
next Legion::JSON.dump({
errors: [{ message: 'query is required' }]
})
end
result = Legion::API::GraphQL::Schema.execute(
query,
variables: variables,
operation_name: operation_name,
context: { request: request }
)
status 200
Legion::JSON.dump(result.to_h)
rescue StandardError => e
Legion::Logging.error "API POST /api/graphql: #{e.class} — #{e.message}" if defined?(Legion::Logging)
status 500
Legion::JSON.dump({ errors: [{ message: e.message }] })
end
app.get '/api/graphql' do
content_type 'text/html'
Legion::API::Routes::GraphQL.graphiql_html
end
end
|