graphiti icon indicating copy to clipboard operation
graphiti copied to clipboard

NoMethodError when page is not a hash

Open fmluizao opened this issue 4 years ago • 3 comments

Request:

/some_controller?limit=10&page=1

Error:

undefined method `each_pair' for "1":String
Did you mean?  each_char

The error comes from here:

# graphiti/query.rb
 def pagination
      @pagination ||= begin
        {}.tap do |hash|
          (@params[:page] || {}).each_pair do |name, value|

Would you accept a pull request to handle that gracefully?

fmluizao avatar Mar 24 '21 11:03 fmluizao

To throw a better error? Absolutely!

richmolj avatar Mar 24 '21 12:03 richmolj

Which kind of error should we throw? Some kind of schema error?

fmluizao avatar Mar 24 '21 13:03 fmluizao

I think a Graphiti::Errors::InvalidRequest. In fact this is what we throw for writes, and have validator classes for writes https://github.com/graphiti-api/graphiti/tree/master/lib/graphiti/request_validators

Ideally we'd do the something similar for reads (already runs through a validator and does nothing actually - https://github.com/graphiti-api/graphiti/blob/master/lib/graphiti/request_validators/validator.rb#L17)

richmolj avatar Mar 24 '21 13:03 richmolj