graphiti-api / graphiti-rails

MIT License
54 stars 28 forks source link

Custom `:jsonapi` parameter_parsers being overriden by `graphiti-rails` #106

Open svobom57 opened 1 year ago

svobom57 commented 1 year ago

We are facing a peculiar issue where we override the jsonapi parser in order to make the incoming payload snake_case instead of camelCase.

We use a solution similar to the one proposed here: https://github.com/graphiti-api/graphiti/issues/286

where at some point we call: ActionDispatch::Request.parameter_parsers[:jsonapi] = transformer

Now the problem is that graphiti-rails does the same thing here in the Railtie: https://github.com/graphiti-api/graphiti-rails/blob/759db8d25f14ae4b171e9b961e51ab50027a0d67/lib/graphiti/rails/railtie.rb#L75 which then overrides our custom implementation if called after our custom initialisation.

We have this set up in quite a few repos but for one of our repos the initialisation process (for some reason I'm not sure of) is reversed and the order of the calls are as follows:

  1. Our custom initialiser is called
  2. graphiti-rails Railtie is called overriding our custom parser

I'm not quite sure what determines the order of the Railtie call and our initialisation but I'm thinking that perhaps the gem should be resilient to this and only assign the transformer if one does not already exist, i.e. instead of the current code:

      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

run:

      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
wagenet commented 10 months ago

@svobom57 Unfortunately, I'm not working with Rails at my current job and have no bandwidth to help here. It looks like that may be true for the other contributors to this project as well. If you (or anyone else) have interest in picking up ownership, please let me know. Sorry!

svobom57 commented 9 months ago

@wagenet Opened up a PR here: https://github.com/graphiti-api/graphiti-rails/pull/109 Let me know what else to do. šŸ™