Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ public static IRequestExecutorBuilder AddHttpResponseFormatter(
/// <returns>
/// Returns the <see cref="IServiceCollection"/> so that configuration can be chained.
/// </returns>
/// <remarks>
/// The <typeparamref name="T"/> will be activated with the <see cref="IServiceProvider"/> of the schema services.
/// If your <typeparamref name="T"/> needs to access application services you need to
/// make the services available in the schema services via <see cref="RequestExecutorBuilderExtensions.AddApplicationService"/>.
/// </remarks>
public static IRequestExecutorBuilder AddHttpResponseFormatter<
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] T>(
this IRequestExecutorBuilder builder)
Expand Down Expand Up @@ -221,6 +226,14 @@ public static IRequestExecutorBuilder AddHttpResponseFormatter<
/// <returns>
/// Returns the <see cref="IServiceCollection"/> so that configuration can be chained.
/// </returns>
/// <remarks>
/// The <see cref="IServiceProvider"/> passed to the <paramref name="factory"/>
/// is for the schema services. If you need to access application services
/// you need to either make the services available in the schema services
/// via <see cref="RequestExecutorBuilderExtensions.AddApplicationService"/> or use
/// <see cref="ExecutionServiceProviderExtensions.GetRootServiceProvider(IServiceProvider)"/>
/// to access the application services from within the schema service provider.
/// </remarks>
public static IRequestExecutorBuilder AddHttpResponseFormatter<T>(
this IRequestExecutorBuilder builder,
Func<IServiceProvider, T> factory)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using HotChocolate.AspNetCore;
using HotChocolate.AspNetCore.Formatters;
using HotChocolate.AspNetCore.Subscriptions.Protocols;
using HotChocolate.Execution;
using HotChocolate.Fusion.Configuration;
using HotChocolate.Transport.Formatters;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -66,4 +72,228 @@ public static IFusionGatewayBuilder AddHttpRequestInterceptor(
s.AddSingleton(factory);
});
}

/// <summary>
/// Adds an interceptor for GraphQL socket sessions.
/// </summary>
/// <param name="builder">
/// The <see cref="IFusionGatewayBuilder"/>.
/// </param>
/// <typeparam name="T">
/// The <see cref="ISocketSessionInterceptor"/> implementation.
/// </typeparam>
/// <returns>
/// Returns the <see cref="IFusionGatewayBuilder"/> so that configuration can be chained.
/// </returns>
/// <remarks>
/// The <typeparamref name="T"/> will be activated with the <see cref="IServiceProvider"/> of
/// the schema services. If your <typeparamref name="T"/> needs to access application services
/// you need to make the services available in the schema services via
/// <see cref="CoreFusionGatewayBuilderExtensions.AddApplicationService"/>.
/// </remarks>
public static IFusionGatewayBuilder AddSocketSessionInterceptor<
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] T>(
this IFusionGatewayBuilder builder)
where T : class, ISocketSessionInterceptor
{
ArgumentNullException.ThrowIfNull(builder);

return builder.ConfigureSchemaServices(
(_, s) =>
{
s.RemoveAll<ISocketSessionInterceptor>();
s.AddSingleton<ISocketSessionInterceptor, T>();
});
}

/// <summary>
/// Adds an interceptor for GraphQL socket sessions.
/// </summary>
/// <param name="builder">
/// The <see cref="IFusionGatewayBuilder"/>.
/// </param>
/// <param name="factory">
/// A factory that creates the interceptor instance.
/// </param>
/// <typeparam name="T">
/// The <see cref="ISocketSessionInterceptor"/> implementation.
/// </typeparam>
/// <returns>
/// Returns the <see cref="IFusionGatewayBuilder"/> so that configuration can be chained.
/// </returns>
/// <remarks>
/// The <see cref="IServiceProvider"/> passed to the <paramref name="factory"/>
/// is for the schema services. If you need to access application services
/// you need to either make the services available in the schema services
/// via <see cref="CoreFusionGatewayBuilderExtensions.AddApplicationService"/> or use
/// <see cref="ExecutionServiceProviderExtensions.GetRootServiceProvider(IServiceProvider)"/>
/// to access the application services from within the schema service provider.
/// </remarks>
public static IFusionGatewayBuilder AddSocketSessionInterceptor<T>(
this IFusionGatewayBuilder builder,
Func<IServiceProvider, T> factory)
where T : class, ISocketSessionInterceptor
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(factory);

return builder.ConfigureSchemaServices(
(_, s) =>
{
s.RemoveAll<ISocketSessionInterceptor>();
s.AddSingleton<ISocketSessionInterceptor, T>(factory);
});
}

