question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[UseProjection] not working

See original GitHub issue

Is there an existing issue for this?

  • I have searched the existing issues

Product

Hot Chocolate

Describe the bug

Projection not working following document : ⚠️ Note: If you use more than one middleware, keep in mind that ORDER MATTERS. The correct order is UsePaging > UseProjection > UseFiltering > UseSorting

Steps to reproduce

here is my code

[UsePaging(MaxPageSize = 10)]
[UseProjection]
[UseFiltering]
[UseSorting]
public async Task<List<Project>> GetProjectsAsync(
    [Service] IMediator mediator,
    CancellationToken cancellationToken)
{
    return await mediator.Send(new GetProjectsQuery(), cancellationToken);
}
public class GetProjectsQueryHandler : IRequestHandler<GetProjectsQuery, List<Project>>
{
    private readonly IProjectRepository _repository;

    public GetProjectsQueryHandler(IProjectRepository repository)
    {
        _repository = repository;
    }
    public async Task<List<Project>> Handle(GetProjectsQuery request, CancellationToken cancellationToken)
        => await _repository.GetAllProjects().ToListAsync(cancellationToken);
}
public class ProjectRepository : IProjectRepository
{
    private readonly DbContext _context;

    public ProjectRepository(DbContext context)
    {
        _context = context ?? throw new ArgumentNullException(nameof(context));
    }

    IQueryable<Project> IProjectRepository.GetAllProjects()
        => _context.Projects.AsQueryable().AsNoTracking();
}

Out put console

SELECT [p].[Id], [p].[CreatedBy], [p].[CreatedDate], [p].[Description], [p].[DueDate], [p].[Name], [p].[Status], [p].[UpdatedBy], [p].[UpdatedDate]
FROM [Projects] AS [p]

Program.cs

// This adds the GraphQL server core service and declares a schema.
builder.Services
    .AddMemoryCache()
    .AddGraphQLServer()
    .AddAuthorization()
    .AddQueryType()
    .AddMutationType()
    .AddDataLoaders()
    .AddProjections()
    .AddFiltering()
    .AddSorting()
    .AddInMemorySubscriptions()
    .AddMutationConventions()
    .AddTypeExtension<ProjectQueries>()
    .AddTypeExtension<ProjectMutations>()
    .AddTypeExtension<UserMutations>()
    .InitializeOnStartup();

My Query

query{
   projects{
    nodes{
      id
      name
      description
      
    }
   }
}

Relevant log output

SELECT [p].[Id], [p].[CreatedBy], [p].[CreatedDate], [p].[Description], [p].[DueDate], [p].[Name], [p].[Status], [p].[UpdatedBy], [p].[UpdatedDate]
FROM [Projects] AS [p]

Additional Context?

No response

Version

13.5.0-preview.8

Issue Analytics

  • State:closed
  • Created 2 months ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
michaelstaibcommented, Aug 1, 2023

Paging is also not working… its slicing in memory … which is not what you want.

1reaction
michaelstaibcommented, Aug 1, 2023

Another issue you will run into is scoping btw…

In your case … the layering also makes no sense … as your repository exposes a queryable … this should not be the case and makes the repository pattern useless.

=> await _repository.GetAllProjects().ToListAsync(cancellationToken);

this is where you already execute against the DB … after this all is done.

Read more comments on GitHub >

github_iconTop Results From Across the Web

UseProjection does not work on UnionType · Issue #6312
Expect to be able to UseProjection even when a UnionType is returned. If the resolver does not return an IQueryable then projection middleware ......
Read more >
Am I misunderstanding Projections in HotChocolate?
public class Queries { [UseDbContext(typeof(DbAccess))] [UseProjection] public IQueryable<Name> GetNames([ScopedService] DbAccess db) ...
Read more >
Projections - Hot Chocolate - ChilliCream GraphQL Platform
Fields that define a customer resolver cannot be projected to the database. If the middleware encounters a field that specifies UseProjection() ...
Read more >
Projections - GRAPHQL API IN .NET w/ HOT CHOCOLATE #7.4
GraphQL - Running Queries (An introduction for .NET Developers [Using .NET 6 and C# 10]). DotNet Core Central•10K views.
Read more >
Understanding Middleware in Hot Chocolate - YouTube
Hi everyone, I am Michael, and in this video, I will dive deep into the Hot Chocolate core to see what middleware is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found