Skip to content

Make sure IQueryable implements IAsyncEnumerable before using ToListAsync #4018

@WolfspiritM

Description

@WolfspiritM

Describe the bug
When using EFCore with UseDbContext and returning an IQueryable that is not coming from EFCore (e.g. does not implement IAsyncEnumerable), then the query will fail with the error The source 'IQueryable' doesn't implement 'IAsyncEnumerable<Ocora.Services.Telematics.Core.Entities.Masterdata.Dtos.AssetDto>'. Only sources that implement 'IAsyncEnumerable' can be used for Entity Framework asynchronous operations.

I know that in general always the database IQueryable should be returned for performance reasons and possible filtering but in some cases this might not be possible and for debugging it can also be helpful to return a list instead of the IQueryable.

The error comes from here: https://github.com/ChilliCream/hotchocolate/blob/c1eb36cf3542572372847c5e38f4bc349bc22bfa/src/HotChocolate/Data/src/EntityFramework/ToListMiddleware.cs#L23-L25

I think this needs a check for IAsyncEnumerable similar to https://github.com/ChilliCream/hotchocolate/blob/c1eb36cf3542572372847c5e38f4bc349bc22bfa/src/HotChocolate/Data/src/Data/QueryableExecutable.cs#L44

Maybe this is even a bug as it's not using that extension method but the EFCore internal extension method cause of a missing/wrong using statement?!

To Reproduce
Steps to reproduce the behavior:

  1. Add UseDbContext
  2. return an enumerable (e.g. a list) with ".AsQueryable()".

Expected behavior
No exception as ToList is used instead of ToListAsync if IQueryable does not implement IAsyncEnumerable

Desktop (please complete the following information):

  • OS: [e.g. iOS] Windows 10
  • Version [e.g. 22] 11

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions