Implement UserGroup data loaders

This commit is contained in:
Jordan Dominion
2025-08-17 17:30:55 -04:00
parent 97435c68f0
commit fa6a12b09d
2 changed files with 41 additions and 8 deletions
@@ -1,8 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using GreenDonut;
using GreenDonut.Data;
using HotChocolate;
using HotChocolate.CostAnalysis.Types;
using HotChocolate.Data;
@@ -10,6 +14,8 @@ using HotChocolate.Types;
using HotChocolate.Types.Relay;
using Tgstation.Server.Host.Authority;
using Tgstation.Server.Host.Authority.Core;
using Tgstation.Server.Host.Extensions;
using Tgstation.Server.Host.GraphQL.Transformers;
namespace Tgstation.Server.Host.GraphQL.Types
@@ -25,21 +31,46 @@ namespace Tgstation.Server.Host.GraphQL.Types
/// </summary>
public required PermissionSet PermissionSet { get; set; }
/// <summary>
/// Implements the <see cref="IUserGroupsDataLoader"/>.
/// </summary>
/// <param name="ids">The <see cref="IReadOnlyList{T}"/> of <see cref="UserGroup"/> <see cref="Api.Models.EntityId.Id"/>s to load paired with if the system user should be allowed.</param>
/// <param name="userAuthority">The <see cref="IGraphQLAuthorityInvoker{TAuthority}"/> for the <see cref="IUserAuthority"/>.</param>
/// <param name="queryContext">The <see cref="QueryContext{TEntity}"/> for <see cref="User"/> mapped to an <see cref="AuthorityResponse{TResult}"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation.</param>
/// <returns>A <see cref="ValueTask{TResult}"/> resulting in a <see cref="Dictionary{TKey, TValue}"/> of the requested <see cref="UserGroup"/> <see cref="AuthorityResponse{TResult}"/>s.</returns>
[DataLoader(AccessModifier = DataLoaderAccessModifier.PublicInterface)]
public static ValueTask<Dictionary<long, AuthorityResponse<UserGroup>>> GetUserGroups(
IReadOnlyList<long> ids,
IGraphQLAuthorityInvoker<IUserGroupAuthority> userAuthority,
QueryContext<AuthorityResponse<UserGroup>>? queryContext,
CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(ids);
ArgumentNullException.ThrowIfNull(userAuthority);
return userAuthority.ExecuteDataLoader<Models.UserGroup, UserGroup, UserGroupTransformer>(
(authority, id) => authority.GetId<UserGroup>(id, cancellationToken),
ids,
queryContext);
}
/// <summary>
/// Node resolver for <see cref="User"/>s.
/// </summary>
/// <param name="id">The <see cref="Entity.Id"/> to lookup.</param>
/// <param name="userGroupAuthority">The <see cref="IGraphQLAuthorityInvoker{TAuthority}"/> for the <see cref="IUserGroupAuthority"/>.</param>
/// <param name="userGroupsDataLoader">The <see cref="IUserGroupsDataLoader"/> to use.</param>
/// <param name="queryContext">The <see cref="QueryContext{TEntity}"/> for the operation.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation.</param>
/// <returns>A <see cref="ValueTask"/> resulting in the queried <see cref="User"/>, if present.</returns>
public static ValueTask<UserGroup?> GetUserGroup(
long id,
[Service] IGraphQLAuthorityInvoker<IUserGroupAuthority> userGroupAuthority,
[Service] IUserGroupsDataLoader userGroupsDataLoader,
QueryContext<UserGroup>? queryContext,
CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(userGroupAuthority);
return userGroupAuthority.InvokeTransformableAllowMissing<Models.UserGroup, UserGroup, UserGroupTransformer>(
authority => authority.GetId(id, false, cancellationToken));
ArgumentNullException.ThrowIfNull(userGroupsDataLoader);
return userGroupsDataLoader.LoadAuthorityResponse(queryContext, id, cancellationToken);
}
/// <summary>
@@ -43,14 +43,16 @@ namespace Tgstation.Server.Host.GraphQL.Types
/// Gets a <see cref="UserGroup"/> by <see cref="Entity.Id"/>.
/// </summary>
/// <param name="id">The <see cref="Entity.Id"/> of the <see cref="User"/>.</param>
/// <param name="userGroupAuthority">The <see cref="IGraphQLAuthorityInvoker{TAuthority}"/> for the <see cref="IUserGroupAuthority"/>.</param>
/// <param name="userGroupsDataLoader">The <see cref="IUserGroupsDataLoader"/> to use.</param>
/// <param name="queryContext">The <see cref="QueryContext{TEntity}"/> for the operation.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation.</param>
/// <returns>The <see cref="UserGroup"/> represented by <paramref name="id"/>, if any.</returns>
public ValueTask<UserGroup?> ById(
[ID(nameof(UserGroup))] long id,
[Service] IGraphQLAuthorityInvoker<IUserGroupAuthority> userGroupAuthority,
[Service] IUserGroupsDataLoader userGroupsDataLoader,
QueryContext<UserGroup>? queryContext,
CancellationToken cancellationToken)
=> UserGroup.GetUserGroup(id, userGroupAuthority, cancellationToken);
=> UserGroup.GetUserGroup(id, userGroupsDataLoader, queryContext, cancellationToken);
/// <summary>
/// Queries all registered <see cref="UserGroup"/>s.