-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move GraphQl api to separate project (#70)
Co-authored-by: admin <admin@DESKTOP-HNFM8BH>
- Loading branch information
Showing
19 changed files
with
350 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<UserSecretsId>656cca3d-9e2f-40e4-99e6-5d40688db1ca</UserSecretsId> | ||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="HotChocolate.AspNetCore" Version="13.7.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Api\Api.csproj" /> | ||
<ProjectReference Include="..\Logic\Logic.csproj" /> | ||
<ProjectReference Include="..\Services\ElasticSoDatabase\ElasticSoDatabase.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging. | ||
|
||
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base | ||
WORKDIR /app | ||
EXPOSE 80 | ||
EXPOSE 443 | ||
|
||
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build | ||
WORKDIR /src | ||
COPY ["ApiGraphQl/ApiGraphQl.csproj", "ApiGraphQl/"] | ||
RUN dotnet restore "ApiGraphQl/ApiGraphQl.csproj" | ||
COPY . . | ||
WORKDIR "/src/ApiGraphQl" | ||
RUN dotnet build "ApiGraphQl.csproj" -c Release -o /app/build | ||
|
||
FROM build AS publish | ||
RUN dotnet publish "ApiGraphQl.csproj" -c Release -o /app/publish /p:UseAppHost=false | ||
|
||
FROM base AS final | ||
WORKDIR /app | ||
COPY --from=publish /app/publish . | ||
ENTRYPOINT ["dotnet", "ApiGraphQl.dll"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace ApiGraphQl.GraphQlSchema.Errors | ||
{ | ||
public record BusinessLogicError(string Message) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
namespace ApiGraphQl.GraphQlSchema.Errors | ||
{ | ||
public record ValidationError | ||
{ | ||
public string Message { get; } | ||
|
||
public ValidationError(FluentValidation.Results.ValidationResult validationResult) | ||
{ | ||
if (validationResult is null) | ||
throw new ArgumentNullException(nameof(validationResult)); | ||
if (validationResult.IsValid) | ||
throw new ArgumentException("Invalid validationResult state", nameof(validationResult)); | ||
|
||
var errors = validationResult.Errors? | ||
.Select(x => x.ErrorMessage)? | ||
.ToArray() ?? Array.Empty<string>(); | ||
|
||
Message = string.Join(" ", errors); | ||
} | ||
|
||
public ValidationError(string message) | ||
{ | ||
if (string.IsNullOrWhiteSpace(message)) | ||
throw new ArgumentNullException(nameof(message)); | ||
|
||
Message = message; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using Api.Args.Post; | ||
using ApiGraphQl.GraphQlSchema.Errors; | ||
using FluentValidation; | ||
using Logic.BoundedContexts.Posts.Commands; | ||
using MediatR; | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace ApiGraphQl.GraphQlSchema | ||
{ | ||
public partial class Mutation | ||
{ | ||
public async Task<MutationResult<int, ValidationError, BusinessLogicError>> UpVote(int postId, int userId, | ||
IMediator mediator, IValidatorFactory validatorFactory) | ||
{ | ||
if (postId < 0) | ||
return new ValidationError("Invalid post id"); | ||
|
||
var validationResult = validatorFactory.GetValidator<UpVoteArgs>().Validate(new UpVoteArgs | ||
{ | ||
UserId = userId | ||
}); | ||
if (!validationResult.IsValid) | ||
return new ValidationError(validationResult); | ||
|
||
var result = await mediator.Send(new UpVoteCommand | ||
( | ||
postId: postId, | ||
userId: userId | ||
)); | ||
if (result.IsFailure) | ||
return new BusinessLogicError(result.Error); | ||
|
||
return 1; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using Api; | ||
using ApiGraphQl.GraphQlSchema; | ||
using ApiGraphQl.Utils; | ||
using ElasticSoDatabase.Utils; | ||
using FluentValidation; | ||
using HotChocolate.Subscriptions; | ||
using Logic.Utils; | ||
using Logic.Utils.Db; | ||
using MediatR; | ||
using Microsoft.FeatureManagement; | ||
using Microsoft.FeatureManagement.FeatureFilters; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
builder.Services.AddSingleton<IDateTimeProvider, DateTimeProvider>(); | ||
|
||
string commandConnectionString = builder.Configuration.GetConnectionString("SO_Database"); | ||
string queryConectionString = builder.Configuration.GetConnectionString("SO_ReadonlyDatabase"); | ||
|
||
builder.Services.AddDbContexts(commandConnectionString, queryConectionString); | ||
|
||
builder.Services.AddElasticsearch( | ||
builder.Configuration.GetValue<string>("Elasticsearch:Url"), | ||
builder.Configuration.GetValue<string>("Elasticsearch:DefaultIndexName")); | ||
|
||
builder.Services.AddMediatR( | ||
typeof(DatabaseContext).Assembly, | ||
typeof(ElasticsearchExtensions).Assembly); | ||
|
||
AddFeatureFlags(builder); | ||
|
||
AddFluentValidation(builder); | ||
|
||
builder.Services | ||
.AddGraphQLServer() | ||
.RegisterService<IMediator>() | ||
.RegisterService<IValidatorFactory>() | ||
.RegisterService<ITopicEventSender>() | ||
.AddQueryType<Query>() | ||
.AddMutationType<Mutation>() | ||
.AddMutationConventions() | ||
.AddInMemorySubscriptions(); | ||
|
||
var app = builder.Build(); | ||
|
||
app.MapGraphQL(); | ||
|
||
app.Run(); | ||
|
||
|
||
static void AddFeatureFlags(WebApplicationBuilder builder) | ||
{ | ||
builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); | ||
builder.Services.AddSingleton<ITargetingContextAccessor, HttpContextTargetingContextAccessor>(); | ||
|
||
builder.Services | ||
.AddFeatureManagement(builder.Configuration.GetSection("FeatureFlags")) | ||
.AddFeatureFilter<PercentageFilter>() | ||
.AddFeatureFilter<TimeWindowFilter>() | ||
.AddFeatureFilter<TargetingFilter>(); | ||
} | ||
|
||
static void AddFluentValidation(WebApplicationBuilder builder) | ||
{ | ||
builder.Services.AddValidatorsFromAssemblyContaining<Startup>(); | ||
builder.Services.AddTransient<IValidatorFactory, ServiceProviderValidatorFactory>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"profiles": { | ||
"ApiGraphQl": { | ||
"commandName": "Project", | ||
"launchBrowser": false, | ||
"launchUrl": "graphql", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
}, | ||
"dotnetRunMessages": true, | ||
"applicationUrl": "https://localhost:7039;http://localhost:5121" | ||
}, | ||
"Docker": { | ||
"commandName": "Docker", | ||
"launchBrowser": true, | ||
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/graphql", | ||
"environmentVariables": { | ||
"ASPNETCORE_URLS": "https://+:443;http://+:80" | ||
}, | ||
"publishAllPorts": true, | ||
"useSSL": true | ||
} | ||
} | ||
} |
Oops, something went wrong.