0

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<string?> 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?>(string.Empty);
}

The data loader loads the translation records by ID:

[DataLoader]
public static async Task<Dictionary<int, string?>> GetAdCopyTranslatedTextByIdAsync(
    IReadOnlyList<int> 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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.