This is a simple chat application built to demonstrate real-time messaging using WebSocket-based GraphQL subscriptions. Additionally, it is useful for debugging custom socket connection interceptors for HotChocolate and StrawberryShake.
The interceptor for the server-side is the ChatSocketSessionInterceptor.
Here's how it is set up on the request executor builder (in Program.cs):
builder.AddGraphQL()
.AddTypes()
.AddInMemorySubscriptions()
.AddSocketSessionInterceptor<ChatSocketSessionInterceptor>();The interceptor for the client-side is the ChatConnectionInterceptor.
Here is how the client is set up (in Program.cs):
builder.Services.AddPingPongGraphClient()
.ConfigureHttpClient(client => client.BaseAddress = new Uri(builder.Configuration["GraphQLEndpoint"]!))
.ConfigureWebSocketClient((services, client) =>
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogInformation("Creating web socket client");
try
{
logger.LogInformation("Getting Interceptor");
// this is the important piece VVVV
var theInterceptor = services.CreateScope().ServiceProvider.GetRequiredService<ChatConnectionInterceptor>();
client.Uri = new Uri(builder.Configuration["GraphQLEndpoint"]!.Replace("http", "ws"));
client.ConnectionInterceptor = theInterceptor;
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to fetch web socket connection interceptor");
throw;
}
});The important thing to note here is that the client's connection interceptor HAS to be provided from a new scope. Otherwise, the WebSocket client cannot be generated by this factory. The StrawberryShake generated client has 2 separate service collections internally and without the scope being created the service will not be returned.