class Graphiti::Railtie

@deprecated Use graphiti-rails’s Graphiti::Rails::Railtie

Constants

PARSER

from jsonapi-rails

Public Instance Methods

configure_endpoint_lookup() click to toggle source
# File lib/graphiti/railtie.rb, line 98
def configure_endpoint_lookup
  Graphiti.config.context_for_endpoint = ->(path, action) {
    method = :GET
    case action
      when :show then path = "#{path}/1"
      when :create then method = :POST
      when :update
        path = "#{path}/1"
        method = :PUT
      when :destroy
        path = "#{path}/1"
        method = :DELETE
    end

    route = begin
      ::Rails.application.routes.recognize_path(path, method: method)
    rescue
      nil
    end
    "#{route[:controller]}_controller".classify.safe_constantize if route
  }
end
establish_concurrency() click to toggle source

Only run concurrently if our environment supports it

# File lib/graphiti/railtie.rb, line 93
def establish_concurrency
  Graphiti.config.concurrency = !::Rails.env.test? &&
    ::Rails.application.config.cache_classes
end
register_parameter_parser() click to toggle source
# File lib/graphiti/railtie.rb, line 52
def register_parameter_parser
  if ::Rails::VERSION::MAJOR >= 5
    ActionDispatch::Request.parameter_parsers[:jsonapi] = PARSER
  else
    ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime[:jsonapi]] = PARSER
  end
end
register_renderers() click to toggle source
# File lib/graphiti/railtie.rb, line 60
def register_renderers
  ActiveSupport.on_load(:action_controller) do
    ::ActionController::Renderers.add(:jsonapi) do |proxy, options|
      self.content_type ||= Mime[:jsonapi]

      # opts = {}
      # if respond_to?(:default_jsonapi_render_options)
      #   opts = default_jsonapi_render_options
      # end

      if proxy.is_a?(Hash) # for destroy
        render(options.merge(json: proxy))
      else
        proxy.to_jsonapi(options)
      end
    end
  end

  ActiveSupport.on_load(:action_controller) do
    ::ActionController::Renderers.add(:jsonapi_errors) do |proxy, options|
      self.content_type ||= Mime[:jsonapi]

      validation = GraphitiErrors::Validation::Serializer.new \
        proxy.data, proxy.payload.relationships

      render \
        json: {errors: validation.errors},
        status: :unprocessable_entity
    end
  end
end