Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
116 lines (94 loc) · 3.83 KB
/
Copy pathProgram.cs
File metadata and controls
116 lines (94 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using HotChocolate.AspNetCore;
using HotChocolate.Demo.Application;
using HotChocolate.Demo.Data;
using HotChocolate.Demo.GraphQL;
using HotChocolate.Demo.GraphQL.DataLoaders;
using HotChocolate.Demo.GraphQL.Types;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using System.Text;
var builder = WebApplication.CreateBuilder(args);
// -----------------------------------------------------------------------------
// 1) Persistence (EF Core, hier: InMemory fuer die Demo)
// -----------------------------------------------------------------------------
builder.Services.AddPooledDbContextFactory<LibraryDbContext>(options =>
options.UseInMemoryDatabase("library-demo"));
// -----------------------------------------------------------------------------
// 2) Application Services (Use-Cases)
// -----------------------------------------------------------------------------
builder.Services.AddScoped<BookService>();
builder.Services.AddScoped<AuthorService>();
// -----------------------------------------------------------------------------
// 3) Authentication (Demo: symmetrischer JWT-Key)
// -----------------------------------------------------------------------------
var jwtKey = builder.Configuration["Jwt:Key"]
?? "ENTWICKLUNGS-KEY-NUR-FUER-DEMO-ZWECKE-MIN-32-ZEICHEN";
builder.Services
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = false,
ValidateAudience = false,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtKey))
};
});
builder.Services.AddAuthorization(options =>
{
// Policy fuer Lese-Operationen
options.AddPolicy("reader", p => p.RequireAuthenticatedUser());
// Policy fuer schreibende Operationen (Rollen-basiert)
options.AddPolicy("librarian", p =>
p.RequireAuthenticatedUser().RequireRole("librarian", "admin"));
});
// -----------------------------------------------------------------------------
// 4) GraphQL Server Konfiguration
// -----------------------------------------------------------------------------
builder.Services
.AddGraphQLServer()
// Root-Types
.AddQueryType<Query>()
.AddMutationType<Mutation>()
.AddSubscriptionType<Subscription>()
// Object Types (explicit, Code-First)
.AddType<BookType>()
.AddType<AuthorType>()
// Error-Filter (Mapping von Exceptions zu GraphQL-Errors)
.AddErrorFilter<DomainErrorFilter>()
// Middleware
.AddFiltering()
.AddSorting()
.AddProjections()
// Authorization
.AddAuthorization()
// Subscriptions (InMemory)
.AddInMemorySubscriptions()
// Persisted Queries (optional, fuer Production empfohlen)
// .AddReadOnlyFileSystemQueryStorage("./persisted_queries")
// .UsePersistedQueryPipeline()
// Komplexitaets-/Sicherheits-Limits
.ModifyRequestOptions(opt =>
{
opt.IncludeExceptionDetails = builder.Environment.IsDevelopment();
})
.AddMaxExecutionDepthRule(10)
// DataLoader registrieren (Batch-Loader gegen N+1)
.AddDataLoader<AuthorByIdDataLoader>()
.AddDataLoader<BooksByAuthorIdDataLoader>();
// -----------------------------------------------------------------------------
// 5) App bauen
// -----------------------------------------------------------------------------
var app = builder.Build();
// Seed-Daten fuer die Demo
await DatabaseSeeder.SeedAsync(app.Services);
app.UseAuthentication();
app.UseAuthorization();
// WebSockets fuer Subscriptions
app.UseWebSockets();
// GraphQL-Endpunkt (inkl. Banana Cake Pop IDE unter /graphql)
app.MapGraphQL("/graphql");
app.Run();