Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System.Collections.Immutable;
using System.Text;
using HotChocolate.Fusion.Logging;
using HotChocolate.Fusion.Packaging;
using Microsoft.Extensions.Logging;

namespace HotChocolate.Fusion.Aspire;

internal static class AspireCompositionHelper
{
public static async Task<bool> TryComposeAsync(
string fusionArchivePath,
ImmutableArray<SourceSchemaInfo> newSourceSchemas,
GraphQLCompositionSettings settings,
ILogger<SchemaComposition> logger,
CancellationToken cancellationToken)
{
using var archive = File.Exists(fusionArchivePath)
? FusionArchive.Open(fusionArchivePath, FusionArchiveMode.Update)
: FusionArchive.Create(fusionArchivePath);

var compositionLog = new CompositionLog();
var environment = settings.EnvironmentName ?? "Aspire";
var compositionSettings = new CompositionSettings
{
Merger = { EnableGlobalObjectIdentification = settings.EnableGlobalObjectIdentification }
};
var sourceSchemas = newSourceSchemas.ToDictionary(
s => s.Name,
s => (s.Schema, s.SchemaSettings));

var result = await CompositionHelper.ComposeAsync(
compositionLog,
sourceSchemas,
archive,
environment,
compositionSettings,
cancellationToken);

var output = new StringBuilder();

foreach (var entry in compositionLog)
{
if (entry.Severity is LogSeverity.Error)
{
output.AppendLine($"‼️ {FormatMultilineMessage(entry.Message)}");
}
else
{
output.AppendLine(entry.Message);
}
}

if (result.IsFailure)
{
output.Append("❌ Composition failed:");
logger.LogError("{Message}", output.ToString());
return false;
}

output.Append("✅ Composition completed successfully.");
logger.LogInformation("{Message}", output.ToString());

return true;
}

/// <summary>
/// Since we're prefixing the message with an emoji and space before printing,
/// we need to also indent each line of a multiline message by three spaces to fix the alignment.
/// </summary>
private static string FormatMultilineMessage(string message)
{
var lines = message.Split(Environment.NewLine);

if (lines.Length <= 1)
{
return message;
}

return string.Join(Environment.NewLine + " ", lines);
}
}
175 changes: 0 additions & 175 deletions src/HotChocolate/Fusion-vnext/src/Fusion.Aspire/CompositionHelper.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public struct GraphQLCompositionSettings
/// <summary>
/// Gets or sets a value indicating whether Global Object Identification should be enabled.
/// </summary>
public bool EnableGlobalObjectIdentification { get; set; }
public bool? EnableGlobalObjectIdentification { get; set; }

/// <summary>
/// Gets or sets the environment name that shall be used for composition.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,19 @@ private async Task<bool> ComposeSchemaAsync(
return true;
}

var gatewayDirectory = GetProjectPath(compositionResource)!;
var archivePath = Path.Combine(Path.GetDirectoryName(gatewayDirectory)!, settings.OutputFileName);
return await ComposeSchemaAsync(archivePath, sourceSchemas, settings, cancellationToken);
try
{
var gatewayDirectory = GetProjectPath(compositionResource)!;
var archivePath = Path.Combine(Path.GetDirectoryName(gatewayDirectory)!, settings.OutputFileName);
return await ComposeSchemaAsync(archivePath, sourceSchemas, settings, cancellationToken);
}
finally
{
foreach (var sourceSchema in sourceSchemas)
{
sourceSchema.SchemaSettings.Dispose();
}
}
}
catch (OperationCanceledException)
{
Expand Down Expand Up @@ -247,7 +257,7 @@ private List<IResourceWithEndpoints> GetReferencedResources(
ResourceName = resource.Name,
HttpEndpointUrl = new Uri(schemaUrl),
Schema = new SourceSchemaText(sourceSchemaName, schemaText),
SchemaSettings = schemaSettings.Value
SchemaSettings = schemaSettings
};
}

Expand Down Expand Up @@ -281,11 +291,11 @@ private List<IResourceWithEndpoints> GetReferencedResources(
ResourceName = resource.Name,
HttpEndpointUrl = null, // No HTTP endpoint for file-based schemas
Schema = new SourceSchemaText(sourceSchemaName, schemaFromFile),
SchemaSettings = schemaSettings.Value
SchemaSettings = schemaSettings
};
}

private async Task<JsonElement?> GetSourceSchemaSettingsAsync(
private async Task<JsonDocument?> GetSourceSchemaSettingsAsync(
IResourceWithEndpoints resource,
string settingsFileName,
CancellationToken cancellationToken)
Expand All @@ -309,8 +319,7 @@ private List<IResourceWithEndpoints> GetReferencedResources(
}

var settingsJson = await File.ReadAllTextAsync(settingsFile, cancellationToken);
using var document = JsonDocument.Parse(settingsJson);
return document.RootElement.Clone();
return JsonDocument.Parse(settingsJson);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -487,7 +496,7 @@ private async Task<bool> ComposeSchemaAsync(
File.Copy(archivePath, tempArchivePath);
}

if (await CompositionHelper.TryComposeAsync(
if (await AspireCompositionHelper.TryComposeAsync(
tempArchivePath,
[.. sourceSchemas],
settings.Settings,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ internal sealed record SourceSchemaInfo
public string? ResourceName { get; init; }
public Uri? HttpEndpointUrl { get; init; }
public required SourceSchemaText Schema { get; init; }
public required JsonElement SchemaSettings { get; init; }
public required JsonDocument SchemaSettings { get; init; }
}
Loading
Loading