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

Commit

Permalink
feat(enrolling): add GraphQL support (#239)
Browse files Browse the repository at this point in the history
- add Serilog request logging
- add span enricher for serilog
  • Loading branch information
ratanparai committed Nov 23, 2020
1 parent 3d599cf commit 16abcfe
Show file tree
Hide file tree
Showing 13 changed files with 95 additions and 8 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Expand Up @@ -8,6 +8,7 @@ root = true
[*]
indent_style = space
trim_trailing_whitespace = true
guidelines = 80, 120

# Code files
[*.{cs,csx,vb,vbx}]
Expand Down
Expand Up @@ -5,10 +5,10 @@ namespace OpenCodeFoundation.ESchool.Services.Enrolling.API.Application.Commands
public class EnrollmentApplicationCommand
: IRequest<bool>
{
public string Name { get; set; }
public string Name { get; init; }

public string Email { get; set; }
public string Email { get; init; }

public string Mobile { get; set; }
public string Mobile { get; init; }
}
}
5 changes: 4 additions & 1 deletion src/Services/Enrolling/Enrolling.API/Enrolling.API.csproj
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
Expand All @@ -7,13 +7,15 @@
<RootNamespace>OpenCodeFoundation.ESchool.Services.Enrolling.API</RootNamespace>
<GenerateDocumentationFile>true</GenerateDocumentationFile>

<Nullable>enable</Nullable>
<CodeAnalysisRuleSet>..\..\..\..\eSchool.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>

<ItemGroup>
<!-- HealthChecks -->
<PackageReference Include="AspNetCore.HealthChecks.SqlServer" Version="3.2.0" />
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="3.1.2" />
<PackageReference Include="HotChocolate.AspNetCore" Version="11.0.0" />

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

<!-- Logging -->
<PackageReference Include="Serilog.AspNetCore" Version="3.4.0" />
<PackageReference Include="Serilog.Enrichers.Span" Version="1.0.1" />
<PackageReference Include="Serilog.Sinks.Seq" Version="4.0.0" />

<!-- Swagger -->
Expand Down
5 changes: 3 additions & 2 deletions src/Services/Enrolling/Enrolling.API/Program.cs
Expand Up @@ -6,12 +6,13 @@
using Microsoft.Extensions.Hosting;
using OpenCodeFoundation.ESchool.Services.Enrolling.Infrastructure;
using Serilog;
using Serilog.Enrichers.Span;

namespace OpenCodeFoundation.ESchool.Services.Enrolling.API
{
public class Program
{
public static readonly string Namespace = typeof(Program).Namespace;
public static readonly string Namespace = typeof(Program).Namespace!;
public static readonly string AppName = Namespace.Substring(Namespace.LastIndexOf('.', Namespace.LastIndexOf('.') - 1) + 1);

public static int Main(string[] args)
Expand Down Expand Up @@ -61,6 +62,7 @@ private static ILogger CreateSerilogLogger(IConfiguration configuration)
.MinimumLevel.Verbose()
.Enrich.WithProperty("ApplicationContext", AppName)
.Enrich.FromLogContext()
.Enrich.WithSpan()
.WriteTo.Console()
.WriteTo.Seq("http://seq")
.ReadFrom.Configuration(configuration)
Expand All @@ -75,7 +77,6 @@ private static IConfiguration GetConfiguration()
.AddEnvironmentVariables();

// Load other configurations here. Ex. Keyvault or AppConfiguration

return builder.Build();
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/Services/Enrolling/Enrolling.API/Startup.cs
Expand Up @@ -15,8 +15,10 @@
using OpenCodeFoundation.ESchool.Services.Enrolling.API.Application.Behaviors;
using OpenCodeFoundation.ESchool.Services.Enrolling.API.Application.Validations;
using OpenCodeFoundation.ESchool.Services.Enrolling.API.Extensions;
using OpenCodeFoundation.ESchool.Services.Enrolling.API.Graphql;
using OpenCodeFoundation.ESchool.Services.Enrolling.Infrastructure;
using OpenCodeFoundation.OpenTelemetry;
using Serilog;

namespace OpenCodeFoundation.ESchool.Services.Enrolling.API
{
Expand Down Expand Up @@ -47,6 +49,11 @@ public void ConfigureServices(IServiceCollection services)
});
});

services.AddGraphQLServer()
.AddQueryType<Query>()
.AddMutationType<Mutation>()
.AddErrorFilter<GraphQlErrorFilter>();

