Repository navigation
Derive exported batching flags from the server's batching options #10368
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
Merged
Merged
Changes from all commits
Commits
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
22 changes: 22 additions & 0 deletions
22
src/HotChocolate/AspNetCore/src/AspNetCore/TransportCapabilitiesProvider.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,22 @@ | ||
| using HotChocolate.Execution.Internal; | ||
| using Microsoft.Extensions.Options; | ||
|
|
||
| namespace HotChocolate.AspNetCore; | ||
|
|
||
| /// <summary> | ||
| /// Derives the transport capabilities declared in an exported schema settings file | ||
| /// from the <see cref="GraphQLServerOptions"/> of the schema. | ||
| /// </summary> | ||
| internal sealed class TransportCapabilitiesProvider( | ||
| IOptionsMonitor<GraphQLServerOptions> serverOptions) | ||
| : ITransportCapabilitiesProvider | ||
| { | ||
| public TransportCapabilities GetCapabilities(string schemaName) | ||
| { | ||
| var batching = serverOptions.Get(schemaName).Batching; | ||
|
|
||
| return new TransportCapabilities( | ||
| VariableBatching: batching.HasFlag(AllowedBatching.VariableBatching), | ||
| RequestBatching: batching.HasFlag(AllowedBatching.RequestBatching)); | ||
| } | ||
| } |
81 changes: 81 additions & 0 deletions
81
src/HotChocolate/AspNetCore/test/AspNetCore.Tests/TransportCapabilitiesProviderTests.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,81 @@ | ||
| using HotChocolate.Execution.Internal; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
|
|
||
| namespace HotChocolate.AspNetCore; | ||
|
|
||
| public class TransportCapabilitiesProviderTests | ||
| { | ||
| [Fact] | ||
| public void GetCapabilities_Should_DeclareNoBatching_When_ServerOptionsAllowNoBatching() | ||
| { | ||
| // arrange | ||
| var services = new ServiceCollection(); | ||
| services.AddGraphQLServer("a"); | ||
| var provider = GetProvider(services); | ||
|
|
||
| // act | ||
| var capabilities = provider.GetCapabilities("a"); | ||
|
|
||
| // assert | ||
| Assert.Equal( | ||
| new TransportCapabilities(VariableBatching: false, RequestBatching: false), | ||
| capabilities); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GetCapabilities_Should_DeclareBothBatchingModes_When_SourceSchemaDefaultsApplied() | ||
| { | ||
| // arrange | ||
| var services = new ServiceCollection(); | ||
| services.AddGraphQLServer("a").AddSourceSchemaDefaults(); | ||
| var provider = GetProvider(services); | ||
|
|
||
| // act | ||
| var capabilities = provider.GetCapabilities("a"); | ||
|
|
||
| // assert | ||
| Assert.Equal( | ||
| new TransportCapabilities(VariableBatching: true, RequestBatching: true), | ||
| capabilities); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GetCapabilities_Should_MapEachFlagSeparately_When_OnlyVariableBatchingIsAllowed() | ||
| { | ||
| // arrange | ||
| var services = new ServiceCollection(); | ||
| services | ||
| .AddGraphQLServer("a") | ||
| .ModifyServerOptions(o => o.Batching = AllowedBatching.VariableBatching); | ||
| var provider = GetProvider(services); | ||
|
|
||
| // act | ||
| var capabilities = provider.GetCapabilities("a"); | ||
|
|
||
| // assert | ||
| Assert.Equal( | ||
| new TransportCapabilities(VariableBatching: true, RequestBatching: false), | ||
| capabilities); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GetCapabilities_Should_ReadNamedSchemaOptions_When_MultipleSchemasAreRegistered() | ||
| { | ||
| // arrange | ||
| var services = new ServiceCollection(); | ||
| services.AddGraphQLServer("a").AddSourceSchemaDefaults(); | ||
| services.AddGraphQLServer("b"); | ||
| var provider = GetProvider(services); | ||
|
|
||
| // act | ||
| var capabilities = provider.GetCapabilities("b"); | ||
|
|
||
| // assert | ||
| Assert.Equal( | ||
| new TransportCapabilities(VariableBatching: false, RequestBatching: false), | ||
| capabilities); | ||
| } | ||
|
|
||
| private static ITransportCapabilitiesProvider GetProvider(IServiceCollection services) | ||
| => services.BuildServiceProvider().GetRequiredService<ITransportCapabilitiesProvider>(); | ||
| } |
19 changes: 19 additions & 0 deletions
19
src/HotChocolate/Core/src/Types/Execution/Internal/ITransportCapabilitiesProvider.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,19 @@ | ||
| namespace HotChocolate.Execution.Internal; | ||
|
|
||
| /// <summary> | ||
| /// Supplies the transport capabilities that <see cref="SchemaFileExporter"/> declares | ||
| /// in the settings file it creates for a schema. | ||
| /// </summary> | ||
| internal interface ITransportCapabilitiesProvider | ||
| { | ||
| /// <summary> | ||
| /// Gets the transport capabilities of the schema with the given name. | ||
| /// </summary> | ||
| /// <param name="schemaName"> | ||
| /// The name of the schema. | ||
| /// </param> | ||
| /// <returns> | ||
| /// The transport capabilities to declare for the schema. | ||
| /// </returns> | ||
| TransportCapabilities GetCapabilities(string schemaName); | ||
| } |
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
15 changes: 15 additions & 0 deletions
15
src/HotChocolate/Core/src/Types/Execution/Internal/TransportCapabilities.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,15 @@ | ||
| namespace HotChocolate.Execution.Internal; | ||
|
|
||
| /// <summary> | ||
| /// The transport capabilities that <see cref="SchemaFileExporter"/> declares | ||
| /// in the settings file it creates for a schema. | ||
| /// </summary> | ||
| /// <param name="VariableBatching"> | ||
| /// Whether the server accepts variable batching requests. | ||
| /// </param> | ||
| /// <param name="RequestBatching"> | ||
| /// Whether the server accepts request batching requests. | ||
| /// </param> | ||
| internal readonly record struct TransportCapabilities( | ||
| bool VariableBatching, | ||
| bool RequestBatching); |
120 changes: 120 additions & 0 deletions
120
src/HotChocolate/Core/test/Types.Tests/Execution/Internal/SchemaFileExporterTests.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,120 @@ | ||
| using HotChocolate.Types; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
|
|
||
| namespace HotChocolate.Execution.Internal; | ||
|
|
||
| public class SchemaFileExporterTests : IDisposable | ||
| { | ||
| private readonly string _directory = System.IO.Path.Combine( | ||
| System.IO.Path.GetTempPath(), | ||
| System.IO.Path.GetRandomFileName()); | ||
|
|
||
| [Fact] | ||
| public async Task Export_Should_DeclareBothBatchingModes_When_NoProviderIsRegistered() | ||
| { | ||
| // arrange | ||
| var services = new ServiceCollection(); | ||
| services | ||
| .AddGraphQL() | ||
| .AddQueryType(d => d.Name("Query").Field("foo").Resolve("bar")); | ||
| var executor = await GetExecutorAsync(services); | ||
| Directory.CreateDirectory(_directory); | ||
|
|
||
| // act | ||
| var result = await SchemaFileExporter.Export( | ||
| System.IO.Path.Combine(_directory, "schema.graphqls"), | ||
| executor, | ||
| rewriteToSemanticNonNull: false, | ||
| TestContext.Current.CancellationToken); | ||
|
|
||
| // assert | ||
| var settings = await File.ReadAllTextAsync( | ||
| result.SettingsFileName, | ||
| TestContext.Current.CancellationToken); | ||
| settings.ReplaceLineEndings("\n").MatchInlineSnapshot( | ||
| """ | ||
| { | ||
| "name": "_Default", | ||
| "transports": { | ||
| "http": { | ||
| "url": "http://localhost:5000/graphql", | ||
| "capabilities": { | ||
| "batching": { | ||
| "variableBatching": true, | ||
| "requestBatching": true, | ||
| "aliasBatching": true | ||
| }, | ||
| "onError": "propagate" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| """ + "\n"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Export_Should_DeclareProviderCapabilities_When_ProviderIsRegistered() | ||
| { | ||
| // arrange | ||
| var services = new ServiceCollection(); | ||
| services.AddSingleton<ITransportCapabilitiesProvider>( | ||
| new FixedCapabilitiesProvider( | ||
| new TransportCapabilities(VariableBatching: false, RequestBatching: true))); | ||
| services | ||
| .AddGraphQL() | ||
| .AddQueryType(d => d.Name("Query").Field("foo").Resolve("bar")); | ||
| var executor = await GetExecutorAsync(services); | ||
| Directory.CreateDirectory(_directory); | ||
|
|
||
| // act | ||
| var result = await SchemaFileExporter.Export( | ||
| System.IO.Path.Combine(_directory, "schema.graphqls"), | ||
| executor, | ||
| rewriteToSemanticNonNull: false, | ||
| TestContext.Current.CancellationToken); | ||
|
|
||
| // assert | ||
| var settings = await File.ReadAllTextAsync( | ||
| result.SettingsFileName, | ||
| TestContext.Current.CancellationToken); | ||
| settings.ReplaceLineEndings("\n").MatchInlineSnapshot( | ||
| """ | ||
| { | ||
| "name": "_Default", | ||
| "transports": { | ||
| "http": { | ||
| "url": "http://localhost:5000/graphql", | ||
| "capabilities": { | ||
| "batching": { | ||
| "variableBatching": false, | ||
| "requestBatching": true, | ||
| "aliasBatching": true | ||
| }, | ||
| "onError": "propagate" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| """ + "\n"); | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| if (Directory.Exists(_directory)) | ||
| { | ||
| Directory.Delete(_directory, recursive: true); | ||
| } | ||
| } | ||
|
|
||
| private static async Task<IRequestExecutor> GetExecutorAsync(IServiceCollection services) | ||
| => await services | ||
| .BuildServiceProvider() | ||
| .GetRequiredService<IRequestExecutorProvider>() | ||
| .GetExecutorAsync(cancellationToken: TestContext.Current.CancellationToken); | ||
|
|
||
| private sealed class FixedCapabilitiesProvider(TransportCapabilities capabilities) | ||
| : ITransportCapabilitiesProvider | ||
| { | ||
| public TransportCapabilities GetCapabilities(string schemaName) => capabilities; | ||
| } | ||
| } |
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
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.