Skip to content
This repository was archived by the owner on Apr 11, 2024. It is now read-only.

Commit 16abcfe

Browse files
authored
feat(enrolling): add GraphQL support (#239)
- add Serilog request logging - add span enricher for serilog
1 parent 3d599cf commit 16abcfe

13 files changed

Lines changed: 95 additions & 8 deletions

File tree

.editorconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ root = true
88
[*]
99
indent_style = space
1010
trim_trailing_whitespace = true
11+
guidelines = 80, 120
1112

1213
# Code files
1314
[*.{cs,csx,vb,vbx}]

src/Services/Enrolling/Enrolling.API/Application/Commands/EnrollmentApplicationCommand.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ namespace OpenCodeFoundation.ESchool.Services.Enrolling.API.Application.Commands
55
public class EnrollmentApplicationCommand
66
: IRequest<bool>
77
{
8-
public string Name { get; set; }
8+
public string Name { get; init; }
99

10-
public string Email { get; set; }
10+
public string Email { get; init; }
1111

12-
public string Mobile { get; set; }
12+
public string Mobile { get; init; }
1313
}
1414
}

src/Services/Enrolling/Enrolling.API/Enrolling.API.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk.Web">
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
44
<TargetFramework>net5.0</TargetFramework>
@@ -7,13 +7,15 @@
77
<RootNamespace>OpenCodeFoundation.ESchool.Services.Enrolling.API</RootNamespace>
88
<GenerateDocumentationFile>true</GenerateDocumentationFile>
99

10+
<Nullable>enable</Nullable>
1011
<CodeAnalysisRuleSet>..\..\..\..\eSchool.ruleset</CodeAnalysisRuleSet>
1112
</PropertyGroup>
1213

1314
<ItemGroup>
1415
<!-- HealthChecks -->
1516
<PackageReference Include="AspNetCore.HealthChecks.SqlServer" Version="3.2.0" />
1617
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="3.1.2" />
18+
<PackageReference Include="HotChocolate.AspNetCore" Version="11.0.0" />
1719

1820
<!-- In-memory commandbus -->
1921
<PackageReference Include="MediatR" Version="9.0.0" />
@@ -29,6 +31,7 @@
2931

3032
<!-- Logging -->
3133
<PackageReference Include="Serilog.AspNetCore" Version="3.4.0" />
34+
<PackageReference Include="Serilog.Enrichers.Span" Version="1.0.1" />
3235
<PackageReference Include="Serilog.Sinks.Seq" Version="4.0.0" />
3336

3437
<!-- Swagger -->

src/Services/Enrolling/Enrolling.API/Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
using Microsoft.Extensions.Hosting;
77
using OpenCodeFoundation.ESchool.Services.Enrolling.Infrastructure;
88
using Serilog;
9+
using Serilog.Enrichers.Span;
910

1011
namespace OpenCodeFoundation.ESchool.Services.Enrolling.API
1112
{
1213
public class Program
1314
{
14-
public static readonly string Namespace = typeof(Program).Namespace;
15+
public static readonly string Namespace = typeof(Program).Namespace!;
1516
public static readonly string AppName = Namespace.Substring(Namespace.LastIndexOf('.', Namespace.LastIndexOf('.') - 1) + 1);
1617

1718
public static int Main(string[] args)
@@ -61,6 +62,7 @@ private static ILogger CreateSerilogLogger(IConfiguration configuration)
6162
.MinimumLevel.Verbose()
6263
.Enrich.WithProperty("ApplicationContext", AppName)
6364
.Enrich.FromLogContext()
65+
.Enrich.WithSpan()
6466
.WriteTo.Console()
6567
.WriteTo.Seq("http://seq")
6668
.ReadFrom.Configuration(configuration)
@@ -75,7 +77,6 @@ private static IConfiguration GetConfiguration()
7577
.AddEnvironmentVariables();
7678

7779
// Load other configurations here. Ex. Keyvault or AppConfiguration
78-
7980
return builder.Build();
8081
}
8182
}

src/Services/Enrolling/Enrolling.API/Startup.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
using OpenCodeFoundation.ESchool.Services.Enrolling.API.Application.Behaviors;
1616
using OpenCodeFoundation.ESchool.Services.Enrolling.API.Application.Validations;
1717
using OpenCodeFoundation.ESchool.Services.Enrolling.API.Extensions;
18+
using OpenCodeFoundation.ESchool.Services.Enrolling.API.Graphql;
1819
using OpenCodeFoundation.ESchool.Services.Enrolling.Infrastructure;
1920
using OpenCodeFoundation.OpenTelemetry;
21+
using Serilog;
2022

