graphiti icon indicating copy to clipboard operation
graphiti copied to clipboard

Modifying many_to_many generated filter

Open hanssgo opened this issue 6 years ago • 4 comments

When I have a many_to_many relationship. Sometimes the generated filter does not match with what we want.

Lets say I want to override this generated filter (generated by many_to_many relationship as documented in https://www.graphiti.dev/guides/concepts/resources#many-to-many, not actually in our code base)

filter :team_id, :integer do
  eq do |scope, value|
    scope
      .includes(:team_memberships)
      .where(team_memberships: { team_id: value }
  end
end

With this

filter :team_id, :integer, only: [:eq] do
  eq do |scope, value|
    scope
      .includes(:team_memberships)
      .where(team_memberships: { team_id: value, active: true }
  end
end

When I try to override that filter, it works just fine in our Polymorphic Resource but it does not in our non-polymorphic resource. It seems the generated filter takes precedence over our manually implemented filter. Any suggestions for this?

hanssgo avatar Oct 26 '20 22:10 hanssgo

I think this is likely due to Rails' autoloading in development, where your override words correctly but then another class is loaded and blows it away. Really we should check if the filter already exists here (resource_class.config[:filters].has_key?(inverse_filter.to_sym)). Could you let me know if that fixes your issue and if so send a PR?

richmolj avatar Oct 27 '20 11:10 richmolj

Yes, that is much better

hanssgo avatar Oct 27 '20 16:10 hanssgo

https://github.com/graphiti-api/graphiti/pull/295 please review

hanssgo avatar Oct 27 '20 17:10 hanssgo

Fix has been merged. Thanks!

hanssgo avatar Nov 02 '20 17:11 hanssgo