Skip to content

Commit c1b9d4c

Browse files
authored
Add graphql extension methods for dtos (#13)
1 parent 5672c15 commit c1b9d4c

File tree

7 files changed

+87
-2
lines changed

7 files changed

+87
-2
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<Project>
22
<PropertyGroup>
3-
<Version>1.2.0</Version>
3+
<Version>1.2.1</Version>
44
</PropertyGroup>
55
</Project>

index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Welcome to BytLabs Backend Packages documentation. This collection of .NET libra
99
## Quick Navigation
1010

1111
- [Getting Started](docs/getting-started.md)
12-
- [API Documentation](api/index.md)
12+
- [API Documentation](api/BytLabs.Api.md)
1313
- [Introduction](docs/introduction.md)
1414

1515
## Key Features

src/BytLabs.Api/BytLabs.Api.Graphql/RequestExecutorBuilderExtension.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ public static IRequestExecutorBuilder AddCommandType<TCommand>(this IRequestExec
3434
return requestExecutorBuilder;
3535
}
3636

37+
/// <summary>
38+
/// Adds a DTO (Data Transfer Object) type to the GraphQL schema.
39+
/// </summary>
40+
/// <typeparam name="T">The DTO type to add.</typeparam>
41+
/// <param name="requestExecutorBuilder">The GraphQL request executor builder.</param>
42+
/// <returns>The configured request executor builder with added DTO type.</returns>
43+
/// <remarks>
44+
/// This method registers a DTO type using the <see cref="DtoType{T}"/> wrapper to expose it in the GraphQL schema.
45+
/// </remarks>
46+
public static IRequestExecutorBuilder AddDtoType<T>(this IRequestExecutorBuilder requestExecutorBuilder)
47+
{
48+
return requestExecutorBuilder
49+
.AddType<DtoType<T>>();
50+
}
51+
3752
/// <summary>
3853
/// Adds custom error types to the GraphQL schema.
3954
/// </summary>

src/BytLabs.Api/BytLabs.Api.Graphql/Types/AggregateType.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,17 @@
22

33
namespace BytLabs.Api.Graphql.Types
44
{
5+
/// <summary>
6+
/// Base GraphQL type for aggregate root entities.
7+
/// </summary>
8+
/// <typeparam name="TAggregate">The aggregate root type</typeparam>
9+
/// <typeparam name="TId">The type of the aggregate's identifier</typeparam>
510
public class AggregateType<TAggregate, TId> : ObjectType<TAggregate> where TAggregate : IAggregateRoot<TId>
611
{
12+
/// <summary>
13+
/// Configures the GraphQL type descriptor for the aggregate.
14+
/// </summary>
15+
/// <param name="descriptor">The descriptor to configure</param>
716
protected override void Configure(IObjectTypeDescriptor<TAggregate> descriptor)
817
{
918
descriptor.Ignore(a => a.DomainEvents);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using HotChocolate.Data.Filters;
2+
3+
namespace BytLabs.Api.Graphql
4+
{
5+
/// <summary>
6+
/// Base GraphQL filter input type for DTOs.
7+
/// </summary>
8+
/// <typeparam name="T">The DTO type to create filters for</typeparam>
9+
public class DtoFilterInputType<T> : FilterInputType<T>
10+
{
11+
/// <summary>
12+
/// Configures the filter input type descriptor.
13+
/// </summary>
14+
/// <param name="descriptor">The descriptor to configure</param>
15+
protected override void Configure(IFilterInputTypeDescriptor<T> descriptor)
16+
{
17+
base.Configure(descriptor);
18+
descriptor.Name(typeof(T).Name.Replace("Dto", "") + "FilterInput");
19+
}
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using HotChocolate.Data.Sorting;
2+
3+
namespace BytLabs.Api.Graphql
4+
{
5+
/// <summary>
6+
/// Base GraphQL sort input type for DTOs.
7+
/// </summary>
8+
/// <typeparam name="T">The DTO type to create sorting for</typeparam>
9+
public class DtoSortInputType<T> : SortInputType<T>
10+
{
11+
/// <summary>
12+
/// Configures the sort input type descriptor.
13+
/// </summary>
14+
/// <param name="descriptor">The descriptor to configure</param>
15+
protected override void Configure(ISortInputTypeDescriptor<T> descriptor)
16+
{
17+
base.Configure(descriptor);
18+
descriptor.Name(typeof(T).Name.Replace("Dto", "") + "SortInput");
19+
}
20+
}
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace BytLabs.Api.Graphql
2+
{
3+
/// <summary>
4+
/// Base GraphQL type for DTOs.
5+
/// </summary>
6+
/// <typeparam name="T">The DTO type to create a GraphQL type for</typeparam>
7+
public class DtoType<T> : ObjectType<T>
8+
{
9+
/// <summary>
10+
/// Configures the GraphQL type descriptor for the DTO.
11+
/// </summary>
12+
/// <param name="descriptor">The descriptor to configure</param>
13+
protected override void Configure(IObjectTypeDescriptor<T> descriptor)
14+
{
15+
descriptor.Name(typeof(T).Name.Replace("Dto", ""));
16+
descriptor.BindFieldsImplicitly();
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)