-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Program.cs
49 lines (40 loc) · 1.53 KB
/
Program.cs
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
using GraphQL;
using GraphQL.Types;
using Microsoft.Extensions.DependencyInjection.Extensions;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHttpContextAccessor();
builder.Services.TryAddSingleton<ISchema>(_ =>
{
const string definitions = """
type User {
id: ID
name: String
}
type Query {
viewer: User
users: [User]
guest: String
}
""";
var schema = Schema.For(definitions, builder => builder.Types.Include<Query>());
schema.AllTypes["User"]!.AuthorizeWithPolicy("AdminPolicy");
return schema;
});
// Claims principal must look something like this to allow access.
// var user = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim("role", "Admin") }));
builder.Services.AddGraphQL(builder => builder
.AddSystemTextJson()
.ConfigureExecutionOptions(opt =>
{
opt.Root = new Query();
// opt.User = user; // User property has already been initialized. Uncomment to play with ClaimsPrincipal.
})
.AddErrorInfoProvider(opt => opt.ExposeExceptionDetails = true)
.AddAuthorization(settings => settings.AddPolicy("AdminPolicy", p => p.RequireClaim("role", "Admin"))));
var app = builder.Build();
if (app.Environment.IsDevelopment())
app.UseDeveloperExceptionPage();
app.UseGraphQL();
app.UseGraphQLGraphiQL();
app.UseGraphQLPlayground(options: new GraphQL.Server.Ui.Playground.PlaygroundOptions { SchemaPollingEnabled = false });
app.Run();