diff --git a/src/HotChocolate/Fusion/src/Abstractions/FusionGraphPackage.cs b/src/HotChocolate/Fusion/src/Abstractions/FusionGraphPackage.cs index 82ab62c29d1..661185f7313 100644 --- a/src/HotChocolate/Fusion/src/Abstractions/FusionGraphPackage.cs +++ b/src/HotChocolate/Fusion/src/Abstractions/FusionGraphPackage.cs @@ -579,7 +579,8 @@ private async Task WriteSubgraphConfigurationJsonPartAsync( { var config = new SubgraphConfigJson( configuration.Name, - configuration.Clients); + configuration.Clients, + configuration.ConfigurationExtensions); var path = $"{configuration.Name}/{SubgraphConfigFileName}"; var uri = PackUriHelper.CreatePartUri(new Uri(path, UriKind.Relative)); diff --git a/src/HotChocolate/Fusion/src/Abstractions/HotChocolate.Fusion.Abstractions.csproj b/src/HotChocolate/Fusion/src/Abstractions/HotChocolate.Fusion.Abstractions.csproj index f82d8d37732..053fd648a20 100644 --- a/src/HotChocolate/Fusion/src/Abstractions/HotChocolate.Fusion.Abstractions.csproj +++ b/src/HotChocolate/Fusion/src/Abstractions/HotChocolate.Fusion.Abstractions.csproj @@ -14,6 +14,7 @@ + diff --git a/src/HotChocolate/Fusion/src/Abstractions/JsonElementExtensions.cs b/src/HotChocolate/Fusion/src/Abstractions/JsonElementExtensions.cs new file mode 100644 index 00000000000..d5254096ca7 --- /dev/null +++ b/src/HotChocolate/Fusion/src/Abstractions/JsonElementExtensions.cs @@ -0,0 +1,18 @@ +using System.Text.Json; +using HotChocolate.Utilities; + +namespace HotChocolate.Fusion; + +internal static class JsonElementExtensions +{ + public static JsonElement SafeClone(this JsonElement element) + { + var writer = new ArrayWriter(); + using var jsonWriter = new Utf8JsonWriter(writer); + + element.WriteTo(jsonWriter); + var reader = new Utf8JsonReader(writer.GetSpan(), true, default); + + return JsonElement.ParseValue(ref reader); + } +} diff --git a/src/HotChocolate/Fusion/src/Abstractions/SubgraphConfigJson.cs b/src/HotChocolate/Fusion/src/Abstractions/SubgraphConfigJson.cs index 19831ed86c9..a3eee77f479 100644 --- a/src/HotChocolate/Fusion/src/Abstractions/SubgraphConfigJson.cs +++ b/src/HotChocolate/Fusion/src/Abstractions/SubgraphConfigJson.cs @@ -17,13 +17,17 @@ internal sealed record SubgraphConfigJson /// /// The list of clients that can be used to fetch data from this subgraph. /// + /// + /// The "extensions" property of the subgraph-config.json. + /// public SubgraphConfigJson( string name, IReadOnlyList? clients = null, - JsonDocument? extensions = null) + JsonElement? extensions = null) { Name = name; Clients = clients ?? Array.Empty(); + Extensions = extensions; } /// @@ -39,7 +43,7 @@ public SubgraphConfigJson( /// /// Gets the "extensions" property of the subgraph-config.json. /// - public JsonDocument? Extensions { get; init; } + public JsonElement? Extensions { get; init; } /// /// Deconstructs the into its components. @@ -50,10 +54,13 @@ public SubgraphConfigJson( /// /// The list of clients that can be used to fetch data from this subgraph. /// + /// + /// The "extensions" property of the subgraph-config.json. + /// public void Deconstruct( out string name, out IReadOnlyList clients, - out JsonDocument? extensions) + out JsonElement? extensions) { name = Name; clients = Clients; diff --git a/src/HotChocolate/Fusion/src/Abstractions/SubgraphConfigJsonSerializer.cs b/src/HotChocolate/Fusion/src/Abstractions/SubgraphConfigJsonSerializer.cs index 6556c6593f3..6937fe33213 100644 --- a/src/HotChocolate/Fusion/src/Abstractions/SubgraphConfigJsonSerializer.cs +++ b/src/HotChocolate/Fusion/src/Abstractions/SubgraphConfigJsonSerializer.cs @@ -146,6 +146,12 @@ public static async ValueTask FormatAsync( } } + if (config.Extensions is not null) + { + writer.WritePropertyName("extensions"); + config.Extensions.Value.WriteTo(writer); + } + writer.WriteEndObject(); await writer.FlushAsync(cancellationToken); } @@ -176,6 +182,7 @@ public static async Task ParseAsync( stream, cancellationToken: cancellationToken); var configs = new List(); var subgraph = default(string?); + var extensions = default(JsonElement?); foreach (var property in document.RootElement.EnumerateObject()) { @@ -193,6 +200,10 @@ public static async Task ParseAsync( configs.Add(ReadWebSocketClientConfiguration(property.Value)); break; + case "extensions": + extensions = property.Value.SafeClone(); + break; + default: throw new NotSupportedException( $"Configuration property `{property.Value}` is not supported."); @@ -204,7 +215,7 @@ public static async Task ParseAsync( throw new InvalidOperationException("No subgraph name was specified."); } - return new SubgraphConfigJson(subgraph, configs); + return new SubgraphConfigJson(subgraph, configs, extensions); } private static HttpClientConfiguration ReadHttpClientConfiguration( diff --git a/src/HotChocolate/Fusion/src/Abstractions/SubgraphConfiguration.cs b/src/HotChocolate/Fusion/src/Abstractions/SubgraphConfiguration.cs index 524101636b2..6fe5f8486d8 100644 --- a/src/HotChocolate/Fusion/src/Abstractions/SubgraphConfiguration.cs +++ b/src/HotChocolate/Fusion/src/Abstractions/SubgraphConfiguration.cs @@ -30,7 +30,7 @@ public SubgraphConfiguration( string schema, IReadOnlyList extensions, IReadOnlyList clients, - JsonDocument? configurationExtensions) + JsonElement? configurationExtensions) { Name = name; Schema = schema; @@ -62,7 +62,7 @@ public SubgraphConfiguration( string schema, string extensions, IReadOnlyList clients, - JsonDocument? configurationExtensions) + JsonElement? configurationExtensions) : this(name, schema, new[] { extensions }, clients, configurationExtensions) { } /// @@ -88,7 +88,7 @@ public SubgraphConfiguration( string schema, string extensions, IClientConfiguration client, - JsonDocument? configurationExtensions) + JsonElement? configurationExtensions) : this(name, schema, new[] { extensions }, new[] { client }, configurationExtensions) { } /// @@ -114,5 +114,5 @@ public SubgraphConfiguration( /// /// Gets the configuration extension. /// - public JsonDocument? ConfigurationExtensions { get; } + public JsonElement? ConfigurationExtensions { get; } } diff --git a/src/HotChocolate/Fusion/src/CommandLine/Helpers/PackageHelper.cs b/src/HotChocolate/Fusion/src/CommandLine/Helpers/PackageHelper.cs index 73f56c8b16f..3386bffc3eb 100644 --- a/src/HotChocolate/Fusion/src/CommandLine/Helpers/PackageHelper.cs +++ b/src/HotChocolate/Fusion/src/CommandLine/Helpers/PackageHelper.cs @@ -197,12 +197,6 @@ private static WebSocketClientConfiguration ReadWebSocketClientConfiguration( return new WebSocketClientConfiguration(baseAddress, clientName); } - private static JsonDocument ReadExtensions(JsonElement element) - { - var extensionText = element.GetRawText(); - return JsonDocument.Parse(extensionText); - } - private static async Task ParseSubgraphConfigAsync( Stream stream, CancellationToken ct) @@ -210,7 +204,7 @@ private static async Task ParseSubgraphConfigAsync( using var document = await JsonDocument.ParseAsync(stream, cancellationToken: ct); var configs = new List(); var subgraph = default(string?); - var jsonDocument = default(JsonDocument?); + var extensions = default(JsonElement?); foreach (var property in document.RootElement.EnumerateObject()) { @@ -229,7 +223,7 @@ private static async Task ParseSubgraphConfigAsync( break; case "extensions": - jsonDocument = ReadExtensions(property.Value); + extensions = property.Value.SafeClone(); break; default: @@ -243,7 +237,7 @@ private static async Task ParseSubgraphConfigAsync( throw new InvalidOperationException("No subgraph name was specified."); } - return new SubgraphConfigurationDto(subgraph, configs, jsonDocument); + return new SubgraphConfigurationDto(subgraph, configs, extensions); } public static string FormatSubgraphConfig( @@ -291,7 +285,7 @@ public static string FormatSubgraphConfig( if (subgraphConfig.Extensions is not null) { writer.WritePropertyName("extensions"); - subgraphConfig.Extensions.WriteTo(writer); + subgraphConfig.Extensions.Value.WriteTo(writer); } writer.WriteEndObject(); diff --git a/src/HotChocolate/Fusion/src/CommandLine/Helpers/SubgraphConfigurationDto.cs b/src/HotChocolate/Fusion/src/CommandLine/Helpers/SubgraphConfigurationDto.cs index ced5083daff..8011b8c1fd6 100644 --- a/src/HotChocolate/Fusion/src/CommandLine/Helpers/SubgraphConfigurationDto.cs +++ b/src/HotChocolate/Fusion/src/CommandLine/Helpers/SubgraphConfigurationDto.cs @@ -8,7 +8,7 @@ internal sealed record SubgraphConfigurationDto public SubgraphConfigurationDto( string name, IReadOnlyList? clients = null, - JsonDocument? extensions = null) + JsonElement? extensions = null) { Name = name; Clients = clients ?? Array.Empty(); @@ -19,12 +19,12 @@ public SubgraphConfigurationDto( public IReadOnlyList Clients { get; init; } - public JsonDocument? Extensions { get; init; } + public JsonElement? Extensions { get; init; } public void Deconstruct( out string name, out IReadOnlyList clients, - out JsonDocument? extensions) + out JsonElement? extensions) { name = Name; clients = Clients; diff --git a/src/HotChocolate/Fusion/test/CommandLine.Tests/PackageHelperTests.cs b/src/HotChocolate/Fusion/test/CommandLine.Tests/PackageHelperTests.cs index e35eb29271f..d611e0d0ec2 100644 --- a/src/HotChocolate/Fusion/test/CommandLine.Tests/PackageHelperTests.cs +++ b/src/HotChocolate/Fusion/test/CommandLine.Tests/PackageHelperTests.cs @@ -43,7 +43,7 @@ public async Task Create_Extract_Extensions() { // arrange using var demoProject = await DemoProject.CreateAsync(); - var extensions = JsonDocument.Parse("{ \"foo\": \"bar\" }"); + var extensions = JsonDocument.Parse("{ \"foo\": \"bar\" }").RootElement; var accountConfig = demoProject.Accounts.ToConfiguration(AccountsExtensionSdl, extensions); var account = CreateFiles(accountConfig); var packageFile = CreateTempFile(); @@ -85,7 +85,7 @@ private void Match(SubgraphConfiguration config) using var stream = new MemoryStream(); using var writer = new Utf8JsonWriter(stream, new JsonWriterOptions { Indented = true }); - config.ConfigurationExtensions.WriteTo(writer); + config.ConfigurationExtensions.Value.WriteTo(writer); writer.Flush(); var json = Encoding.UTF8.GetString(stream.ToArray()); diff --git a/src/HotChocolate/Fusion/test/Shared/DemoSubgraph.cs b/src/HotChocolate/Fusion/test/Shared/DemoSubgraph.cs index e6656d118f9..251476ede77 100644 --- a/src/HotChocolate/Fusion/test/Shared/DemoSubgraph.cs +++ b/src/HotChocolate/Fusion/test/Shared/DemoSubgraph.cs @@ -62,7 +62,7 @@ public SubgraphConfiguration ToConfiguration( public SubgraphConfiguration ToConfiguration( string extensions, - JsonDocument configurationExtensions, + JsonElement configurationExtensions, bool onlyHttp = false) => onlyHttp ? new SubgraphConfiguration( diff --git a/src/HotChocolate/Utilities/src/Utilities/HotChocolate.Utilities.csproj b/src/HotChocolate/Utilities/src/Utilities/HotChocolate.Utilities.csproj index 6c270ca8a55..3aef4956611 100644 --- a/src/HotChocolate/Utilities/src/Utilities/HotChocolate.Utilities.csproj +++ b/src/HotChocolate/Utilities/src/Utilities/HotChocolate.Utilities.csproj @@ -16,6 +16,7 @@ +