| | | 1 | | using System.Diagnostics.CodeAnalysis; |
| | | 2 | | using System.Threading.Tasks; |
| | | 3 | | using GraphQL; |
| | | 4 | | using GraphQL.Execution; |
| | | 5 | | using Microsoft.AspNetCore.Builder; |
| | | 6 | | using Microsoft.AspNetCore.Hosting; |
| | | 7 | | using Microsoft.Extensions.DependencyInjection; |
| | | 8 | | using Microsoft.Extensions.Hosting; |
| | | 9 | | using Microsoft.Extensions.Logging; |
| | | 10 | | using Server.Data; |
| | | 11 | | using Server.Schema; |
| | | 12 | | |
| | | 13 | | namespace Server; |
| | | 14 | | |
| | | 15 | | [SuppressMessage("Style", "S2325:Methods and properties that don't access instance data should be static")] |
| | | 16 | | public class Startup |
| | | 17 | | { |
| | | 18 | | // This method gets called by the runtime. Use this method to add services to the container. |
| | | 19 | | public void ConfigureServices(IServiceCollection services) |
| | | 20 | | { |
| | 6 | 21 | | services.AddControllers(); |
| | 6 | 22 | | services |
| | 6 | 23 | | .AddSingleton<IUsersRepository, UsersRepository>() |
| | 12 | 24 | | .AddMediatR(c => c.RegisterServicesFromAssemblyContaining<Startup>()); |
| | | 25 | | |
| | 6 | 26 | | services |
| | 6 | 27 | | .AddGraphQL(b => b |
| | 6 | 28 | | .AddSchema<DemoSchema>() |
| | 6 | 29 | | .AddSystemTextJson() |
| | 6 | 30 | | .AddErrorInfoProvider(opt => opt.ExposeExceptionDetailsMode = ExposeExceptionDetailsMode.Message) |
| | 6 | 31 | | .AddGraphTypes(typeof(DemoSchema).Assembly) // Add all IGraphType implementors in the assembly which Dem |
| | 6 | 32 | | .ConfigureExecutionOptions(options => |
| | 6 | 33 | | { |
| | 48 | 34 | | options.EnableMetrics = true; |
| | 6 | 35 | | // Unhandled exception delegate will be set in Configure method with proper DI |
| | 54 | 36 | | }) |
| | 6 | 37 | | ); |
| | 6 | 38 | | } |
| | | 39 | | |
| | | 40 | | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. |
| | | 41 | | public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> logger) |
| | | 42 | | { |
| | | 43 | | // Configure GraphQL unhandled exception handling with injected logger |
| | 6 | 44 | | var executionOptions = app.ApplicationServices.GetRequiredService<Microsoft.Extensions.Options.IOptions<Executio |
| | 6 | 45 | | executionOptions.Value.UnhandledExceptionDelegate = ctx => |
| | 6 | 46 | | { |
| | 0 | 47 | | logger.LogError("{Error} occurred", ctx.OriginalException.Message); |
| | 0 | 48 | | return Task.CompletedTask; |
| | 6 | 49 | | }; |
| | | 50 | | |
| | 6 | 51 | | if (env.IsDevelopment()) |
| | | 52 | | { |
| | 6 | 53 | | app.UseDeveloperExceptionPage(); |
| | | 54 | | } |
| | | 55 | | |
| | 6 | 56 | | app.UseHttpsRedirection(); |
| | | 57 | | |
| | 6 | 58 | | app.UseRouting(); |
| | | 59 | | |
| | 6 | 60 | | app.UseEndpoints(endpoints => |
| | 6 | 61 | | { |
| | 6 | 62 | | // map HTTP middleware for schema at the default path /graphql |
| | 6 | 63 | | endpoints.MapGraphQL<DemoSchema>(); |
| | 6 | 64 | | |
| | 6 | 65 | | // map ui |
| | 6 | 66 | | endpoints.MapGraphQLGraphiQL("/"); |
| | 12 | 67 | | }); |
| | 6 | 68 | | } |
| | | 69 | | } |