Skip to content

DataLoader LoadAsync Nullability #9104

Closed
Closed

Description

@cliedeman

Product

Hot Chocolate

Version

15.0.0

Link to minimal reproduction

https://github.com/cliedeman/hotchocolate-dataloader-nullability/blob/main/Program.cs

Steps to reproduce

dotnet build in the folder will print the following warning:

warning CS8619: Nullability of reference types in value of type 'Task<Author?>' doesn't match target type 'Task<Author>'.
var builder = WebApplication.CreateBuilder(args);
builder.AddGraphQL().AddTypes();
var app = builder.Build();
app.MapGraphQL();
app.RunWithGraphQLCommands(args);

public record Book(string Title, int AuthorId);
public record Author(string Name);

[ExtendObjectType<Book>]
public static class BookExtensions
{
    public static Task<Author> GetAuthorAsync(
        [Parent] Book book,
        IAuthorByIdDataLoader authorById,
        CancellationToken cancellationToken)
        // ISSUE
        // Nullability of reference types in value of type 'System.Threading.Tasks.Task<Author?>' does not match target type 'System.Threading.Tasks.Task<Author>'
        => authorById.LoadAsync(book.AuthorId, cancellationToken);
}

namespace GettingStarted
{
    internal static class AuthorDataLoaders
    {
        [DataLoader]
        public static Task<Dictionary<int, Author>> GetAuthorByIdAsync(
            IReadOnlyList<int> authorIds,
            CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }
    }
}

[QueryType]
public static class Query
{
    public static Book GetBook()
    => new Book("C# in depth.", 1);
}

What is expected?

We are trying to migrate from hotchocolate 13 to 15. We expect [DataLoader]s to continue working as is

What is actually happening?

we are getting a lot of CS8619 because data loaders now return null

Program.cs(19,12): warning CS8619: Nullability of reference types in value of type 'Task<Author?>' doesn't match target type 'Task<Author>'.

Relevant log output

Additional context

This change happened in #7389
and the ability was to control this behaviour was discussed in #2911

Activity

  1. added theissue type on Feb 9, 2026
  2. liyanjie2048 commented on Feb 12, 2026

    @liyanjie2048

    You might be looking for the method dataLoader.LoadRequiredAsync.

  3. michaelstaib commented on Feb 13, 2026

    @michaelstaib
    Member

    Exactly right ... if you require a key to always be resolvable use LoadRequiredAsync. However, in GraphQL a good practice is to let composite return types be nullable, if they could reasonable be nullable. LoadRequiredAsync will throw an exception of the DataLoader returns null for the requested key.

  4. changed the issue type fromtoon Feb 13, 2026
  5. cliedeman commented on Feb 13, 2026

    @cliedeman
    Author

    thanks @michaelstaib @liyanjie2048
    Ill try to add this to the migration notes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions