-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathStartupWithRouting.cs
More file actions
126 lines (108 loc) · 4.19 KB
/
StartupWithRouting.cs
File metadata and controls
126 lines (108 loc) · 4.19 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
117
118
119
120
121
122
123
124
125
126
using GraphQL.Execution;
using GraphQL.Samples.Schemas.Chat;
using GraphQL.Server.Transports.AspNetCore;
using GraphQL.Server.Ui.Altair;
using GraphQL.Server.Ui.GraphiQL;
using GraphQL.Server.Ui.Playground;
using GraphQL.Server.Ui.Voyager;
namespace GraphQL.Samples.Complex;
public class StartupWithRouting
{
public StartupWithRouting(IConfiguration configuration, IWebHostEnvironment environment)
{
Configuration = configuration;
Environment = environment;
}
public IConfiguration Configuration { get; }
public IWebHostEnvironment Environment { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services
.AddRouting()
.AddSingleton<IChat, Chat>()
.Configure<ErrorInfoProviderOptions>(opt => opt.ExposeExceptionDetails = Environment.IsDevelopment());
services.AddGraphQL(builder => builder
.AddSchema<ChatSchema>()
.AddAutoClrMappings()
.ConfigureExecutionOptions(options =>
{
var logger = options.RequestServices!.GetRequiredService<ILogger<Startup>>();
options.UnhandledExceptionDelegate = ctx =>
{
logger.LogError("{Error} occurred", ctx.OriginalException.Message);
return Task.CompletedTask;
};
})
.AddSystemTextJson()
.AddErrorInfoProvider<CustomErrorInfoProvider>()
.AddDataLoader()
.AddUserContextBuilder(context => new Dictionary<string, object?> { { "user", context.User.Identity!.IsAuthenticated ? context.User : null } })
.AddGraphTypes(typeof(ChatSchema).Assembly)
.UseApolloTracing(_ => Environment.IsDevelopment()));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app)
{
if (Environment.IsDevelopment())
app.UseDeveloperExceptionPage();
app.UseWebSockets();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGraphQL<GraphQLHttpMiddlewareWithLogs<ChatSchema>>("/graphql", new GraphQLHttpMiddlewareOptions()
{
ReadFormOnPost = true,
});
endpoints.MapGraphQLPlayground(options: new PlaygroundOptions
{
BetaUpdates = true,
RequestCredentials = Server.Ui.Playground.RequestCredentials.Omit,
HideTracingResponse = false,
EditorCursorShape = EditorCursorShape.Line,
EditorTheme = EditorTheme.Light,
EditorFontSize = 14,
EditorReuseHeaders = true,
EditorFontFamily = "Consolas",
PrettierPrintWidth = 80,
PrettierTabWidth = 2,
PrettierUseTabs = true,
SchemaDisableComments = false,
SchemaPollingEnabled = true,
SchemaPollingEndpointFilter = "*localhost*",
SchemaPollingInterval = 5000,
Headers = new()
{
["MyHeader1"] = "MyValue",
["MyHeader2"] = 42,
},
});
endpoints.MapGraphQLGraphiQL(options: new GraphiQLOptions
{
Headers = new()
{
["X-api-token"] = "130fh9823bd023hd892d0j238dh",
}
});
endpoints.MapGraphQLAltair(options: new AltairOptions
{
Headers = new()
{
["X-api-token"] = "130fh9823bd023hd892d0j238dh",
},
SubscriptionsPayload = new()
{
["hello"] = "world",
},
});
endpoints.MapGraphQLVoyager(options: new VoyagerOptions
{
Headers = new()
{
["MyHeader1"] = "MyValue",
["MyHeader2"] = 42,
},
});
});
}
}