Skip to content

Commit

Permalink
upgrade hotchocolate to 14 (#1167)
Browse files Browse the repository at this point in the history
* upgrade hot chocolate to v14

* workaround projection issue

* Fix admin dashboard

* Fix: dotnet run generate-gql-schema

---------

Co-authored-by: Tim Haasdyk <[email protected]>
  • Loading branch information
hahn-kev and myieye authored Oct 31, 2024
1 parent 0a42f92 commit e83930d
Show file tree
Hide file tree
Showing 9 changed files with 280 additions and 252 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public IsLanguageForgeProjectDataLoader(
IBatchScheduler batchScheduler,
[FromKeyedServices(ResiliencePolicyName)]
ResiliencePipeline<IReadOnlyDictionary<string, bool>> resiliencePipeline,
DataLoaderOptions? options = null)
DataLoaderOptions options)
: base(batchScheduler, options)
{
_resiliencePipeline = resiliencePipeline;
Expand Down
26 changes: 11 additions & 15 deletions backend/LexBoxApi/GraphQL/GraphQlSetupKernel.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using DataAnnotatedModelValidations;
using HotChocolate.Diagnostics;
using LexBoxApi.Auth;
using LexBoxApi.GraphQL.CustomFilters;
using LexBoxApi.Services;
using LexBoxApi.Services.Email;
using LexCore.ServiceInterfaces;
using LexData;

namespace LexBoxApi.GraphQL;
Expand All @@ -18,15 +15,14 @@ public static void AddLexGraphQL(this IServiceCollection services, IHostEnvironm
if (forceGenerateSchema || env.IsDevelopment())
services.AddHostedService<DevGqlSchemaWriterService>();

services.AddGraphQLServer()
services
.AddGraphQLServer()
.ModifyCostOptions(options =>
{
// See: https://github.com/sillsdev/languageforge-lexbox/issues/1179
options.EnforceCostLimits = false;
})
.InitializeOnStartup()
.RegisterDbContext<LexBoxDbContext>()
.RegisterService<IHgService>()
.RegisterService<IIsLanguageForgeProjectDataLoader>()
.RegisterService<LoggedInContext>()
.RegisterService<IEmailService>()
.RegisterService<LexAuthService>()
.RegisterService<IPermissionService>()
.AddDataAnnotationsValidator()
.AddSorting(descriptor =>
{
Expand All @@ -39,11 +35,11 @@ public static void AddLexGraphQL(this IServiceCollection services, IHostEnvironm
descriptor.AddDeterministicInvariantContainsFilter();
})
.AddProjections()
.SetPagingOptions(new()
.ModifyPagingOptions(options =>
{
DefaultPageSize = 100,
MaxPageSize = 1000,
IncludeTotalCount = true
options.DefaultPageSize = 100;
options.MaxPageSize = 1000;
options.IncludeTotalCount = true;
})
.AddAuthorization()
.AddLexBoxApiTypes()
Expand Down
6 changes: 5 additions & 1 deletion backend/LexBoxApi/GraphQL/LexQueries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,11 @@ public IQueryable<User> UsersInMyOrg(LexBoxDbContext context, LoggedInContext lo
IPermissionService permissionService,
IResolverContext context)
{
var org = await dbContext.Orgs.Where(o => o.Id == orgId).AsNoTracking().Project(context).SingleOrDefaultAsync();
//todo remove this workaround once the issue is fixed
var projectContext =
context.GetLocalStateOrDefault<IResolverContext>("HotChocolate.Data.Projections.ProxyContext") ??
context;
var org = await dbContext.Orgs.Where(o => o.Id == orgId).AsNoTracking().Project(projectContext).SingleOrDefaultAsync();
if (org is null) return org;
// Site admins and org admins can see everything
if (permissionService.CanEditOrg(orgId)) return org;
Expand Down
8 changes: 4 additions & 4 deletions backend/LexBoxApi/GraphQL/OrgMutations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public async Task<Organization> DeleteOrg(Guid orgId,
public async Task<IQueryable<Organization>> AddProjectToOrg(
LexBoxDbContext dbContext,
IPermissionService permissionService,
[Service] ProjectService projectService,
ProjectService projectService,
Guid orgId,
Guid projectId)
{
Expand Down Expand Up @@ -100,7 +100,7 @@ public async Task<IQueryable<Organization>> AddProjectToOrg(
public async Task<Organization?> AddProjectsToOrg(
LexBoxDbContext dbContext,
IPermissionService permissionService,
[Service] ProjectService projectService,
ProjectService projectService,
IResolverContext resolverContext,
Guid orgId,
Guid[] projectIds)
Expand Down Expand Up @@ -139,7 +139,7 @@ public async Task<IQueryable<Organization>> AddProjectToOrg(
public async Task<IQueryable<Organization>> RemoveProjectFromOrg(
LexBoxDbContext dbContext,
IPermissionService permissionService,
[Service] ProjectService projectService,
ProjectService projectService,
Guid orgId,
Guid projectId)
{
Expand Down Expand Up @@ -185,7 +185,7 @@ public async Task<IQueryable<Organization>> SetOrgMemberRole(
OrgRole role,
string emailOrUsername,
bool canInvite,
[Service] IEmailService emailService)
IEmailService emailService)
{
var org = await dbContext.Orgs.FindAsync(orgId);
NotFoundException.ThrowIfNull(org);
Expand Down
22 changes: 11 additions & 11 deletions backend/LexBoxApi/GraphQL/ProjectMutations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public record CreateProjectResponse(Guid? Id, CreateProjectResult Result);
LoggedInContext loggedInContext,
IPermissionService permissionService,
CreateProjectInput input,
[Service] ProjectService projectService,
[Service] IEmailService emailService)
ProjectService projectService,
IEmailService emailService)
{
if (!loggedInContext.User.IsAdmin)
{
Expand Down Expand Up @@ -73,7 +73,7 @@ public async Task<IQueryable<Project>> AddProjectMember(
LoggedInContext loggedInContext,
AddProjectMemberInput input,
LexBoxDbContext dbContext,
[Service] IEmailService emailService)
IEmailService emailService)
{
await permissionService.AssertCanManageProject(input.ProjectId);
var project = await dbContext.Projects.FindAsync(input.ProjectId);
Expand Down Expand Up @@ -248,7 +248,7 @@ public async Task<IQueryable<Project>> AskToJoinProject(
LoggedInContext loggedInContext,
Guid projectId,
LexBoxDbContext dbContext,
[Service] IEmailService emailService)
IEmailService emailService)
{
await permissionService.AssertCanAskToJoinProject(projectId);

Expand Down Expand Up @@ -320,7 +320,7 @@ public async Task<IQueryable<Project>> ChangeProjectDescription(ChangeProjectDes
[UseProjection]
public async Task<IQueryable<Project>> SetProjectConfidentiality(SetProjectConfidentialityInput input,
IPermissionService permissionService,
[Service] ProjectService projectService,
ProjectService projectService,
LexBoxDbContext dbContext)
{
await permissionService.AssertCanManageProject(input.ProjectId);
Expand All @@ -342,7 +342,7 @@ public async Task<IQueryable<Project>> SetProjectConfidentiality(SetProjectConfi
public async Task<IQueryable<Project>> SetRetentionPolicy(
SetRetentionPolicyInput input,
IPermissionService permissionService,
[Service] ProjectService projectService,
ProjectService projectService,
LexBoxDbContext dbContext)
{
await permissionService.AssertCanManageProject(input.ProjectId);
Expand All @@ -363,7 +363,7 @@ public async Task<IQueryable<Project>> SetRetentionPolicy(
[UseProjection]
public async Task<IQueryable<Project>> UpdateProjectLexEntryCount(string code,
IPermissionService permissionService,
[Service] ProjectService projectService,
ProjectService projectService,
LexBoxDbContext dbContext)
{
var projectId = await projectService.LookupProjectId(code);
Expand All @@ -382,7 +382,7 @@ public async Task<IQueryable<Project>> UpdateProjectLexEntryCount(string code,
[UseProjection]
public async Task<IQueryable<Project>> UpdateProjectLanguageList(string code,
IPermissionService permissionService,
[Service] ProjectService projectService,
ProjectService projectService,
LexBoxDbContext dbContext)
{
var projectId = await projectService.LookupProjectId(code);
Expand All @@ -401,7 +401,7 @@ public async Task<IQueryable<Project>> UpdateProjectLanguageList(string code,
[UseProjection]
public async Task<IQueryable<Project>> UpdateLangProjectId(string code,
IPermissionService permissionService,
[Service] ProjectService projectService,
ProjectService projectService,
LexBoxDbContext dbContext)
{
var projectId = await projectService.LookupProjectId(code);
Expand All @@ -420,7 +420,7 @@ public async Task<IQueryable<Project>> UpdateLangProjectId(string code,
[UseProjection]
public async Task<IQueryable<Project>> UpdateFLExModelVersion(string code,
IPermissionService permissionService,
[Service] ProjectService projectService,
ProjectService projectService,
LexBoxDbContext dbContext)
{
var projectId = await projectService.LookupProjectId(code);
Expand Down Expand Up @@ -499,7 +499,7 @@ public async Task<DraftProject> DeleteDraftProject(
public async Task<IQueryable<Project>> SoftDeleteProject(
Guid projectId,
IPermissionService permissionService,
[Service] ProjectService projectService,
ProjectService projectService,
LexBoxDbContext dbContext,
IHgService hgService)
{
Expand Down
14 changes: 7 additions & 7 deletions backend/LexBoxApi/LexBoxApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
<ItemGroup>
<PackageReference Include="AppAny.Quartz.EntityFrameworkCore.Migrations.PostgreSQL" Version="0.5.1" />
<PackageReference Include="CrystalQuartz.AspNetCore" Version="7.2.0-beta" />
<PackageReference Include="DataAnnotatedModelValidations" Version="5.2.0" />
<PackageReference Include="HotChocolate.Analyzers" Version="13.9.11" />
<PackageReference Include="HotChocolate.Types.Analyzers" Version="13.9.11">
<PackageReference Include="DataAnnotatedModelValidations" Version="6.0.0" />
<PackageReference Include="HotChocolate.Analyzers" Version="13.9.14" />
<PackageReference Include="HotChocolate.Types.Analyzers" Version="14.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="HotChocolate.AspNetCore" Version="13.9.11" />
<PackageReference Include="HotChocolate.AspNetCore.Authorization" Version="13.9.11" />
<PackageReference Include="HotChocolate.Data.EntityFramework" Version="13.9.11" />
<PackageReference Include="HotChocolate.Diagnostics" Version="13.9.11" />
<PackageReference Include="HotChocolate.AspNetCore" Version="14.0.0" />
<PackageReference Include="HotChocolate.AspNetCore.Authorization" Version="14.0.0" />
<PackageReference Include="HotChocolate.Data.EntityFramework" Version="14.0.0" />
<PackageReference Include="HotChocolate.Diagnostics" Version="14.0.0" />
<PackageReference Include="Humanizer.Core" Version="2.14.1" />
<PackageReference Include="MailKit" Version="4.7.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Google" Version="8.0.10" />
Expand Down
3 changes: 2 additions & 1 deletion backend/LexBoxApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Text.Json.Serialization;
using AppAny.Quartz.EntityFrameworkCore.Migrations;
using AppAny.Quartz.EntityFrameworkCore.Migrations.PostgreSQL;
using HotChocolate.AspNetCore;
using LexBoxApi;
using LexBoxApi.Auth;
using LexBoxApi.Auth.Attributes;
Expand Down Expand Up @@ -167,7 +168,7 @@
app.UseAuthentication();
app.UseAuthorization();
app.MapSecurityTxt();
app.MapBananaCakePop("/api/graphql/ui").AllowAnonymous();
app.MapNitroApp("/api/graphql/ui").WithOptions(new (){ServeMode = GraphQLToolServeMode.Embedded}).AllowAnonymous();
if (app.Environment.IsDevelopment())
//required for vite to generate types
app.MapGraphQLSchema("/api/graphql/schema.graphql").AllowAnonymous();
Expand Down
19 changes: 16 additions & 3 deletions backend/LexBoxApi/Services/DevGqlSchemaWriterService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
using HotChocolate.Execution;
using LexBoxApi.Auth;
using LexBoxApi.GraphQL;
using LexBoxApi.GraphQL.CustomTypes;
using LexBoxApi.Services.Email;
using LexCore.ServiceInterfaces;
using LexData;
using Microsoft.Extensions.Hosting.Internal;

namespace LexBoxApi.Services;
Expand All @@ -18,9 +23,17 @@ public static bool IsSchemaGenerationRequest(string[] args)
public static async Task GenerateGqlSchema(string[] args)
{
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddLogging();
builder.Services.AddSingleton<IHostLifetime, ConsoleLifetime>();
builder.Services.AddLexGraphQL(builder.Environment, true);
builder.Services
.AddLogging()
.AddSingleton<IHostLifetime, ConsoleLifetime>()
.AddScoped<IEmailService, EmailService>()
.AddScoped<IHgService, HgService>()
.AddScoped<IIsLanguageForgeProjectDataLoader, IsLanguageForgeProjectDataLoader>()
.AddScoped((services) => new LoggedInContext(null!, null!))
.AddScoped((services) => new LexBoxDbContext(null!, null!))
.AddScoped<IPermissionService, PermissionService>()
.AddScoped<ProjectService>()
.AddLexGraphQL(builder.Environment, true);
var host = builder.Build();
await host.StartAsync();
await host.StopAsync();
Expand Down
Loading

0 comments on commit e83930d

Please sign in to comment.