-
-
Notifications
You must be signed in to change notification settings - Fork 799
Re-add IRequestExecutorOptionsMonitor #8846
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
Draft
tobias-tengler
wants to merge
4
commits into
main
Choose a base branch
from
tte/re-add-executor-options-monitor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d4a3cdd
Re-add IRequestExecutorOptionsMonitor with tests
tobias-tengler d004eda
Apply Co-Pilot suggestion
tobias-tengler 5d9c2fe
Merge d004eda83c80feb5eb4894dadfe3e6fc0d5c4780 into cc2f3fe1cee1aa749…
tobias-tengler afa27f0
Update performance data [skip ci]
github-actions[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 0 additions & 41 deletions
41
src/HotChocolate/Core/src/Execution/Configuration/ConfigureRequestExecutorSetup.cs
This file was deleted.
Oops, something went wrong.
20 changes: 20 additions & 0 deletions
20
src/HotChocolate/Core/src/Execution/Configuration/DefaultRequestExecutorOptionsMonitor.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| using Microsoft.Extensions.Options; | ||
|
|
||
| namespace HotChocolate.Execution.Configuration; | ||
|
|
||
| internal sealed class DefaultRequestExecutorOptionsMonitor(IOptionsMonitor<RequestExecutorSetup> optionsMonitor) | ||
| : IRequestExecutorOptionsMonitor | ||
| { | ||
| public RequestExecutorSetup Get(string schemaName) => optionsMonitor.Get(schemaName); | ||
|
|
||
| public IDisposable OnChange(Action<string> listener) => NoOpListener.Instance; | ||
|
|
||
| private sealed class NoOpListener : IDisposable | ||
| { | ||
| public void Dispose() | ||
| { | ||
| } | ||
|
|
||
| public static NoOpListener Instance { get; } = new(); | ||
| } | ||
| } | ||
14 changes: 0 additions & 14 deletions
14
src/HotChocolate/Core/src/Execution/Configuration/IConfigureRequestExecutorSetup.cs
This file was deleted.
Oops, something went wrong.
25 changes: 25 additions & 0 deletions
25
src/HotChocolate/Core/src/Execution/Configuration/IRequestExecutorOptionsMonitor.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| namespace HotChocolate.Execution.Configuration; | ||
|
|
||
| /// <summary> | ||
| /// Used for notifications when <see cref="RequestExecutorSetup"/> instances change. | ||
| /// </summary> | ||
| public interface IRequestExecutorOptionsMonitor | ||
| { | ||
| /// <summary> | ||
| /// Returns a configured <see cref="RequestExecutorSetup"/> | ||
| /// instance with the given name. | ||
| /// </summary> | ||
| RequestExecutorSetup Get(string schemaName); | ||
|
|
||
| /// <summary> | ||
| /// Registers a listener to be called whenever a named | ||
| /// <see cref="RequestExecutorSetup"/> changes. | ||
| /// </summary> | ||
| /// <param name="listener"> | ||
| /// The action to be invoked when <see cref="RequestExecutorSetup"/> has changed. | ||
| /// </param> | ||
| /// <returns> | ||
| /// An <see cref="IDisposable"/> which should be disposed to stop listening for changes. | ||
| /// </returns> | ||
| IDisposable OnChange(Action<string> listener); | ||
| } |
12 changes: 11 additions & 1 deletion
12
...otChocolate/Core/src/Execution/DependencyInjection/InternalServiceCollectionExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
...otChocolate/Core/test/Execution.Tests/Configuration/RequestExecutorOptionsMonitorTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| using HotChocolate.Types; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
|
|
||
| namespace HotChocolate.Execution.Configuration; | ||
|
|
||
| public class RequestExecutorOptionsMonitorTests | ||
| { | ||
| [Fact] | ||
| public async Task RequestExecutorSetup_Can_Be_Provided_Through_RequestExecutorOptionsMonitor() | ||
| { | ||
| // arrange | ||
| var monitor = new TestOptionsMonitor(); | ||
| monitor.Set(ISchemaDefinition.DefaultName, new RequestExecutorSetup | ||
| { | ||
| SchemaBuilder = SchemaBuilder.New() | ||
| .AddQueryType(d => d.Field("field").Resolve("")), | ||
| Pipeline = { new RequestMiddlewareConfiguration((_, _) => _ => ValueTask.CompletedTask) } | ||
| }); | ||
|
|
||
| var services = new ServiceCollection(); | ||
| services.AddSingleton<IRequestExecutorOptionsMonitor>(_ => monitor); | ||
|
|
||
| services.AddGraphQLServer(); | ||
|
|
||
| // act | ||
| var executor = await services.BuildServiceProvider().GetRequestExecutorAsync(); | ||
|
|
||
| // assert | ||
| executor.Schema.MatchInlineSnapshot( | ||
| """ | ||
| schema { | ||
| query: ObjectType | ||
| } | ||
| type ObjectType { | ||
| field: String | ||
| } | ||
| """); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task RequestExecutor_Can_Be_Reloaded_Through_RequestExecutorOptionsMonitor() | ||
| { | ||
| // arrange | ||
| var executorEvictedResetEvent = new ManualResetEventSlim(false); | ||
| var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5)); | ||
|
|
||
| var monitor = new TestOptionsMonitor(); | ||
| monitor.Set(ISchemaDefinition.DefaultName, new RequestExecutorSetup | ||
| { | ||
| SchemaBuilder = SchemaBuilder.New() | ||
| .AddQueryType(d => d.Field("field").Resolve("")), | ||
| Pipeline = { new RequestMiddlewareConfiguration((_, _) => _ => ValueTask.CompletedTask) } | ||
| }); | ||
|
|
||
| var services = new ServiceCollection(); | ||
| services.AddSingleton<IRequestExecutorOptionsMonitor>(_ => monitor); | ||
|
|
||
| services.AddGraphQLServer(); | ||
|
|
||
| var manager = services.BuildServiceProvider().GetRequiredService<RequestExecutorManager>(); | ||
|
|
||
| manager.Subscribe(new RequestExecutorEventObserver(@event => | ||
| { | ||
| if (@event.Type == RequestExecutorEventType.Evicted) | ||
| { | ||
| executorEvictedResetEvent.Set(); | ||
| } | ||
| })); | ||
|
|
||
| // act | ||
| // assert | ||
| var initialExecutor = await manager.GetExecutorAsync(cancellationToken: cts.Token); | ||
|
|
||
| monitor.Set(ISchemaDefinition.DefaultName, new RequestExecutorSetup | ||
| { | ||
| SchemaBuilder = SchemaBuilder.New() | ||
| .AddQueryType(d => d.Field("field2").Resolve("")), | ||
| Pipeline = { new RequestMiddlewareConfiguration((_, _) => _ => ValueTask.CompletedTask) } | ||
| }); | ||
|
|
||
| executorEvictedResetEvent.Wait(cts.Token); | ||
|
|
||
| var executorAfterEviction = await manager.GetExecutorAsync(cancellationToken: cts.Token); | ||
|
|
||
| Assert.NotSame(initialExecutor, executorAfterEviction); | ||
|
|
||
| executorAfterEviction.Schema.MatchInlineSnapshot( | ||
| """ | ||
| schema { | ||
| query: ObjectType | ||
| } | ||
| type ObjectType { | ||
| field2: String | ||
| } | ||
| """); | ||
|
|
||
| cts.Dispose(); | ||
tobias-tengler marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| private sealed class TestOptionsMonitor : IRequestExecutorOptionsMonitor | ||
| { | ||
| private readonly Dictionary<string, RequestExecutorSetup> _setups = new(); | ||
| private readonly List<Action<string>> _listeners = new(); | ||
|
|
||
| public void Set(string schemaName, RequestExecutorSetup setup) | ||
| { | ||
| _setups[schemaName] = setup; | ||
|
|
||
| foreach (var listener in _listeners) | ||
| { | ||
| listener(schemaName); | ||
| } | ||
| } | ||
|
|
||
| public RequestExecutorSetup Get(string schemaName) | ||
| { | ||
| return _setups[schemaName]; | ||
| } | ||
|
|
||
| public IDisposable OnChange(Action<string> listener) | ||
| { | ||
| _listeners.Add(listener); | ||
| return new Unsubscriber(this, listener); | ||
| } | ||
|
|
||
| private void Unsubscribe(Action<string> listener) | ||
| { | ||
| _listeners.Remove(listener); | ||
| } | ||
|
|
||
| private sealed class Unsubscriber( | ||
| TestOptionsMonitor monitor, | ||
| Action<string> listener) | ||
| : IDisposable | ||
| { | ||
| public void Dispose() => monitor.Unsubscribe(listener); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.