2123
namespace OpenCodeFoundation.ESchool.Services.Enrolling.API
2224
{
@@ -47,6 +49,11 @@ public void ConfigureServices(IServiceCollection services)
4749
});
4850
});
4951

52+
services.AddGraphQLServer()
53+
.AddQueryType<Query>()
54+
.AddMutationType<Mutation>()
55+
.AddErrorFilter<GraphQlErrorFilter>();
56+
5057
services.AddControllers()
5158
.AddJsonOptions(options =>
5259
{
@@ -80,6 +87,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF
8087
app.UseDeveloperExceptionPage();
8188
}
8289

90+
app.UseSerilogRequestLogging();
91+
8392
app.UseSwagger()
8493
.UseSwaggerUI(c =>
8594
{
@@ -103,6 +112,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF
103112
{
104113
Predicate = r => r.Name.Contains("self"),
105114
});
115+
116+
endpoints.MapGraphQL();
106117
});
107118
}
108119
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using HotChocolate;
2+
3+
namespace OpenCodeFoundation.ESchool.Services.Enrolling.API.Graphql
4+
{
5+
public class GraphQlErrorFilter
6+
: IErrorFilter
7+
{
8+
public IError OnError(IError error)
9+
{
10+
return error.WithMessage(
11+
error.Exception?.Message ?? string.Empty);
12+
}
13+
}
14+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.Threading.Tasks;
2+
using HotChocolate;
3+
using OpenCodeFoundation.ESchool.Services.Enrolling.API.Application.Commands;
4+
using OpenCodeFoundation.ESchool.Services.Enrolling.Domain.AggregatesModel.EnrollmentAggregate;
5+
using OpenCodeFoundation.ESchool.Services.Enrolling.Infrastructure;
6+
7+
namespace OpenCodeFoundation.ESchool.Services.Enrolling.API.Graphql
8+
{
9+
public class Mutation
10+
{
11+
public async Task<Enrollment> AddEnrollmentAsync(
12+
EnrollmentApplicationCommand input,
13+
[Service] EnrollingContext context)
14+
{
15+
var enrollment = new Enrollment(
16+
input.Name,
17+
input.Email,
18+
input.Mobile);
19+
20+
await context.Enrollments.AddAsync(enrollment);
21+
await context.SaveChangesAsync();
22+
return enrollment;
23+
}
24+
}
25+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
using HotChocolate;
4+
using Microsoft.EntityFrameworkCore;
5+
using Microsoft.Extensions.Logging;
6+
using OpenCodeFoundation.ESchool.Services.Enrolling.Domain.AggregatesModel.EnrollmentAggregate;
7+
using OpenCodeFoundation.ESchool.Services.Enrolling.Infrastructure;
8+
9+
namespace OpenCodeFoundation.ESchool.Services.Enrolling.API.Graphql
10+
{
11+
public class Query
12+
{
13+
public async Task<List<Enrollment>> GetEnrollmentsAsync(
14+
[Service] EnrollingContext context,
15+
[Service] ILogger<Query> logger)
16+
{
17+
var enrollments = await context.Enrollments
18+
.ToListAsync();
19+
20+
logger.LogInformation(
21+
"Returning enrollments {EnrollmentCount} with payload {@Enrollment}",
22+
enrollments.Count,
23+
enrollments);
24+
25+
return enrollments;
26+
}
27+
}
28+
}

src/Services/Enrolling/Enrolling.Domain/Enrolling.Domain.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<RootNamespace>OpenCodeFoundation.ESchool.Services.Enrolling.Domain</RootNamespace>
88
<GenerateDocumentationFile>true</GenerateDocumentationFile>
99

10+
<Nullable>enable</Nullable>
1011
<CodeAnalysisRuleSet>..\..\..\..\eSchool.ruleset</CodeAnalysisRuleSet>
1112
</PropertyGroup>
1213

src/Services/Enrolling/Enrolling.FunctionalTests/TestServerFixture.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.Extensions.Hosting;
99
using OpenCodeFoundation.ESchool.Services.Enrolling.API;
1010
using OpenCodeFoundation.ESchool.Services.Enrolling.Infrastructure;
11+
using Serilog;
1112

1213
namespace OpenCodeFoundation.ESchool.Services.Enrolling.FunctionalTests
1314
{
@@ -40,6 +41,7 @@ public IHost CreateHost()
4041
config.AddJsonFile("appsettings.json", optional: false)
4142
.AddEnvironmentVariables();
4243
});
44+
webBuilder.UseSerilog();
4345
});
4446

4547
return builder.Start();

0 commit comments

Comments
 (0)