landmarkhw/Dapper.GraphQL

SqlQueryContext: SplitOn() should call RemoveSingleTableQueryItems()

JohnStov opened this issue · 3 comments

I want my QueryBuilder.Build() to look like this:

            query
                .Select($"{alias}.EmployeeId", $"{alias}.Forename", $"{alias}.Surname", $"{alias}.MobileNumber", $"{alias}.Postcode")
                .AndWhere($"{alias}.IsDeleted = 0")
                .SplitOn<Employee>($"{alias}.EmployeeId");

            var fields = context.GetSelectedFields();

            if (fields.ContainsKey("depot"))
            {
                var depotAlias = "depot";
                query.LeftJoin($"Depot {depotAlias} ON {alias}.DepotId = {depotAlias}.DepotId");
                _depotQueryBuilder.Build(query, fields["depot"], depotAlias);
            }

            if (fields.ContainsKey("weeklyRosters"))
            {
                var weeklyRosterAlias = "weeklyRoster";
                query.LeftJoin($"WeeklyRoster {weeklyRosterAlias} ON {alias}.DepotId = {weeklyRosterAlias}.DepotId");
                _weeklyRosterQueryBuilder.Build(query, fields["weeklyRosters"], weeklyRosterAlias);
            }

            return query;

But this throws an ArgumentException in Dapper.SqlMapper.GetNextSplit() saying "When using the multi-mapping APIs ensure you set the splitOn param if you have keys other than Id". Adding a call to SqlQueryContext.RemoveSingleTableQueryItems() in SqlQueryContext.SplitOn() fixes the problem, so that we get the right number of entries in SqlQueryContext._types.

I've realised why this doesn't fail with the existing examples. It's because the PersonQueryBuilder calls LeftJoin() before it calls SplitOn(). LeftJoin() calls RemoveSingleTableQueryItems() so the problem doesn't arise.

Thanks again for the contribution, this will be included in the next release (0.4.2-beta).

This was released in 0.4.2-beta. Thanks!