Skip to content

Commit 84ae022

Browse files
[Fusion] ComposeCommand: Store source schema configurations in package (#8613)
1 parent 2e22212 commit 84ae022

File tree

1 file changed

+46
-55
lines changed

1 file changed

+46
-55
lines changed

src/HotChocolate/Fusion-vnext/src/Fusion.CommandLine/Commands/ComposeCommand.cs

Lines changed: 46 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -438,44 +438,33 @@ public static async Task<CompositionResult<MutableSchemaDefinition>> ComposeAsyn
438438
{
439439
environment ??= Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Development";
440440

441-
var sourceSchemas = await ReadSourceSchemasAsync(sourceSchemaFiles, cancellationToken);
442-
var schemaNames = new SortedSet<string>(StringComparer.Ordinal);
443-
444-
await UpdateArchiveMetadata(archive, schemaNames, cancellationToken);
441+
var sourceSchemas = await ReadSourceSchemasAsync(
442+
sourceSchemaFiles,
443+
cancellationToken);
445444

446-
using var bufferWriter = new PooledArrayWriter();
445+
var sourceSchemaNamesInPackage = new SortedSet<string>(
446+
await archive.GetSourceSchemaNamesAsync(cancellationToken),
447+
StringComparer.Ordinal);
447448

448-
foreach (var schemaName in schemaNames)
449+
foreach (var schemaName in sourceSchemaNamesInPackage)
449450
{
450-
if (!sourceSchemas.ContainsKey(schemaName))
451+
if (sourceSchemas.ContainsKey(schemaName))
451452
{
452-
var sourceSchemaConfiguration =
453-
await archive.TryGetSourceSchemaConfigurationAsync(
454-
schemaName,
455-
cancellationToken);
453+
// We have a new configuration for the schema, so we'll take that
454+
// instead of the one in the gateway package.
455+
continue;
456+
}
456457

457-
if (sourceSchemaConfiguration is null)
458-
{
459-
continue;
460-
}
458+
var configuration = await archive.TryGetSourceSchemaConfigurationAsync(schemaName, cancellationToken);
461459

462-
bufferWriter.Reset();
460+
if (configuration is null)
461+
{
462+
continue;
463+
}
463464

464-
await using (var stream = await sourceSchemaConfiguration.OpenReadSchemaAsync(cancellationToken))
465-
{
466-
int read;
465+
var sourceText = await ReadSchemaSourceTextAsync(configuration, cancellationToken);
467466

468-
do
469-
{
470-
var memory = bufferWriter.GetMemory(4096);
471-
read = await stream.ReadAsync(memory, cancellationToken);
472-
bufferWriter.Advance(read);
473-
} while (read > 0);
474-
}
475-
476-
var sourceText = new SourceSchemaText(schemaName, Encoding.UTF8.GetString(bufferWriter.WrittenSpan));
477-
sourceSchemas[schemaName] = (sourceText, sourceSchemaConfiguration.Settings);
478-
}
467+
sourceSchemas[schemaName] = (new SourceSchemaText(schemaName, sourceText), configuration.Settings);
479468
}
480469

481470
var schemaComposerOptions = new SchemaComposerOptions
@@ -495,12 +484,29 @@ await archive.TryGetSourceSchemaConfigurationAsync(
495484
return result;
496485
}
497486

498-
bufferWriter.Reset();
487+
using var bufferWriter = new PooledArrayWriter();
499488
new SettingsComposer().Compose(
500489
bufferWriter,
501490
sourceSchemas.Values.Select(t => t.Item2.RootElement).ToArray(),
502491
environment);
503492

493+
var metadata = new ArchiveMetadata
494+
{
495+
SupportedGatewayFormats = [new Version(2, 0, 0)],
496+
SourceSchemas = [..sourceSchemas.Keys]
497+
};
498+
499+
await archive.SetArchiveMetadataAsync(metadata, cancellationToken);
500+
501+
foreach (var (schemaName, (schema, settings)) in sourceSchemas)
502+
{
503+
await archive.SetSourceSchemaConfigurationAsync(
504+
schemaName,
505+
Encoding.UTF8.GetBytes(schema.SourceText),
506+
settings,
507+
cancellationToken);
508+
}
509+
504510
await archive.SetGatewayConfigurationAsync(
505511
result.Value + Environment.NewLine,
506512
JsonDocument.Parse(bufferWriter.WrittenMemory),
@@ -512,30 +518,6 @@ await archive.SetGatewayConfigurationAsync(
512518
return result;
513519
}
514520

515-
private static async Task UpdateArchiveMetadata(
516-
FusionArchive archive,
517-
SortedSet<string> sourceSchemaNames,
518-
CancellationToken cancellationToken)
519-
{
520-
var metadata = await archive.GetArchiveMetadataAsync(cancellationToken);
521-
522-
if (metadata is not null)
523-
{
524-
foreach (var schemaName in metadata.SourceSchemas)
525-
{
526-
sourceSchemaNames.Add(schemaName);
527-
}
528-
}
529-
530-
metadata = new ArchiveMetadata
531-
{
532-
SupportedGatewayFormats = [new Version(2, 0, 0)],
533-
SourceSchemas = [.. sourceSchemaNames]
534-
};
535-
536-
await archive.SetArchiveMetadataAsync(metadata, cancellationToken);
537-
}
538-
539521
public static void WriteCompositionLog(
540522
CompositionLog compositionLog,
541523
IStandardStreamWriter writer,
@@ -640,6 +622,15 @@ static async Task ReadSourceSchemaAsync(
640622
}
641623
}
642624

625+
private static async Task<string> ReadSchemaSourceTextAsync(
626+
SourceSchemaConfiguration configuration,
627+
CancellationToken cancellationToken)
628+
{
629+
await using var stream = await configuration.OpenReadSchemaAsync(cancellationToken);
630+
using var reader = new StreamReader(stream, Encoding.UTF8);
631+
return await reader.ReadToEndAsync(cancellationToken);
632+
}
633+
643634
/// <summary>
644635
/// Since we're prefixing the message with an emoji and space before printing,
645636
/// we need to also indent each line of a multiline message by three spaces to fix the alignment.

0 commit comments

Comments
 (0)