I have setup Hot Chocolate with projections and the EF integration, working nicely
But want to try to translate some of the returned fields by setting their descriptor to “Translate()”
Used this video for inspiration, but instead of ToUpper I started trying to look up the translation in Redis
public static class FieldExtensions
{
public static IObjectFieldDescriptor Translate(this IObjectFieldDescriptor descriptor)
{
const string KeyPrefix = "translations";
return descriptor.Use(next => async context =>
{
await next(context);
if (context.Result is string s)
{
var cache = context.Service<IDistributedCache>();
var headers = context.GetGlobalStateOrDefault<IHeaderDictionary>("headers");
var language = headers?["lang"];
if(!String.IsNullOrWhiteSpace(language))
{
var translation = await cache.GetStringAsync($"{KeyPrefix}_{language}_{s}");
context.Result = translation ?? s;
}
}
});
}
}
But I’m worried about performance on multiple items, and if it would be possible to use a dataloader for this instead? Or inside the middleware in some way?