Skip to content

.NET 8: Updated star wars project #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/ancillary-projects/starwars/starwars-api70/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ public void ConfigureServices(IServiceCollection services)
options.ConnectionKeepAliveInterval = SOCKET_CONNECTION_KEEPALIVE;
});

services.AddControllers();
// if you have rest controllers this item be sure they are included.
// Graphql and rest can live side by side in the same project without issue
// --------------------------------------------------
// builder.Services.AddControllers();
}

/// <summary>
Expand All @@ -144,10 +147,10 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

// if you have no rest controllers this item can be safely skipped
// graphql and rest can live side by side in the same project without issue
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
// app.UseEndpoints(endpoints =>
// {
// endpoints.MapControllers();
// });

// ************************************************************
// Finalize the graphql setup:
Expand Down
141 changes: 131 additions & 10 deletions src/ancillary-projects/starwars/starwars-api80/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,142 @@

namespace GraphQL.AspNet.StarWarsAPI6X
{
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using System;
using GraphQL.AspNet.Configuration;
using GraphQL.AspNet.Execution;
using GraphQL.AspNet.StarwarsAPI.Common.Services;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.WebSockets;
using Microsoft.Extensions.DependencyInjection;

public static class Program
{
private const string ALL_ORIGINS_POLICY = "_allOrigins";

private static readonly TimeSpan SOCKET_CONNECTION_KEEPALIVE = TimeSpan.FromSeconds(10);

public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
var builder = WebApplication.CreateBuilder(args);

// Add various required services
// ----------------------------------------------
builder.Services.AddAuthorization();
builder.Services.AddSingleton<StarWarsDataRepository>();
builder.Services.AddScoped<IStarWarsDataService, StarWarsDataService>();

// apply an unrestricted cors policy for the demo services
// to allow use on many of the tools for testing (graphiql, altair etc.)
// Do not do this in production
builder.Services.AddCors(options =>
{
options.AddPolicy(
ALL_ORIGINS_POLICY,
builder =>
{
builder.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
});
});

// ----------------------------------------------------------
// Register GraphQL with the application
// ----------------------------------------------------------
// By default graphql will scan your assembly for any GraphControllers
// and automatically wire them up to the schema
// you can control which assemblies are scanned and which classes are registered using
// the schema configuration options set here.
//
// in this example because of the two test projects (netcore3.1 and net5.0)
// we have moved all the shared code to a common assembly (starwars-common) and are injecting it
// as a single unit
//
// we then add subscription services to the schema builder returned from .AddGraphQL()
builder.Services.AddGraphQL(options =>
{
options.ResponseOptions.ExposeExceptions = true;
options.ResponseOptions.MessageSeverityLevel = GraphMessageSeverity.Information;

// options.ExecutionOptions.EnableMetrics = true;
// options.ResponseOptions.ExposeMetrics = true;

var assembly = typeof(StarWarsDataRepository).Assembly;
options.AddAssembly(assembly);
})
.AddSubscriptions(options =>
{
// this route path is set by default
// it is listed here just as a matter of example
options.Route = SubscriptionConstants.Routing.DEFAULT_SUBSCRIPTIONS_ROUTE;

// for some web based graphql tools such as graphiql and graphql-playground
// the default keep-alive timeout of 2 minutes is too long.
//
// still others (like graphql-playground running in electron) do not respond/configure
// for socket-level ping/pong frames to allow for socket-level keep alives
//
// here we set this demo project websocket keep-alive (at the server level)
// to be below all those thresholds to ensure a hassle free experience.
// In practice, you should configure your server (both subscription keep alives and socket keep alives)
// with an interval that is compatiable with your client side environment.
options.ConnectionKeepAliveInterval = SOCKET_CONNECTION_KEEPALIVE;
});

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
// if you have rest controllers this item be sure they are included.
// Graphql and rest can live side by side in the same project without issue
// --------------------------------------------------
// builder.Services.AddControllers();

// ASP.NET websockets implementation must also be added to the runtime
builder.Services.AddWebSockets((options) =>
{
// here add some common origins of various tools that may be
// used for running this demo
// do not add these in a production app
options.AllowedOrigins.Add("http://localhost:5000");
options.AllowedOrigins.Add("http://localhost:4000");
options.AllowedOrigins.Add("http://localhost:3000");
options.AllowedOrigins.Add("null");

// some electron-based graphql tools send a file reference
// as their origin
// do not add these in a production app
options.AllowedOrigins.Add("file://");
options.AllowedOrigins.Add("ws://");
});

var app = builder.Build();

// Configure the HTTP request pipeline.
app.AddStarWarsStartedMessageToConsole();
app.UseRouting();
app.UseCors(ALL_ORIGINS_POLICY);
app.UseAuthorization();

// enable web sockets on this server instance
// this must be done before a call to 'UseGraphQL' if subscriptions are enabled for any
// schema otherwise the subscriptions may not register correctly
app.UseWebSockets();

// if you have no rest controllers this item can be safely skipped
// graphql and rest can live side by side in the same project without issue
// -----------------------------------------------------------------
// app.UseEndpoints(endpoints =>
// {
// endpoints.MapControllers();
// });

// ************************************************************
// Finalize the graphql setup by loading the schema, build out the templates for all found graph types
// and publish the route to hook the graphql runtime to the web.
// be sure to register it after "UseAuthorization" if you require access to this.User
//
// If the construction of your runtime schema has any errors they will be thrown here
// before your application starts listening for requests.
// ************************************************************
app.UseGraphQL();
app.Run();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"StarWars: .NET 6": {
"StarWars: .NET 8": {
"commandName": "Project",
"applicationUrl": "http://localhost:5000",
"environmentVariables": {
Expand Down
172 changes: 0 additions & 172 deletions src/ancillary-projects/starwars/starwars-api80/Startup.cs

This file was deleted.