I need to query a has_many association field with a different name than the one in the original schema. When the field is not an association, it’s rather simple, but I can’t figure out a solution to do it with associations.
This gives an error(“field image_labels in select is a virtual field”), so I tried using select_merge to select it first, I also tried injecting SQL code with fragment, but didn’t find a solution…
The reason I want to do this is because I am resolving my image_labels in an union type, and so I have several field names that I need to rename with the same name.
I am trying to do it without using the has_many field. Here is what my query looks like:
from(
d in datapoints,
join: l in assoc(d, :label_image_classes),
on: l.datapoint_id == d.id and l.dataset_id == ^dataset.id,
preload: [:label_image_classes]
)
|> Absinthe.Relay.Connection.from_query(&Repo.all/1, pagination_args)
Thing is, it resolves to a map with a field :label_image_classes, whereas I need to have a field :labels.
I could modify the map after the query, but I figured it would be very error-prone…
This is exactly what I am trying to do actually! The only thing is that I have several associations that resolve to the labels field, so I need to write a custom resolver.
But now in my resolver, I need to rename the association field to labels, because Absinthe won’t take care of it for me.
Totally my bad if my use case is not clear enough, let me know if there is still something unclear.
Because I can’t resolve the labels field automatically, I need to write the logic in a resolver. But I don’t want to write a custom resolver for the labels, because then I would have to deal with the batching of the queries. Instead, I am trying populate the labels field one level higher: in my datapoint resolver.
In my datapoint resolver, the labels can be of different types, but I need to merge everything in labels to let Absinthe resolve it.
Does my previous message answer this? I am not writing the :labels resolver, but the :datapoint resolver and I try to populate the :labels field directly inside this resolver. The reason I am doing it this way is because I don’t want to deal with query batching.
You might not want to do batching, but it seems You just transfer the problem to the query You want to build…
Because You want to retrieve image_labels for each datapoint, which will be n+1 query any way. Even if You transform the join to a list in the final result.