forked from graphql-dotnet/server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStartup.cs
131 lines (115 loc) · 4.44 KB
/
Startup.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
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
127
128
129
130
131
using GraphQL.Samples.Schemas.Chat;
using GraphQL.Server;
using GraphQL.Server.Ui.GraphiQL;
using GraphQL.Server.Ui.Playground;
using GraphQL.Server.Ui.Altair;
using GraphQL.Server.Ui.Voyager;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
#if !NETCOREAPP2_2
using Microsoft.Extensions.Hosting;
#endif
namespace GraphQL.Samples.Server
{
public class Startup
{
#if NETCOREAPP2_2
public Startup(IConfiguration configuration, IHostingEnvironment environment)
#else
public Startup(IConfiguration configuration, IWebHostEnvironment environment)
#endif
{
Configuration = configuration;
Environment = environment;
}
public IConfiguration Configuration { get; }
#if NETCOREAPP2_2
public IHostingEnvironment Environment { get; }
#else
public IWebHostEnvironment Environment { get; }
#endif
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services
.AddSingleton<IChat, Chat>()
.AddSingleton<ChatSchema>()
.AddGraphQL((options, provider) =>
{
options.EnableMetrics = Environment.IsDevelopment();
var logger = provider.GetRequiredService<ILogger<Startup>>();
options.UnhandledExceptionDelegate = ctx => logger.LogError("{Error} occured", ctx.OriginalException.Message);
})
#if NETCOREAPP2_2
.AddNewtonsoftJson(deserializerSettings => { }, serializerSettings => { })
#else
.AddSystemTextJson(deserializerSettings => { }, serializerSettings => { })
#endif
.AddErrorInfoProvider(opt => opt.ExposeExceptionStackTrace = Environment.IsDevelopment())
.AddWebSockets()
.AddDataLoader()
.AddGraphTypes(typeof(ChatSchema));
}
// 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.UseGraphQLWebSockets<ChatSchema>("/graphql");
app.UseGraphQL<ChatSchema, GraphQLHttpMiddlewareWithLogs<ChatSchema>>("/graphql");
app.UseGraphQLPlayground(new GraphQLPlaygroundOptions
{
Path = "/ui/playground",
BetaUpdates = true,
RequestCredentials = 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 Dictionary<string, object>
{
["MyHeader1"] = "MyValue",
["MyHeader2"] = 42,
},
});
app.UseGraphiQLServer(new GraphiQLOptions
{
Path = "/ui/graphiql",
GraphQLEndPoint = "/graphql",
});
app.UseGraphQLAltair(new GraphQLAltairOptions
{
Path = "/ui/altair",
GraphQLEndPoint = "/graphql",
Headers = new Dictionary<string, string>
{
["X-api-token"] = "130fh9823bd023hd892d0j238dh",
}
});
app.UseGraphQLVoyager(new GraphQLVoyagerOptions
{
Path = "/ui/voyager",
GraphQLEndPoint = "/graphql",
Headers = new Dictionary<string, object>
{
["MyHeader1"] = "MyValue",
["MyHeader2"] = 42,
},
});
}
}
}