ARTICLE AD BOX
I am hitting a wall combining HotChocolate's projection engine (ISelection) with Entity Framework Core when trying to project database entities into custom view models / DTOs.
Whenever I append HotChocolate's .Select(selection) to my query, the SQL translation engine silently strips out all the LEFT JOINs for my child collections, resulting in empty lists in my GraphQL response.
I have a complex database schema where I need to flatten nested relationships and handle some conditional logic. To avoid exposing my raw database entities to the GraphQL schema, I project them into a ProjectDto.
Here is the AutoMapper profile (which translates perfectly to EF Core expression trees on its own):
public class ProjectProfile : Profile { public ProjectProfile() { CreateMap<ProjectEntity, ProjectDto>() // 1. Flattening a nested relationship (skipping the middle table) .ForMember(dest => dest.TimeLogs, opt => opt.MapFrom(src => src.TimeSheets.SelectMany(ts => ts.TimeLogEntries))) // 2. Standard 1-to-many relationship .ForMember(dest => dest.Tasks, opt => opt.MapFrom(src => src.ProjectTasks)) // 3. Conditional mapping from different tables .ForMember(dest => dest.Metadata, opt => opt.MapFrom(src => src.Type == ProjectType.Internal && src.InternalMeta != null ? new MetadataDto { /* map fields */ } : src.Type == ProjectType.External && src.ExternalMeta != null ? new MetadataDto { /* map fields */ } : null)); } }PROBLEM
In my DataLoader (or Resolver), I am applying AutoMapper's ProjectTo followed by HotChocolate's selection to prevent overfetching:
public async Task<IReadOnlyDictionary<Guid, ProjectDto>> GetProjectsAsync( IReadOnlyList<Guid> projectIds, ISelection selection, AppDbContext context) { var projects = await context.Projects .Where(p => projectIds.Contains(p.Id)) .ProjectTo<ProjectDto>(_mapper.ConfigurationProvider) .Select(selection) // <--- THIS CAUSES THE ISSUE .ToListAsync(); return projects.ToDictionary(p => p.Id); }If I execute a GraphQL query that explicitly asks for tasks and timeLogs, the resulting SQL query only selects the scalar columns from the root Project table. There are zero LEFT JOINs, and the collections return empty.
Is there a supported way to make HotChocolate's ISelection (or [UseProjection]) play nicely with complex DTO mappings in EF Core?
