Skip to content

Commit

Permalink
Allow config command to edit subgraph package.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib committed Jul 5, 2023
1 parent 7b492e8 commit d0ac6ab
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
using HotChocolate.Fusion.CommandLine.Helpers;
using HotChocolate.Fusion.CommandLine.Options;
using HotChocolate.Fusion.Composition;
using HotChocolate.Utilities;
using static System.IO.Path;
using static HotChocolate.Fusion.CommandLine.Defaults;
using static HotChocolate.Fusion.CommandLine.Helpers.PackageHelper;

namespace HotChocolate.Fusion.CommandLine.Commands;

Expand Down Expand Up @@ -61,19 +63,33 @@ private static async Task ExecuteAsync(
{
new HttpClientConfiguration(uri, clientName)
});
var configJson = PackageHelper.FormatSubgraphConfig(config);
var configJson = FormatSubgraphConfig(config);
await File.WriteAllTextAsync(configFile.FullName, configJson, cancellationToken);
}
else if (configFile.Extension.EqualsOrdinal(".fsp"))
{
var config = await LoadSubgraphConfigFromSubgraphPackageAsync(configFile.FullName, cancellationToken);

var clients = config.Clients.ToList();

clients.RemoveAll(t => t is HttpClientConfiguration);
clients.Add(new HttpClientConfiguration(uri, clientName));

await ReplaceSubgraphConfigInSubgraphPackageAsync(
configFile.FullName,
config with { Clients = clients },
cancellationToken);
}
else
{
var config = await PackageHelper.LoadSubgraphConfigAsync(configFile.FullName, cancellationToken);
var config = await LoadSubgraphConfigAsync(configFile.FullName, cancellationToken);

var clients = config.Clients.ToList();

clients.RemoveAll(t => t is HttpClientConfiguration);
clients.Add(new HttpClientConfiguration(uri, clientName));

var configJson = PackageHelper.FormatSubgraphConfig(config with { Clients = clients });
var configJson = FormatSubgraphConfig(config with { Clients = clients });
await File.WriteAllTextAsync(configFile.FullName, configJson, cancellationToken);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.CommandLine;
using HotChocolate.Fusion.CommandLine.Helpers;
using HotChocolate.Fusion.CommandLine.Options;
using HotChocolate.Utilities;
using static System.IO.Path;
using static HotChocolate.Fusion.CommandLine.Helpers.PackageHelper;
using static HotChocolate.Fusion.CommandLine.Defaults;
Expand Down Expand Up @@ -49,6 +50,15 @@ private static async Task ExecuteAsync(
var configJson = FormatSubgraphConfig(config);
await File.WriteAllTextAsync(configFile.FullName, configJson, cancellationToken);
}
else if (configFile.Extension.EqualsOrdinal(".fsp"))
{
var config = await LoadSubgraphConfigFromSubgraphPackageAsync(configFile.FullName, cancellationToken);

await ReplaceSubgraphConfigInSubgraphPackageAsync(
configFile.FullName,
config with { Name = subgraphName },
cancellationToken);
}
else
{
var config = await LoadSubgraphConfigAsync(configFile.FullName, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
using HotChocolate.Fusion.CommandLine.Helpers;
using HotChocolate.Fusion.CommandLine.Options;
using HotChocolate.Fusion.Composition;
using HotChocolate.Utilities;
using static System.IO.Path;
using static HotChocolate.Fusion.CommandLine.Defaults;
using static HotChocolate.Fusion.CommandLine.Helpers.PackageHelper;

namespace HotChocolate.Fusion.CommandLine.Commands;

Expand Down Expand Up @@ -61,19 +63,33 @@ private static async Task ExecuteAsync(
{
new WebSocketClientConfiguration(uri, clientName)
});
var configJson = PackageHelper.FormatSubgraphConfig(config);
var configJson = FormatSubgraphConfig(config);
await File.WriteAllTextAsync(configFile.FullName, configJson, cancellationToken);
}
else if (configFile.Extension.EqualsOrdinal(".fsp"))
{
var config = await LoadSubgraphConfigFromSubgraphPackageAsync(configFile.FullName, cancellationToken);

var clients = config.Clients.ToList();

clients.RemoveAll(t => t is WebSocketClientConfiguration);
clients.Add(new WebSocketClientConfiguration(uri, clientName));

await ReplaceSubgraphConfigInSubgraphPackageAsync(
configFile.FullName,
config with { Clients = clients },
cancellationToken);
}
else
{
var config = await PackageHelper.LoadSubgraphConfigAsync(configFile.FullName, cancellationToken);
var config = await LoadSubgraphConfigAsync(configFile.FullName, cancellationToken);

var clients = config.Clients.ToList();

clients.RemoveAll(t => t is WebSocketClientConfiguration);
clients.Add(new WebSocketClientConfiguration(uri, clientName));

var configJson = PackageHelper.FormatSubgraphConfig(config with { Clients = clients });
var configJson = FormatSubgraphConfig(config with { Clients = clients });
await File.WriteAllTextAsync(configFile.FullName, configJson, cancellationToken);
}
}
Expand Down
105 changes: 32 additions & 73 deletions src/HotChocolate/Fusion/src/CommandLine/Helpers/PackageHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ internal static class PackageHelper
private const string _schemaExtensionKind = "urn:graphql:schema-extensions";
private const string _subgraphConfigKind = "urn:hotchocolate:fusion:subgraph-config";
private const string _subgraphConfigId = "subgraph-config";
private const string _subgraphPackageKind = "urn:hotchocolate:fusion:subgraph-package";

public static async Task CreateSubgraphPackageAsync(
string packageFile,
Expand All @@ -46,6 +45,24 @@ public static async Task CreateSubgraphPackageAsync(
await AddTransportConfigToPackage(package, transportConfig);
await AddSchemaExtensionsToPackage(package, extensions);
}

public static async Task<SubgraphConfigurationDto> LoadSubgraphConfigFromSubgraphPackageAsync(
string packageFile,
CancellationToken ct = default)
{
using var package = Package.Open(packageFile, FileMode.Open, FileAccess.Read);
var transportConfig = await ReadSubgraphConfigPartAsync(package, ct);
return transportConfig;
}

public static async Task ReplaceSubgraphConfigInSubgraphPackageAsync(
string packageFile,
SubgraphConfigurationDto config,
CancellationToken ct = default)
{
using var package = Package.Open(packageFile, FileMode.Open, FileAccess.Read);
await ReplaceTransportConfigInPackageAsync(package, config);
}

public static async Task CreateSubgraphPackageAsync(
Stream stream,
Expand All @@ -66,32 +83,6 @@ public static async Task CreateSubgraphPackageAsync(
await AddSchemaExtensionsToPackage(package, extensions);
}

public static async Task CreateFusionPackageAsync(
string packageFile,
DocumentNode schema,
DocumentNode fusionGraph,
IEnumerable<SubgraphConfiguration> subgraphConfigs)
{
if (File.Exists(packageFile))
{
File.Delete(packageFile);
}

using var package = Package.Open(packageFile, FileMode.Create);

await AddSchemaToPackageAsync(package, schema);
await AddFusionGraphToPackageAsync(package, fusionGraph);

foreach (var subgraphConfig in subgraphConfigs)
{
var uri = CreatePartUri(new Uri($"subgraphs/{subgraphConfig.Name}", Relative));
var part = package.CreatePart(uri, "application/subgraph-package");
await using var stream = part.GetStream(FileMode.Create);
await CreateSubgraphPackageAsync(stream, subgraphConfig);
package.CreateRelationship(part.Uri, TargetMode.Internal, _subgraphPackageKind);
}
}

public static async Task<SubgraphConfiguration> ReadSubgraphPackageAsync(
string packageFile,
CancellationToken cancellationToken = default)
Expand All @@ -115,25 +106,6 @@ public static async Task<SubgraphConfiguration> ReadSubgraphPackageAsync(
subgraphConfig.Clients);
}

public static async Task<SubgraphConfiguration[]> ReadSubgraphConfigsFromFusionPackageAsync(
string packageFile,
CancellationToken cancellationToken = default)
{
using var package = Package.Open(packageFile, FileMode.Open, FileAccess.Read);
var configs = new List<SubgraphConfiguration>();

foreach (var extensionRel in package.GetRelationshipsByType(_subgraphPackageKind))
{
var schemaPart = package.GetPart(extensionRel.TargetUri);
await using var stream = schemaPart.GetStream(FileMode.Open, FileAccess.Read);
using var subgraphPackage = Package.Open(stream, FileMode.Open, FileAccess.Read);
var config = await ReadSubgraphPackageAsync(subgraphPackage, cancellationToken);
configs.Add(config);
}

return configs.ToArray();
}

public static async Task ExtractSubgraphPackageAsync(
string packageFile,
string targetDirectory,
Expand Down Expand Up @@ -303,33 +275,6 @@ public static string FormatSubgraphConfig(
return Encoding.UTF8.GetString(buffer.WrittenSpan);
}

private static async Task AddFusionGraphToPackageAsync(
Package package,
DocumentNode schema)
{
var uri = CreatePartUri(new Uri("fusion.graphql", Relative));
var part = package.CreatePart(uri, "application/graphql-schema");

await using var stream = part.GetStream(FileMode.Create);
await using var writer = new StreamWriter(stream, Encoding.UTF8);
await writer.WriteAsync(schema.ToString(true));

package.CreateRelationship(part.Uri, TargetMode.Internal, _fusionKind, _fusionId);
}

private static async Task<DocumentNode> ReadFusionGraphPartAsync(
Package package,
CancellationToken ct)
{
var schemaRel = package.GetRelationship(_fusionId);
var schemaPart = package.GetPart(schemaRel.TargetUri);

await using var stream = schemaPart.GetStream(FileMode.Open);
using var reader = new StreamReader(stream, Encoding.UTF8);
var schema = await reader.ReadToEndAsync(ct);
return Utf8GraphQLParser.Parse(schema);
}

private static async Task AddSchemaToPackageAsync(
Package package,
DocumentNode schema)
Expand Down Expand Up @@ -406,6 +351,20 @@ private static async Task AddTransportConfigToPackage(

package.CreateRelationship(part.Uri, TargetMode.Internal, _subgraphConfigKind, _subgraphConfigId);
}

private static async Task ReplaceTransportConfigInPackageAsync(
Package package,
SubgraphConfigurationDto subgraphConfig)
{
var uri = CreatePartUri(new Uri("subgraph.json", Relative));
var part = package.GetPart(uri);

await using var stream = part.GetStream(FileMode.Create);
await using var writer = new StreamWriter(stream, Encoding.UTF8);
await writer.WriteAsync(FormatSubgraphConfig(subgraphConfig));

package.CreateRelationship(part.Uri, TargetMode.Internal, _subgraphConfigKind, _subgraphConfigId);
}

private static async Task<SubgraphConfigurationDto> ReadSubgraphConfigPartAsync(
Package package,
Expand Down

0 comments on commit d0ac6ab

Please sign in to comment.