Skip to content

Commit

Permalink
Allow postgres subscriptions to be configured in publish only mode (#…
Browse files Browse the repository at this point in the history
…6512)

* Allow postgres subscriptions to be configured in publish only mode
  • Loading branch information
williamdenton committed Sep 12, 2023
1 parent d0a63e1 commit 6965960
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,31 @@ public static class PostgresSubscriptionTransportExtensions
this IRequestExecutorBuilder builder,
Action<IServiceProvider, PostgresSubscriptionOptions> configure)
{
builder.AddSubscriptionDiagnostics();

var services = builder.Services;

services.AddPostgresSubscriptionPublisher(configure);

services.TryAddSingleton<ITopicEventReceiver>(
sp => sp.GetRequiredService<PostgresPubSub>());

return builder;
}
/// <summary>
/// Registers the Postgres subscription provider for use in publisher scenarios where the graphql server is
/// not running, but you still want to publish events via Postgres for another process to receive.
/// </summary>
/// <param name="builder">
/// The service collecion builder.
/// </param>
/// <param name="configure">
/// A delegate that configures the Postgres subscription provider options.
/// </param>
public static IServiceCollection AddPostgresSubscriptionPublisher(
this IServiceCollection services,
Action<IServiceProvider, PostgresSubscriptionOptions> configure)
{
services.AddSubscriptionDiagnostics();

services.AddSingleton(sp =>
{
var options = new PostgresSubscriptionOptions();
Expand All @@ -61,9 +83,7 @@ public static class PostgresSubscriptionTransportExtensions
services.AddSingleton<PostgresPubSub>();
services.TryAddSingleton<ITopicEventSender>(
sp => sp.GetRequiredService<PostgresPubSub>());
services.TryAddSingleton<ITopicEventReceiver>(
sp => sp.GetRequiredService<PostgresPubSub>());

return builder;
return services;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,32 @@ public static class CoreSubscriptionsServiceCollectionExtensions
throw new ArgumentNullException(nameof(builder));
}

builder.Services.TryAddSingleton<ISubscriptionDiagnosticEvents>(
builder.Services.AddSubscriptionDiagnostics();

return builder;
}

/// <summary>
/// Adds the subscription provider diagnostics.
/// </summary>
/// <param name="services">
/// The ServiceCollection.
/// </param>
/// <returns>
/// Returns the ServiceCollection for configuration chaining.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="services"/> is <c>null</c>.
/// </exception>
public static IServiceCollection AddSubscriptionDiagnostics(
this IServiceCollection services)
{
if (services is null)
{
throw new ArgumentNullException(nameof(services));
}

services.TryAddSingleton<ISubscriptionDiagnosticEvents>(
sp =>
{
var listeners = sp.GetService<IEnumerable<ISubscriptionDiagnosticEventsListener>>();
Expand Down Expand Up @@ -61,6 +86,6 @@ public static class CoreSubscriptionsServiceCollectionExtensions
return new AggregateSubscriptionDiagnosticEventsListener(listenerArray);
});

return builder;
return services;
}
}

0 comments on commit 6965960

Please sign in to comment.