-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgraphql_controller_mixin.rb
67 lines (57 loc) · 1.54 KB
/
graphql_controller_mixin.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
module DevmakerRailsSdk
module GraphqlControllerMixin
def execute
result = params[:_json] ? multiplex_execution(params) : single_execution(params)
render json: result
rescue => e
raise e unless Rails.env.development?
handle_error_in_development e
end
def variables
ensure_hash(params[:variables])
end
def context
{}
end
def schema
nil
end
protected
def single_execution(params)
schema.execute(params[:query], variables: variables, context: context, operation_name: params[:operationName])
end
def multiplex_execution(params)
queries = params[:_json].map do |param|
{
query: param[:query],
operation_name: param[:operationName],
variables: variables,
context: context,
}
end
schema.multiplex(queries)
end
# Handle form data, JSON body, or a blank value
def ensure_hash(ambiguous_param)
case ambiguous_param
when String
if ambiguous_param.present?
ensure_hash(JSON.parse(ambiguous_param))
else
{}
end
when Hash, ActionController::Parameters
ambiguous_param
when nil
{}
else
raise ArgumentError, "Unexpected parameter: #{ambiguous_param}"
end
end
def handle_error_in_development(e)
logger.error e.message
logger.error e.backtrace.join("\n")
render json: { error: { message: e.message, backtrace: e.backtrace }, data: {} }, status: 500
end
end
end