-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathStartup.cs
119 lines (106 loc) · 3.91 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
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;
using System.Collections.Generic;
#if !NETCOREAPP2_2
using Microsoft.AspNetCore.Server.Kestrel.Core;
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)
{
#if NETCOREAPP3_0
// Workaround until GraphQL can swap off Newtonsoft.Json and onto the new MS one.
// Depending on whether you're using IIS or Kestrel, the code required is different
// See: https://github.com/graphql-dotnet/graphql-dotnet/issues/1116
services.Configure<KestrelServerOptions>(options =>
{
options.AllowSynchronousIO = true;
});
services.Configure<IISServerOptions>(options =>
{
options.AllowSynchronousIO = true;
});
#endif
services
.AddSingleton<IChat, Chat>()
.AddSingleton<ChatSchema>()
.AddGraphQL(options =>
{
options.EnableMetrics = true;
options.ExposeExceptions = Environment.IsDevelopment();
options.UnhandledExceptionDelegate = ctx =>
{
Console.WriteLine("error: " + ctx.OriginalException.Message);
};
})
.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",
PlaygroundSettings = new Dictionary<string, object>
{
["editor.theme"] = "light",
["tracing.hideTracingResponse"] = false,
}
});
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",
});
}
}
}