graphiti
graphiti copied to clipboard
Proposal to allow for overriding relationship getter
Hello, please correct me if I am wrong with my assumptions here.
My use-case is that I'd like to use has_one
with a has_one_attached
object. The issue is that the method on the model always returns this non-nil "proxy" object even when nothing is attached. For example:
class User
has_one_attached :avatar
end
> User.new.avatar
#<ActiveStorage::Attached::One...
I think I've traced the logic to handle the relationships all the way to this proc: https://github.com/graphiti-api/graphiti/blob/19c75b58017eaf860a5160ff28c0f7dbd3f47674/lib/graphiti/util/serializer_relationships.rb#L75 -- it simply uses public_send
to call the association method which leads to issues down the line when a non-nil object is returned but is not backed by any data.
Unfortunately, I did not see a great way to resolve this without overriding the entire data_proc
method with a monkey-patch that checks records.attached?
and returns nil accordingly.
Is there a built-in way to do this I don't know about, and if not, what would be the preferred approach to allow for custom associations such as this?
@lserman I'm curious if you found a nifty solution or workaround?