Modifying many_to_many generated filter
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?
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?
Yes, that is much better
https://github.com/graphiti-api/graphiti/pull/295 please review
Fix has been merged. Thanks!