class NulogyGraphqlApi::GraphqlExecutor

Attributes

schema[R]
transaction_service[R]

Public Class Methods

execute(params, context, schema, transaction_service) click to toggle source
# File lib/nulogy_graphql_api/graphql_executor.rb, line 7
def self.execute(params, context, schema, transaction_service)
  new(schema, transaction_service).execute(params, context)
end
new(schema, transaction_service) click to toggle source
# File lib/nulogy_graphql_api/graphql_executor.rb, line 17
def initialize(schema, transaction_service)
  @schema = schema
  @transaction_service = transaction_service
end

Public Instance Methods

execute(params, context) click to toggle source
# File lib/nulogy_graphql_api/graphql_executor.rb, line 11
def execute(params, context)
  graphql_response(params, context).render_http_response
end

Private Instance Methods

ensure_hash(ambiguous_param) click to toggle source
# File lib/nulogy_graphql_api/graphql_executor.rb, line 45
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
execute_graphql(params, context) click to toggle source
# File lib/nulogy_graphql_api/graphql_executor.rb, line 26
def execute_graphql(params, context)
  query = params[:query]
  variables = ensure_hash(params[:variables])
  operation_name = params[:operationName]

  transaction_service.execute_in_transaction do |tx|
    result = GraphqlResponse.new(
      schema.execute(
        query,
        variables: variables,
        operation_name: operation_name,
        context: context
      )
    )
    tx.rollback if result.contains_errors?
    result
  end
end
graphql_response(params, context) click to toggle source
# File lib/nulogy_graphql_api/graphql_executor.rb, line 22
def graphql_response(params, context)
  execute_graphql(params, context)
end