Fixed AuditInterceptor.cs , add GraphQL error logging, add translation request get

This commit is contained in:
gamer147
2025-11-18 13:14:31 -05:00
parent 0c1705ebe1
commit 716087e4a4
6 changed files with 56 additions and 4 deletions

View File

@@ -12,6 +12,7 @@ public static class GraphQLExtensions
return services.AddGraphQLServer()
.AddQueryType<TQuery>()
.AddMutationType<TMutation>()
.AddDiagnosticEventListener<ErrorEventListener>()
.AddType<UnsignedIntType>()
.AddType<InstantType>()
.AddMutationConventions(applyToAllMutations: true)

View File

@@ -12,9 +12,24 @@ public class AuditInterceptor : SaveChangesInterceptor
InterceptionResult<int> result)
{
var context = eventData.Context;
SetAuditFields(context);
if (context == null)
return base.SavingChanges(eventData, result);
}
public override ValueTask<InterceptionResult<int>> SavingChangesAsync(DbContextEventData eventData, InterceptionResult<int> result,
CancellationToken cancellationToken = new CancellationToken())
{
var context = eventData.Context;
SetAuditFields(context);
return base.SavingChangesAsync(eventData, result, cancellationToken);
}
private void SetAuditFields(DbContext? context)
{
if (context == null)
return;
var entries = context.ChangeTracker.Entries<IAuditable>();
@@ -32,7 +47,5 @@ public class AuditInterceptor : SaveChangesInterceptor
e.Entity.LastUpdatedTime = now;
}
}
return base.SavingChanges(eventData, result);
}
}

View File

@@ -18,7 +18,6 @@ public abstract class FictionArchiveDbContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.AddInterceptors(new AuditInterceptor());
base.OnConfiguring(optionsBuilder);
}
public void UpdateDatabase()

View File

@@ -0,0 +1,20 @@
using HotChocolate.Execution;
using HotChocolate.Execution.Instrumentation;
using Microsoft.Extensions.Logging;
namespace FictionArchive.Service.Shared.Services.GraphQL;
public class ErrorEventListener : ExecutionDiagnosticEventListener
{
private readonly ILogger<ErrorEventListener> _logger;
public ErrorEventListener(ILogger<ErrorEventListener> logger)
{
_logger = logger;
}
public override void RequestError(IRequestContext context, Exception exception)
{
_logger.LogError(exception, "An error occurred while processing a GraphQL request.");
}
}

View File

@@ -1,5 +1,8 @@
using FictionArchive.Service.TranslationService.Models;
using FictionArchive.Service.TranslationService.Models.Database;
using FictionArchive.Service.TranslationService.Services.Database;
using FictionArchive.Service.TranslationService.Services.TranslationEngines;
using Microsoft.EntityFrameworkCore;
namespace FictionArchive.Service.TranslationService.GraphQL;
@@ -11,4 +14,13 @@ public class Query
{
return engines.Select(engine => engine.Descriptor);
}
[UsePaging]
[UseProjection]
[UseFiltering]
[UseSorting]
public IQueryable<TranslationRequest> GetTranslationRequests(TranslationServiceDbContext dbContext)
{
return dbContext.TranslationRequests.AsQueryable();
}
}

View File

@@ -42,6 +42,13 @@ public class Program
var app = builder.Build();
// Update database
using (var scope = app.Services.CreateScope())
{
var dbContext = scope.ServiceProvider.GetRequiredService<TranslationServiceDbContext>();
dbContext.UpdateDatabase();
}
app.UseHttpsRedirection();
app.MapHealthChecks("/healthz");