/// <summary>
/// Adds the <see cref="DefaultHttpResponseFormatter"/> with specific formatter options.
/// </summary>
/// <param name="builder">
/// The <see cref="IFusionGatewayBuilder"/>.
/// </param>
/// <param name="indented">
/// Defines whether the underlying <see cref="Utf8JsonWriter"/>
/// should pretty print the JSON which includes:
/// indenting nested JSON tokens, adding new lines, and adding
/// white space between property names and values.
/// By default, the JSON is written without extra white spaces.
/// </param>
/// <param name="incrementalDeliveryFormat">
/// The default incremental delivery format to use when the client does not specify one
/// via the <c>Accept</c> header. Defaults to <see cref="IncrementalDeliveryFormat.Version_0_2"/>.
/// </param>
/// <returns>
/// Returns the <see cref="IFusionGatewayBuilder"/> so that configuration can be chained.
/// </returns>
public static IFusionGatewayBuilder AddHttpResponseFormatter(
this IFusionGatewayBuilder builder,
bool indented = false,
IncrementalDeliveryFormat incrementalDeliveryFormat = IncrementalDeliveryFormat.Version_0_2)
{
ArgumentNullException.ThrowIfNull(builder);

return builder.ConfigureSchemaServices(
(_, s) =>
{
s.RemoveAll<IHttpResponseFormatter>();
s.AddSingleton<IHttpResponseFormatter>(
sp => DefaultHttpResponseFormatter.Create(
new HttpResponseFormatterOptions
{
Json = new JsonResultFormatterOptions
{
Indented = indented
}
},
sp.GetRequiredService<ITimeProvider>(),
incrementalDeliveryFormat));
});
}

/// <summary>
/// Adds the <see cref="DefaultHttpResponseFormatter"/> with specific formatter options.
/// </summary>
/// <param name="builder">
/// The <see cref="IFusionGatewayBuilder"/>.
/// </param>
/// <param name="options">
/// The HTTP response formatter options.
/// </param>
/// <param name="incrementalDeliveryFormat">
/// The default incremental delivery format to use when the client does not specify one
/// via the <c>Accept</c> header. Defaults to <see cref="IncrementalDeliveryFormat.Version_0_2"/>.
/// </param>
/// <returns>
/// Returns the <see cref="IFusionGatewayBuilder"/> so that configuration can be chained.
/// </returns>
public static IFusionGatewayBuilder AddHttpResponseFormatter(
this IFusionGatewayBuilder builder,
HttpResponseFormatterOptions options,
IncrementalDeliveryFormat incrementalDeliveryFormat = IncrementalDeliveryFormat.Version_0_2)
{
ArgumentNullException.ThrowIfNull(builder);

return builder.ConfigureSchemaServices(
(_, s) =>
{
s.RemoveAll<IHttpResponseFormatter>();
s.AddSingleton<IHttpResponseFormatter>(
sp => DefaultHttpResponseFormatter.Create(
options,
sp.GetRequiredService<ITimeProvider>(),
incrementalDeliveryFormat));
});
}

/// <summary>
/// Adds a custom HTTP response formatter.
/// </summary>
/// <param name="builder">
/// The <see cref="IFusionGatewayBuilder"/>.
/// </param>
/// <typeparam name="T">
/// The type of the custom <see cref="IHttpResponseFormatter"/>.
/// </typeparam>
/// <returns>
/// Returns the <see cref="IFusionGatewayBuilder"/> so that configuration can be chained.
/// </returns>
/// <remarks>
/// The <typeparamref name="T"/> will be activated with the <see cref="IServiceProvider"/> of
/// the schema services. If your <typeparamref name="T"/> needs to access application services
/// you need to make the services available in the schema services via
/// <see cref="CoreFusionGatewayBuilderExtensions.AddApplicationService"/>.
/// </remarks>
public static IFusionGatewayBuilder AddHttpResponseFormatter<
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] T>(
this IFusionGatewayBuilder builder)
where T : class, IHttpResponseFormatter
{
ArgumentNullException.ThrowIfNull(builder);

return builder.ConfigureSchemaServices(
(_, s) =>
{
s.RemoveAll<IHttpResponseFormatter>();
s.AddSingleton<IHttpResponseFormatter, T>();
});
}

/// <summary>
/// Adds a custom HTTP response formatter.
/// </summary>
/// <param name="builder">
/// The <see cref="IFusionGatewayBuilder"/>.
/// </param>
/// <param name="factory">
/// The service factory.
/// </param>
/// <typeparam name="T">
/// The type of the custom <see cref="IHttpResponseFormatter"/>.
/// </typeparam>
/// <returns>
/// Returns the <see cref="IFusionGatewayBuilder"/> so that configuration can be chained.
/// </returns>
/// <remarks>
/// The <see cref="IServiceProvider"/> passed to the <paramref name="factory"/>
/// is for the schema services. If you need to access application services
/// you need to either make the services available in the schema services
/// via <see cref="CoreFusionGatewayBuilderExtensions.AddApplicationService"/> or use
/// <see cref="ExecutionServiceProviderExtensions.GetRootServiceProvider(IServiceProvider)"/>
/// to access the application services from within the schema service provider.
/// </remarks>
public static IFusionGatewayBuilder AddHttpResponseFormatter<T>(
this IFusionGatewayBuilder builder,
Func<IServiceProvider, T> factory)
where T : class, IHttpResponseFormatter
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(factory);

return builder.ConfigureSchemaServices(
(_, s) =>
{
s.RemoveAll<IHttpResponseFormatter>();
s.AddSingleton<IHttpResponseFormatter>(factory);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ protected class Gateway(
{
public HttpClient CreateClient() => testServer.CreateClient();

public WebSocketClient CreateWebSocketClient() => testServer.CreateWebSocketClient();

public IServiceProvider Services => testServer.Services;

public List<SourceSchemaText> SourceSchemas => sourceSchemas;
Expand Down
Loading
Loading