class Graphiti::Rails::Railtie
This Railtie
exposes some configuration options:
-
`config.graphiti.handled_exception_formats`, defaulting to `[:jsonapi]`. Formats in this list will always have their exceptions handled by
Graphiti
. -
`config.graphiti.respond_to_formats`, defaulting to `[:json, :jsonapi, :xml]`. When {Graphiti::Rails::Responders} is included in a controller it will respond to these mime types by default.
Constants
- MEDIA_TYPE
from jsonapi-rails
- PARSER
Public Instance Methods
configure_endpoint_lookup()
click to toggle source
# File lib/graphiti/rails/railtie.rb, line 119 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/rails/railtie.rb, line 114 def establish_concurrency Graphiti.config.concurrency = !::Rails.env.test? && ::Rails.application.config.cache_classes end
register_mime_type()
click to toggle source
# File lib/graphiti/rails/railtie.rb, line 69 def register_mime_type Mime::Type.register(MEDIA_TYPE, :jsonapi) end
register_parameter_parser()
click to toggle source
# File lib/graphiti/rails/railtie.rb, line 73 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/rails/railtie.rb, line 81 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