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.
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:
d in datapoints,
join: l in assoc(d, :label_image_classes),
on: l.datapoint_id == d.id and l.dataset_id == ^dataset.id,
|> 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…
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.