Skip to content

Commit

Permalink
Move to JsonElement in ConfigurationExtensions (#6580)
Browse files Browse the repository at this point in the history
  • Loading branch information
PascalSenn authored Oct 9, 2023
1 parent 59b0e80 commit 23e4e1e
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,8 @@ private async Task<PackagePart> 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<ItemGroup>
<InternalsVisibleTo Include="HotChocolate.Fusion" />
<InternalsVisibleTo Include="HotChocolate.Fusion.Composition" />
<InternalsVisibleTo Include="HotChocolate.Fusion.CommandLine" />
</ItemGroup>

<ItemGroup>
Expand Down
18 changes: 18 additions & 0 deletions src/HotChocolate/Fusion/src/Abstractions/JsonElementExtensions.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
13 changes: 10 additions & 3 deletions src/HotChocolate/Fusion/src/Abstractions/SubgraphConfigJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ internal sealed record SubgraphConfigJson
/// <param name="clients">
/// The list of clients that can be used to fetch data from this subgraph.
/// </param>
/// <param name="extensions">
/// The "extensions" property of the subgraph-config.json.
/// </param>
public SubgraphConfigJson(
string name,
IReadOnlyList<IClientConfiguration>? clients = null,
JsonDocument? extensions = null)
JsonElement? extensions = null)
{
Name = name;
Clients = clients ?? Array.Empty<IClientConfiguration>();
Extensions = extensions;
}

/// <summary>
Expand All @@ -39,7 +43,7 @@ public SubgraphConfigJson(
/// <summary>
/// Gets the "extensions" property of the subgraph-config.json.
/// </summary>
public JsonDocument? Extensions { get; init; }
public JsonElement? Extensions { get; init; }

/// <summary>
/// Deconstructs the <see cref="SubgraphConfigJson"/> into its components.
Expand All @@ -50,10 +54,13 @@ public SubgraphConfigJson(
/// <param name="clients">
/// The list of clients that can be used to fetch data from this subgraph.
/// </param>
/// <param name="extensions">
/// The "extensions" property of the subgraph-config.json.
/// </param>
public void Deconstruct(
out string name,
out IReadOnlyList<IClientConfiguration> clients,
out JsonDocument? extensions)
out JsonElement? extensions)
{
name = Name;
clients = Clients;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -176,6 +182,7 @@ public static async Task<SubgraphConfigJson> ParseAsync(
stream, cancellationToken: cancellationToken);
var configs = new List<IClientConfiguration>();
var subgraph = default(string?);
var extensions = default(JsonElement?);

foreach (var property in document.RootElement.EnumerateObject())
{
Expand All @@ -193,6 +200,10 @@ public static async Task<SubgraphConfigJson> 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.");
Expand All @@ -204,7 +215,7 @@ public static async Task<SubgraphConfigJson> ParseAsync(
throw new InvalidOperationException("No subgraph name was specified.");
}

return new SubgraphConfigJson(subgraph, configs);
return new SubgraphConfigJson(subgraph, configs, extensions);
}

private static HttpClientConfiguration ReadHttpClientConfiguration(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public SubgraphConfiguration(
string schema,
IReadOnlyList<string> extensions,
IReadOnlyList<IClientConfiguration> clients,
JsonDocument? configurationExtensions)
JsonElement? configurationExtensions)
{
Name = name;
Schema = schema;
Expand Down Expand Up @@ -62,7 +62,7 @@ public SubgraphConfiguration(
string schema,
string extensions,
IReadOnlyList<IClientConfiguration> clients,
JsonDocument? configurationExtensions)
JsonElement? configurationExtensions)
: this(name, schema, new[] { extensions }, clients, configurationExtensions) { }

/// <summary>
Expand All @@ -88,7 +88,7 @@ public SubgraphConfiguration(
string schema,
string extensions,
IClientConfiguration client,
JsonDocument? configurationExtensions)
JsonElement? configurationExtensions)
: this(name, schema, new[] { extensions }, new[] { client }, configurationExtensions) { }

/// <summary>
Expand All @@ -114,5 +114,5 @@ public SubgraphConfiguration(
/// <summary>
/// Gets the configuration extension.
/// </summary>
public JsonDocument? ConfigurationExtensions { get; }
public JsonElement? ConfigurationExtensions { get; }
}
14 changes: 4 additions & 10 deletions src/HotChocolate/Fusion/src/CommandLine/Helpers/PackageHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,20 +197,14 @@ 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<SubgraphConfigurationDto> ParseSubgraphConfigAsync(
Stream stream,
CancellationToken ct)
{
using var document = await JsonDocument.ParseAsync(stream, cancellationToken: ct);
var configs = new List<IClientConfiguration>();
var subgraph = default(string?);
var jsonDocument = default(JsonDocument?);
var extensions = default(JsonElement?);

foreach (var property in document.RootElement.EnumerateObject())
{
Expand All @@ -229,7 +223,7 @@ private static async Task<SubgraphConfigurationDto> ParseSubgraphConfigAsync(
break;

case "extensions":
jsonDocument = ReadExtensions(property.Value);
extensions = property.Value.SafeClone();
break;

default:
Expand All @@ -243,7 +237,7 @@ private static async Task<SubgraphConfigurationDto> 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(
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal sealed record SubgraphConfigurationDto
public SubgraphConfigurationDto(
string name,
IReadOnlyList<IClientConfiguration>? clients = null,
JsonDocument? extensions = null)
JsonElement? extensions = null)
{
Name = name;
Clients = clients ?? Array.Empty<IClientConfiguration>();
Expand All @@ -19,12 +19,12 @@ public SubgraphConfigurationDto(

public IReadOnlyList<IClientConfiguration> Clients { get; init; }

public JsonDocument? Extensions { get; init; }
public JsonElement? Extensions { get; init; }

public void Deconstruct(
out string name,
out IReadOnlyList<IClientConfiguration> clients,
out JsonDocument? extensions)
out JsonElement? extensions)
{
name = Name;
clients = Clients;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion src/HotChocolate/Fusion/test/Shared/DemoSubgraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public SubgraphConfiguration ToConfiguration(

public SubgraphConfiguration ToConfiguration(
string extensions,
JsonDocument configurationExtensions,
JsonElement configurationExtensions,
bool onlyHttp = false)
=> onlyHttp
? new SubgraphConfiguration(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<InternalsVisibleTo Include="HotChocolate.AspNetCore.Tests" />
<InternalsVisibleTo Include="HotChocolate.AspNetCore.Tests.Utilities" />
<InternalsVisibleTo Include="HotChocolate.Fusion" />
<InternalsVisibleTo Include="HotChocolate.Fusion.Abstractions" />
<InternalsVisibleTo Include="HotChocolate.OpenApi" />
<InternalsVisibleTo Include="HotChocolate.Transport.Http" />
<InternalsVisibleTo Include="HotChocolate.Transport.Sockets.Client" />
Expand Down

0 comments on commit 23e4e1e

Please sign in to comment.