contrib icon indicating copy to clipboard operation
contrib copied to clipboard

Adds support for modifying edge queries

Open ivanvanderbyl opened this issue 1 year ago • 0 comments

This PR adds support for applying modifiers to edge queries.

Our use-case is that we have a table that stores the history of prices for a hotel and we want to apply a join to a sub-select to only return the latest prices on this edge. This cannot be done with predicates alone and requires use of query modifiers.

Usage:

func (r *rateWhereInputResolver) HasOccupancyAndDates(ctx context.Context, obj *ent.RateWhereInput, data *model.OccupancyAndDateInput) error {

  obj.Modify(func(s *sql.Selector) {
		t2 := sql.Table(Table)

		subQuery := sql.Select(
			sql.As(sql.Max(t2.C(FieldTimestamp)), FieldTimestamp),
			sql.As(t2.C(FieldHotelPropertyID), "hp_id"),
			t2.C(FieldKey),
		).
			From(t2).
			GroupBy(t2.C(FieldHotelPropertyID), t2.C(FieldKey)))
                 s.Join(subQuery)
    ...
}

ivanvanderbyl avatar Sep 09 '22 01:09 ivanvanderbyl