2026-02-05 21:35:26 +01:00
|
|
|
using Books.Api.Authorization;
|
2026-01-18 02:52:30 +01:00
|
|
|
using Books.Api.Domain.Companies;
|
2026-02-05 21:35:26 +01:00
|
|
|
using Books.Api.Domain.UserAccess;
|
2026-01-18 02:52:30 +01:00
|
|
|
using Books.Api.EventFlow.Repositories;
|
|
|
|
|
using Books.Api.GraphQL.Types;
|
|
|
|
|
using GraphQL;
|
|
|
|
|
using GraphQL.Types;
|
|
|
|
|
|
|
|
|
|
namespace Books.Api.GraphQL.Queries;
|
|
|
|
|
|
|
|
|
|
public class BooksQuery : ObjectGraphType
|
|
|
|
|
{
|
|
|
|
|
public BooksQuery()
|
|
|
|
|
{
|
|
|
|
|
Name = "Query";
|
|
|
|
|
Description = "Root query for the Books API";
|
|
|
|
|
|
|
|
|
|
// companies: [CompanyType]
|
|
|
|
|
Field<ListGraphType<CompanyType>>("companies")
|
2026-02-05 21:35:26 +01:00
|
|
|
.Description("Get all companies accessible to the current user")
|
2026-01-18 02:52:30 +01:00
|
|
|
.ResolveAsync(async ctx =>
|
|
|
|
|
{
|
2026-02-05 21:35:26 +01:00
|
|
|
var accessService = ctx.RequestServices!.GetRequiredService<ICompanyAccessService>();
|
2026-01-18 02:52:30 +01:00
|
|
|
var repository = ctx.RequestServices!.GetRequiredService<ICompanyRepository>();
|
2026-02-05 21:35:26 +01:00
|
|
|
var userAccesses = await accessService.GetUserCompaniesAsync(ctx.CancellationToken);
|
|
|
|
|
var companyIds = userAccesses.Select(a => CompanyId.With(a.CompanyId)).ToList();
|
|
|
|
|
if (companyIds.Count == 0) return Enumerable.Empty<object>();
|
|
|
|
|
return await repository.GetByIds(companyIds, ctx.CancellationToken);
|
2026-01-18 02:52:30 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// company(id: ID!): CompanyType
|
|
|
|
|
Field<CompanyType>("company")
|
|
|
|
|
.Description("Get a company by ID")
|
|
|
|
|
.Argument<NonNullGraphType<IdGraphType>>("id", "The company ID")
|
|
|
|
|
.ResolveAsync(async ctx =>
|
|
|
|
|
{
|
|
|
|
|
var id = ctx.GetArgument<string>("id");
|
2026-02-05 21:35:26 +01:00
|
|
|
var accessService = ctx.RequestServices!.GetRequiredService<ICompanyAccessService>();
|
|
|
|
|
await accessService.RequireAccessAsync(id, CompanyRole.Viewer, ctx.CancellationToken);
|
2026-01-18 02:52:30 +01:00
|
|
|
var repository = ctx.RequestServices!.GetRequiredService<ICompanyRepository>();
|
|
|
|
|
var companies = await repository.GetByIds([CompanyId.With(id)], ctx.CancellationToken);
|
|
|
|
|
return companies.FirstOrDefault();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|