|
9 | 9 |
|
10 | 10 | namespace GraphQL.AspNet.StarWarsAPI6X
|
11 | 11 | {
|
12 |
| - using Microsoft.AspNetCore.Hosting; |
13 |
| - using Microsoft.Extensions.Hosting; |
| 12 | + using System; |
| 13 | + using GraphQL.AspNet.Configuration; |
| 14 | + using GraphQL.AspNet.Execution; |
| 15 | + using GraphQL.AspNet.StarwarsAPI.Common.Services; |
| 16 | + using Microsoft.AspNetCore.Builder; |
| 17 | + using Microsoft.AspNetCore.WebSockets; |
| 18 | + using Microsoft.Extensions.DependencyInjection; |
14 | 19 |
|
15 | 20 | public static class Program
|
16 | 21 | {
|
| 22 | + private const string ALL_ORIGINS_POLICY = "_allOrigins"; |
| 23 | + |
| 24 | + private static readonly TimeSpan SOCKET_CONNECTION_KEEPALIVE = TimeSpan.FromSeconds(10); |
| 25 | + |
17 | 26 | public static void Main(string[] args)
|
18 | 27 | {
|
19 |
| - CreateHostBuilder(args).Build().Run(); |
20 |
| - } |
| 28 | + var builder = WebApplication.CreateBuilder(args); |
| 29 | + |
| 30 | + // Add various required services |
| 31 | + // ---------------------------------------------- |
| 32 | + builder.Services.AddAuthorization(); |
| 33 | + builder.Services.AddSingleton<StarWarsDataRepository>(); |
| 34 | + builder.Services.AddScoped<IStarWarsDataService, StarWarsDataService>(); |
| 35 | + |
| 36 | + // apply an unrestricted cors policy for the demo services |
| 37 | + // to allow use on many of the tools for testing (graphiql, altair etc.) |
| 38 | + // Do not do this in production |
| 39 | + builder.Services.AddCors(options => |
| 40 | + { |
| 41 | + options.AddPolicy( |
| 42 | + ALL_ORIGINS_POLICY, |
| 43 | + builder => |
| 44 | + { |
| 45 | + builder.AllowAnyOrigin() |
| 46 | + .AllowAnyHeader() |
| 47 | + .AllowAnyMethod(); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + // ---------------------------------------------------------- |
| 52 | + // Register GraphQL with the application |
| 53 | + // ---------------------------------------------------------- |
| 54 | + // By default graphql will scan your assembly for any GraphControllers |
| 55 | + // and automatically wire them up to the schema |
| 56 | + // you can control which assemblies are scanned and which classes are registered using |
| 57 | + // the schema configuration options set here. |
| 58 | + // |
| 59 | + // in this example because of the two test projects (netcore3.1 and net5.0) |
| 60 | + // we have moved all the shared code to a common assembly (starwars-common) and are injecting it |
| 61 | + // as a single unit |
| 62 | + // |
| 63 | + // we then add subscription services to the schema builder returned from .AddGraphQL() |
| 64 | + builder.Services.AddGraphQL(options => |
| 65 | + { |
| 66 | + options.ResponseOptions.ExposeExceptions = true; |
| 67 | + options.ResponseOptions.MessageSeverityLevel = GraphMessageSeverity.Information; |
| 68 | + |
| 69 | + // options.ExecutionOptions.EnableMetrics = true; |
| 70 | + // options.ResponseOptions.ExposeMetrics = true; |
| 71 | + |
| 72 | + var assembly = typeof(StarWarsDataRepository).Assembly; |
| 73 | + options.AddAssembly(assembly); |
| 74 | + }) |
| 75 | + .AddSubscriptions(options => |
| 76 | + { |
| 77 | + // this route path is set by default |
| 78 | + // it is listed here just as a matter of example |
| 79 | + options.Route = SubscriptionConstants.Routing.DEFAULT_SUBSCRIPTIONS_ROUTE; |
| 80 | + |
| 81 | + // for some web based graphql tools such as graphiql and graphql-playground |
| 82 | + // the default keep-alive timeout of 2 minutes is too long. |
| 83 | + // |
| 84 | + // still others (like graphql-playground running in electron) do not respond/configure |
| 85 | + // for socket-level ping/pong frames to allow for socket-level keep alives |
| 86 | + // |
| 87 | + // here we set this demo project websocket keep-alive (at the server level) |
| 88 | + // to be below all those thresholds to ensure a hassle free experience. |
| 89 | + // In practice, you should configure your server (both subscription keep alives and socket keep alives) |
| 90 | + // with an interval that is compatiable with your client side environment. |
| 91 | + options.ConnectionKeepAliveInterval = SOCKET_CONNECTION_KEEPALIVE; |
| 92 | + }); |
21 | 93 |
|
22 |
| - public static IHostBuilder CreateHostBuilder(string[] args) => |
23 |
| - Host.CreateDefaultBuilder(args) |
24 |
| - .ConfigureWebHostDefaults(webBuilder => |
25 |
| - { |
26 |
| - webBuilder.UseStartup<Startup>(); |
27 |
| - }); |
| 94 | + // if you have rest controllers this item be sure they are included. |
| 95 | + // Graphql and rest can live side by side in the same project without issue |
| 96 | + // -------------------------------------------------- |
| 97 | + // builder.Services.AddControllers(); |
| 98 | + |
| 99 | + // ASP.NET websockets implementation must also be added to the runtime |
| 100 | + builder.Services.AddWebSockets((options) => |
| 101 | + { |
| 102 | + // here add some common origins of various tools that may be |
| 103 | + // used for running this demo |
| 104 | + // do not add these in a production app |
| 105 | + options.AllowedOrigins.Add("http://localhost:5000"); |
| 106 | + options.AllowedOrigins.Add("http://localhost:4000"); |
| 107 | + options.AllowedOrigins.Add("http://localhost:3000"); |
| 108 | + options.AllowedOrigins.Add("null"); |
| 109 | + |
| 110 | + // some electron-based graphql tools send a file reference |
| 111 | + // as their origin |
| 112 | + // do not add these in a production app |
| 113 | + options.AllowedOrigins.Add("file://"); |
| 114 | + options.AllowedOrigins.Add("ws://"); |
| 115 | + }); |
| 116 | + |
| 117 | + var app = builder.Build(); |
| 118 | + |
| 119 | + // Configure the HTTP request pipeline. |
| 120 | + app.AddStarWarsStartedMessageToConsole(); |
| 121 | + app.UseRouting(); |
| 122 | + app.UseCors(ALL_ORIGINS_POLICY); |
| 123 | + app.UseAuthorization(); |
| 124 | + |
| 125 | + // enable web sockets on this server instance |
| 126 | + // this must be done before a call to 'UseGraphQL' if subscriptions are enabled for any |
| 127 | + // schema otherwise the subscriptions may not register correctly |
| 128 | + app.UseWebSockets(); |
| 129 | + |
| 130 | + // if you have no rest controllers this item can be safely skipped |
| 131 | + // graphql and rest can live side by side in the same project without issue |
| 132 | + // ----------------------------------------------------------------- |
| 133 | + // app.UseEndpoints(endpoints => |
| 134 | + // { |
| 135 | + // endpoints.MapControllers(); |
| 136 | + // }); |
| 137 | + |
| 138 | + // ************************************************************ |
| 139 | + // Finalize the graphql setup by loading the schema, build out the templates for all found graph types |
| 140 | + // and publish the route to hook the graphql runtime to the web. |
| 141 | + // be sure to register it after "UseAuthorization" if you require access to this.User |
| 142 | + // |
| 143 | + // If the construction of your runtime schema has any errors they will be thrown here |
| 144 | + // before your application starts listening for requests. |
| 145 | + // ************************************************************ |
| 146 | + app.UseGraphQL(); |
| 147 | + app.Run(); |
| 148 | + } |
28 | 149 | }
|
29 | 150 | }
|
0 commit comments