services.AddControllers()
.AddJsonOptions(options =>
{
Expand Down Expand Up @@ -80,6 +87,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF
app.UseDeveloperExceptionPage();
}

app.UseSerilogRequestLogging();

app.UseSwagger()
.UseSwaggerUI(c =>
{
Expand All @@ -103,6 +112,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF
{
Predicate = r => r.Name.Contains("self"),
});
endpoints.MapGraphQL();
});
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/Services/Enrolling/Enrolling.API/graphql/GraphQlErrorFilter.cs
@@ -0,0 +1,14 @@
using HotChocolate;

namespace OpenCodeFoundation.ESchool.Services.Enrolling.API.Graphql
{
public class GraphQlErrorFilter
: IErrorFilter
{
public IError OnError(IError error)
{
return error.WithMessage(
error.Exception?.Message ?? string.Empty);
}
}
}
25 changes: 25 additions & 0 deletions src/Services/Enrolling/Enrolling.API/graphql/Mutation.cs
@@ -0,0 +1,25 @@
using System.Threading.Tasks;
using HotChocolate;
using OpenCodeFoundation.ESchool.Services.Enrolling.API.Application.Commands;
using OpenCodeFoundation.ESchool.Services.Enrolling.Domain.AggregatesModel.EnrollmentAggregate;
using OpenCodeFoundation.ESchool.Services.Enrolling.Infrastructure;

namespace OpenCodeFoundation.ESchool.Services.Enrolling.API.Graphql
{
public class Mutation
{
public async Task<Enrollment> AddEnrollmentAsync(
EnrollmentApplicationCommand input,
[Service] EnrollingContext context)
{
var enrollment = new Enrollment(
input.Name,
input.Email,
input.Mobile);

await context.Enrollments.AddAsync(enrollment);
await context.SaveChangesAsync();
return enrollment;
}
}
}
28 changes: 28 additions & 0 deletions src/Services/Enrolling/Enrolling.API/graphql/Query.cs
@@ -0,0 +1,28 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using HotChocolate;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using OpenCodeFoundation.ESchool.Services.Enrolling.Domain.AggregatesModel.EnrollmentAggregate;
using OpenCodeFoundation.ESchool.Services.Enrolling.Infrastructure;

namespace OpenCodeFoundation.ESchool.Services.Enrolling.API.Graphql
{
public class Query
{
public async Task<List<Enrollment>> GetEnrollmentsAsync(
[Service] EnrollingContext context,
[Service] ILogger<Query> logger)
{
var enrollments = await context.Enrollments
.ToListAsync();

logger.LogInformation(
"Returning enrollments {EnrollmentCount} with payload {@Enrollment}",
enrollments.Count,
enrollments);

return enrollments;
}
}
}
Expand Up @@ -7,6 +7,7 @@
<RootNamespace>OpenCodeFoundation.ESchool.Services.Enrolling.Domain</RootNamespace>
<GenerateDocumentationFile>true</GenerateDocumentationFile>

<Nullable>enable</Nullable>
<CodeAnalysisRuleSet>..\..\..\..\eSchool.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>

Expand Down
Expand Up @@ -8,6 +8,7 @@
using Microsoft.Extensions.Hosting;
using OpenCodeFoundation.ESchool.Services.Enrolling.API;
using OpenCodeFoundation.ESchool.Services.Enrolling.Infrastructure;
using Serilog;

namespace OpenCodeFoundation.ESchool.Services.Enrolling.FunctionalTests
{
Expand Down Expand Up @@ -40,6 +41,7 @@ public IHost CreateHost()
config.AddJsonFile("appsettings.json", optional: false)
.AddEnvironmentVariables();
});
webBuilder.UseSerilog();
});

return builder.Start();
Expand Down
Expand Up @@ -7,6 +7,7 @@
<RootNamespace>OpenCodeFoundation.ESchool.Services.Enrolling.Infrastructure</RootNamespace>
<GenerateDocumentationFile>true</GenerateDocumentationFile>

<Nullable>enable</Nullable>
<CodeAnalysisRuleSet>..\..\..\..\eSchool.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>

Expand Down
Expand Up @@ -12,7 +12,7 @@ public EnrollingContext(DbContextOptions<EnrollingContext> options)
{
}

public DbSet<Enrollment> Enrollments { get; set; }
public DbSet<Enrollment> Enrollments { get; set; } = default!;
}

/// <summary>
Expand Down
Expand Up @@ -14,7 +14,7 @@ protected override void Up(MigrationBuilder migrationBuilder)
Id = table.Column<Guid>(nullable: false),
Name = table.Column<string>(nullable: true),
EmailAddress = table.Column<string>(nullable: true),
MobileNumber = table.Column<string>(nullable: true)
MobileNumber = table.Column<string>(nullable: true),
},
constraints: table =>
{
Expand Down

0 comments on commit 16abcfe

Please sign in to comment.