Class: Decidim::Api::QueriesController

Inherits:
ApplicationController show all
Defined in:
decidim-api/app/controllers/decidim/api/queries_controller.rb

Overview

This controller takes queries from an HTTP endpoint and sends them out to the Schema to be executed, later returning the response as JSON.

Instance Method Summary collapse

Methods inherited from ApplicationController

#permission_class_chain, #permission_scope

Methods included from RegistersPermissions

register_permissions

Methods included from NeedsOrganization

enhance_controller, extended, included

Instance Method Details

#createObject


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'decidim-api/app/controllers/decidim/api/queries_controller.rb', line 8

def create
  variables = prepare_variables(params[:variables])
  query = params[:query]
  operation_name = params[:operationName]
  result = Schema.execute(query, variables: variables, context: context, operation_name: operation_name)
  render json: result
rescue StandardError => e
  logger.error e.message
  logger.error e.backtrace.join("\n")

  message = if Rails.env.development?
              { message: e.message, backtrace: e.backtrace }
            else
              { message: "Internal Server error" }
            end

  render json: { errors: [message], data: {} }, status: :internal_server_error
end