Module: GraphQL::Extras::Controller

Defined in:
lib/graphql/extras/controller.rb

Instance Method Summary collapse

Instance Method Details

#graphql(schema:, context: {}, debug: Rails.env.development?) ⇒ Object



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
# File 'lib/graphql/extras/controller.rb', line 4

def graphql(schema:, context: {}, debug: Rails.env.development?)
  query = params[:query]
  operation = params[:operationName]
  variables = cast_graphql_params(params[:variables])

  uploads = params.to_unsafe_h.select do |_, value|
    value.is_a?(ActionDispatch::Http::UploadedFile)
  end

  result = schema.execute(
    query,
    operation_name: operation,
    variables: variables,
    context: context.merge(uploads: uploads)
  )

  render(status: 200, json: result)
rescue => error
  raise error unless debug

  logger.error(error.message)
  logger.error(error.backtrace.join("\n"))

  render(
    status: 500,
    json: {
      data: {},
      error: {
        message: error.message,
        backtrace: error.backtrace
      }
    }
  )
end