Skip to content

Commit

Permalink
Move options to HotChocolate.Fusion project
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias-tengler committed Mar 21, 2024
1 parent fb82609 commit b37b127
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ public interface IRequestExecutorOptionsAccessor
: IErrorHandlerOptionsAccessor
, IRequestTimeoutOptionsAccessor
, IComplexityAnalyzerOptionsAccessor
, IPersistedQueryOptionsAccessor
, IFusionOptionsAccessor;
, IPersistedQueryOptionsAccessor;
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,6 @@ public TimeSpan ExecutionTimeout
/// </summary>
public bool IncludeExceptionDetails { get; set; } = Debugger.IsAttached;

/// <summary>
/// Gets or sets a value indicating whether the <c>Fusion</c> query plan
/// can be requested on a per request basis.
///
/// The default is <c>false</c>.
/// </summary>
public bool AllowFusionQueryPlan { get; set; } = false;

/// <summary>
/// Gets or sets a value indicating whether <c>Fusion</c> debugging
/// information should be included in the response.
///
/// The default value is <see cref="Debugger.IsAttached"/>.
/// </summary>
public bool IncludeFusionDebugInfo { get; set; } = Debugger.IsAttached;

/// <summary>
/// Gets the complexity analyzer settings.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,6 @@ await typeModuleChangeMonitor.ConfigureAsync(context, cancellationToken)
s => s.GetRequiredService<RequestExecutorOptions>());
serviceCollection.AddSingleton<IPersistedQueryOptionsAccessor>(
s => s.GetRequiredService<RequestExecutorOptions>());
serviceCollection.AddSingleton<IFusionOptionsAccessor>(
s => s.GetRequiredService<RequestExecutorOptions>());

serviceCollection.AddSingleton<IErrorHandler, DefaultErrorHandler>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public static FusionGatewayBuilder AddFusionGatewayServer(
sc.TryAddSingleton(fusionGraphConfig);
sc.TryAddSingleton<QueryPlanner>();
sc.TryAddSingleton<NodeIdParser, DefaultNodeIdParser>();
sc.TryAddSingleton<IFusionOptionsAccessor>(GetFusionOptions);
});
});

Expand Down Expand Up @@ -273,6 +275,37 @@ public static FusionGatewayBuilder ModifyRequestOptions(
return builder;
}

/// <summary>
/// Adds a delegate that will be used to modify the <see cref="RequestExecutorOptions"/>.
/// </summary>
/// <param name="builder">
/// The gateway builder.
/// </param>
/// <param name="modify">
/// A delegate that is used to modify the <see cref="RequestExecutorOptions"/>.
/// </param>
/// <returns>
/// Returns the <see cref="FusionGatewayBuilder"/> that can be used to configure the Gateway.
/// </returns>
public static FusionGatewayBuilder ModifyFusionOptions(
this FusionGatewayBuilder builder,
Action<FusionOptions> modify)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}

if (modify is null)
{
throw new ArgumentNullException(nameof(modify));
}

builder.Services.AddSingleton(modify);

return builder;
}

/// <summary>
/// Uses the default fusion gateway pipeline.
/// </summary>
Expand Down Expand Up @@ -511,4 +544,18 @@ internal static ValueTask<ISchema> BuildSchemaAsync(
string? graphName = default,
CancellationToken cancellationToken = default)
=> builder.CoreBuilder.BuildSchemaAsync(graphName, cancellationToken);

private static FusionOptions GetFusionOptions(IServiceProvider sp)
{
var appSp = sp.GetApplicationServices();
var configures = appSp.GetServices<Action<FusionOptions>>();
var options = new FusionOptions();

foreach (var configure in configures)
{
configure(options);
}

return options;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using System.Runtime.CompilerServices;
using HotChocolate.Execution.Options;
using HotChocolate.Execution.Processing;
using HotChocolate.Fusion.Clients;
using HotChocolate.Fusion.Execution.Nodes;
using HotChocolate.Fusion.Metadata;
using HotChocolate.Fusion.Utilities;
using HotChocolate.Transport;
using HotChocolate.Types.Relay;

namespace HotChocolate.Fusion.Execution;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using HotChocolate.Execution;
using HotChocolate.Execution.DependencyInjection;
using HotChocolate.Execution.Options;
using HotChocolate.Execution.Processing;
using HotChocolate.Fetching;
using HotChocolate.Fusion.Clients;
Expand Down
22 changes: 22 additions & 0 deletions src/HotChocolate/Fusion/src/Core/Metadata/FusionOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Diagnostics;

namespace HotChocolate.Fusion.Metadata;

public class FusionOptions : IFusionOptionsAccessor
{
/// <summary>
/// Gets or sets a value indicating whether the <c>Fusion</c> query plan
/// can be requested on a per request basis.
///
/// The default is <c>false</c>.
/// </summary>
public bool AllowFusionQueryPlan { get; set; } = false;

/// <summary>
/// Gets or sets a value indicating whether <c>Fusion</c> debugging
/// information should be included in the response.
///
/// The default value is <see cref="Debugger.IsAttached"/>.
/// </summary>
public bool IncludeFusionDebugInfo { get; set; } = Debugger.IsAttached;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Diagnostics;

namespace HotChocolate.Execution.Options;
namespace HotChocolate.Fusion.Metadata;

public interface IFusionOptionsAccessor
{
Expand Down
8 changes: 4 additions & 4 deletions src/HotChocolate/Fusion/test/Core.Tests/ErrorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public async Task TopLevelResolveSubgraphError()
.AddSingleton(demoProject.WebSocketConnectionFactory)
.AddFusionGatewayServer()
.ConfigureFromDocument(SchemaFormatter.FormatAsDocument(fusionGraph))
.ModifyRequestOptions(options => options.IncludeFusionDebugInfo = true)
.ModifyFusionOptions(options => options.IncludeFusionDebugInfo = true)
.BuildRequestExecutorAsync();

var request = Parse(
Expand Down Expand Up @@ -89,7 +89,7 @@ public async Task NestedResolveSubgraphError()
.AddSingleton(demoProject.WebSocketConnectionFactory)
.AddFusionGatewayServer()
.ConfigureFromDocument(SchemaFormatter.FormatAsDocument(fusionGraph))
.ModifyRequestOptions(options => options.IncludeFusionDebugInfo = true)
.ModifyFusionOptions(options => options.IncludeFusionDebugInfo = true)
.BuildRequestExecutorAsync();

var request = Parse(
Expand Down Expand Up @@ -140,7 +140,7 @@ public async Task NestedResolveWithListSubgraphError()
.AddSingleton(demoProject.WebSocketConnectionFactory)
.AddFusionGatewayServer()
.ConfigureFromDocument(SchemaFormatter.FormatAsDocument(fusionGraph))
.ModifyRequestOptions(options => options.IncludeFusionDebugInfo = true)
.ModifyFusionOptions(options => options.IncludeFusionDebugInfo = true)
.BuildRequestExecutorAsync();

var request = Parse(
Expand Down Expand Up @@ -193,7 +193,7 @@ public async Task ResolveByKeySubgraphError()
.AddSingleton(demoProject.WebSocketConnectionFactory)
.AddFusionGatewayServer()
.ConfigureFromDocument(SchemaFormatter.FormatAsDocument(fusionGraph))
.ModifyRequestOptions(options => options.IncludeFusionDebugInfo = true)
.ModifyFusionOptions(options => options.IncludeFusionDebugInfo = true)
.BuildRequestExecutorAsync();

var request = Parse(
Expand Down

0 comments on commit b37b127

Please sign in to comment.