diff --git a/backend/src/GraphQl/DataFormat/AllOpticalDataByDataFormatDataLoader.cs b/backend/src/GraphQl/DataFormat/AllOpticalDataByDataFormatDataLoader.cs new file mode 100644 index 000000000..b928162a3 --- /dev/null +++ b/backend/src/GraphQl/DataFormat/AllOpticalDataByDataFormatDataLoader.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using GreenDonut; +using HotChocolate.DataLoader; +using Metabase.GraphQl.DataX; + +namespace Metabase.GraphQl.DataFormat +{ + internal sealed class AllOpticalDataByDataFormatDataLoader + : BatchDataLoader<(Data.DataFormat DataFormat, OpticalDataPropositionInput Where, DateTime? Timestamp, string? Locale, uint? First, string? After, uint? Last, string? Before), OpticalDataConnection> + { + private readonly IDbContextFactory _dbContextFactory; + + public AllOpticalDataByDataFormatDataLoader( + IBatchScheduler batchScheduler, + IDbContextFactory dbContextFactory + ) + : base(batchScheduler) + { + _dbContextFactory = dbContextFactory; + } + + protected override + Task> + LoadBatchAsync( + IReadOnlyList<(Data.DataFormat DataFormat, OpticalDataPropositionInput Where, DateTime? Timestamp, string? Locale, uint? First, string? After, uint? Last, string? Before)> keys, + CancellationToken cancellationToken + ) + { + await using var dbContext = + _dbContextFactory.CreateDbContext(); + return await _getQueryable(dbContext).AsQueryable() + .Where(entity => ids.Contains(entity.Id)) + .ToDictionaryAsync( + entity => entity.Id, + entity => (TEntity?)entity, + cancellationToken + ) + .ConfigureAwait(false); + keys + .GroupBy(key => (key.Where, key.Timestamp, key.Locale, key.First, key.After, key.Last, key.Before)) + .; + } + } +} \ No newline at end of file diff --git a/backend/src/GraphQl/DataFormat/DataFormatResolvers.cs b/backend/src/GraphQl/DataFormat/DataFormatResolvers.cs new file mode 100644 index 000000000..434b639ee --- /dev/null +++ b/backend/src/GraphQl/DataFormat/DataFormatResolvers.cs @@ -0,0 +1,48 @@ + +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace Metabase.GraphQl.DataFormats +{ + public sealed class DataFormatResolvers + { + public async Task GetOpticalDataAsync( + Data.Database database, + Guid id, + DateTime? timestamp, + string? locale, + CancellationToken cancellationToken + ) + { + return null; + } + + public async Task GetAllOpticalDataAsync( + Data.Database database, + DataX.OpticalDataPropositionInput where, + DateTime? timestamp, + string? locale, + uint? first, + string? after, + uint? last, + string? before, + AllOpticalDataDataLoader dataLoader, + CancellationToken cancellationToken + ) + { + return dataLoader.LoadAsync(cancellationToken); + } + + public async Task GetHasOpticalDataAsync( + Data.Database database, + DataX.DataPropositionInput where, + DateTime? timestamp, + string? locale, + CancellationToken cancellationToken + ) + { + return null; + } + } +} \ No newline at end of file diff --git a/backend/src/GraphQl/DataFormat/DataFormatType.cs b/backend/src/GraphQl/DataFormat/DataFormatType.cs index 45e3691b9..48742f997 100644 --- a/backend/src/GraphQl/DataFormat/DataFormatType.cs +++ b/backend/src/GraphQl/DataFormat/DataFormatType.cs @@ -1,4 +1,5 @@ using HotChocolate.Types; +using Metabase.GraphQl.DataX; namespace Metabase.GraphQl.DataFormats { @@ -27,6 +28,21 @@ protected override void Configure( descriptor .Field(t => t.ManagerId) .Ignore(); + DataFieldConfigurator.ConfigureDataField( + descriptor, + "opticalData", + _ => _.GetOpticalDataAsync(default!, default!, default!, default!, default!) + ); + DataFieldConfigurator.ConfigureAllDataField( + descriptor, + "allOpticalData", + _ => _.GetAllOpticalDataAsync(default!, default!, default!, default!, default!, default!, default!, default!, default!) + ); + DataFieldConfigurator.ConfigureHasDataField( + descriptor, + "hasOpticalData", + _ => _.GetHasOpticalDataAsync(default!, default!, default!, default!, default!) + ); } } } \ No newline at end of file diff --git a/backend/src/GraphQl/DataX/DataFieldConfigurator.cs b/backend/src/GraphQl/DataX/DataFieldConfigurator.cs new file mode 100644 index 000000000..ecdb533a7 --- /dev/null +++ b/backend/src/GraphQl/DataX/DataFieldConfigurator.cs @@ -0,0 +1,55 @@ +using System; +using System.Linq.Expressions; +using HotChocolate.Types; + +namespace Metabase.GraphQl.DataX +{ + internal static class DataFieldConfigurator + { + internal static void ConfigureDataField( + IObjectTypeDescriptor descriptor, + string fieldName, + Expression> resolverMethod + ) + { + descriptor + .Field(fieldName) + .Argument("id", _ => _.Type>()) + .Argument("timestamp", _ => _.Type()) + .Argument("locale", _ => _.Type()) + .ResolveWith(resolverMethod); + } + + internal static void ConfigureAllDataField( + IObjectTypeDescriptor descriptor, + string fieldName, + Expression> resolverMethod + ) + { + descriptor + .Field(fieldName) + .Argument("where", _ => _.Type>>()) + .Argument("timestamp", _ => _.Type()) + .Argument("locale", _ => _.Type()) + .Argument("first", _ => _.Type()) + .Argument("after", _ => _.Type()) + .Argument("last", _ => _.Type()) + .Argument("before", _ => _.Type()) + .ResolveWith(resolverMethod); + } + + internal static void ConfigureHasDataField( + IObjectTypeDescriptor descriptor, + string fieldName, + Expression> resolverMethod + ) + { + descriptor + .Field(fieldName) + .Argument("where", _ => _.Type>>()) + .Argument("timestamp", _ => _.Type()) + .Argument("locale", _ => _.Type()) + .ResolveWith(resolverMethod); + } + } +} \ No newline at end of file diff --git a/backend/src/GraphQl/Databases/AllOpticalDataByDatabaseDataLoader.cs b/backend/src/GraphQl/Databases/AllOpticalDataByDatabaseDataLoader.cs new file mode 100644 index 000000000..72b72f3b9 --- /dev/null +++ b/backend/src/GraphQl/Databases/AllOpticalDataByDatabaseDataLoader.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using GreenDonut; +using HotChocolate.DataLoader; +using Microsoft.EntityFrameworkCore; + +namespace Metabase.GraphQl.Database +{ + internal sealed class AllOpticalDataByDatabaseDataLoader + : BatchDataLoader<(Data.Database database, DataX.OpticalDataPropositionInput where, DateTime? timestamp, string? locale, uint? first, string? after, uint? last, string? before), OpticalDataConnection> + { + private readonly IDbContextFactory _dbContextFactory; + + public AllOpticalDataByDatabaseDataLoader( + IBatchScheduler batchScheduler, + IDbContextFactory dbContextFactory + ) + : base(batchScheduler) + { + _dbContextFactory = dbContextFactory; + } + + protected override + Task> + LoadBatchAsync( + IReadOnlyList<(Data.Database database, DataX.OpticalDataPropositionInput where, DateTime? timestamp, string? locale, uint? first, string? after, uint? last, string? before)> keys, + CancellationToken cancellationToken + ) + { + return (await QueryDatabase( + database, + new GraphQL.GraphQLRequest( + query: await ConstructQuery( + new[] { + "DataFields.graphql", + "OpticalDataFields.graphql", + "PageInfoFields.graphql", + "AllOpticalData.graphql" + } + ).ConfigureAwait(false), + variables: new + { + where, + timestamp, + locale, + first, + after, + last, + before + }, + operationName: "AllOpticalData" + ), + cancellationToken + ).ConfigureAwait(false) + )?.AllOpticalData; + } + } +} \ No newline at end of file