c# – How do I get nested Hot Chocolate graphql data loaders to batch properly?

I have a graphQL query that looks like this:

query {
  vehiclesByIds(ids: 1,2,3,4,5...) {
    id
    options {
      optionId
      name
      adCopy {
        copyType
        copyText
      }
    }
  }
}

copyText is handled using a resolver that returns the translated text from a translation entity (it gets the language to use from the HTTP headers).

public Task GetCopyTextAsync(
    [Parent] CopyInfo parent,
    [Service] ILanguageService languageService,
    [Service] IAdCopyTranslatedTextByIdDataLoader dataLoader)
{
    if (parent != null && parent.CopyTextId > 0)
    {
        return dataLoader.LoadAsync(parent.CopyTextId);
    }

    return Task.FromResult(string.Empty);
}

The data loader loads the translation records by ID:

[DataLoader]
public static async Task> GetAdCopyTranslatedTextByIdAsync(
    IReadOnlyList keys,
    [Service] AppDbContext context,
    [Service] ILanguageService languageService,
    CancellationToken cancellationToken)
{
    var languageId = languageService.LanguageId ?? 1;
    return await context.AdCopyTranslationTexts
        .Where(x => keys.Contains(x.TranslationId) && x.LanguageId == languageId)
        .ToDictionaryAsync(x => x.Id, x => x.TranslatedText, cancellationToken);
}

The problem I am having is that when this gets run, it is running the data loader once for each vehicle, rather than actually batching them together, which can be leading to dozens of database calls. If I just query that adCopy entities like this:

query {
  adCopy {
    copyType
    copyText
  }
}

It does do a single lookup on the translation records. Does Hot Chocolate not support nesting data loaders like this? I thought that was kind of the point of data loaders – to batch the data rather than making tons of individual data calls.

Read more here: Source link