5
5
using System . Threading . Channels ;
6
6
using HotChocolate . Buffers ;
7
7
using HotChocolate . Fusion . Logging ;
8
+ using HotChocolate . Fusion . Logging . Contracts ;
8
9
using HotChocolate . Fusion . Options ;
9
10
using HotChocolate . Fusion . Packaging ;
11
+ using HotChocolate . Fusion . Results ;
12
+ using HotChocolate . Types . Mutable ;
10
13
using static HotChocolate . Fusion . Properties . CommandLineResources ;
11
14
12
15
namespace HotChocolate . Fusion . CommandLine ;
@@ -361,7 +364,7 @@ await ComposeAsync(
361
364
}
362
365
}
363
366
364
- private static bool IsSchemaFile ( string ? fileName )
367
+ public static bool IsSchemaFile ( string ? fileName )
365
368
{
366
369
if ( fileName is null )
367
370
{
@@ -381,23 +384,61 @@ private static async Task<int> ComposeAsync(
381
384
bool printSchema ,
382
385
CancellationToken cancellationToken )
383
386
{
384
- environment ??= Environment . GetEnvironmentVariable ( "ASPNETCORE_ENVIRONMENT" ) ?? "Development" ;
385
-
386
- Dictionary < string , ( SourceSchemaText , JsonDocument ) > sourceSchemas ;
387
+ using var archive = File . Exists ( archiveFile )
388
+ ? FusionArchive . Open ( archiveFile , mode : FusionArchiveMode . Update )
389
+ : FusionArchive . Create ( archiveFile ) ;
387
390
388
391
try
389
392
{
390
- sourceSchemas = await ReadSourceSchemasAsync ( sourceSchemaFiles , cancellationToken ) ;
393
+ var compositionLog = new CompositionLog ( ) ;
394
+
395
+ var result = await ComposeAsync (
396
+ compositionLog ,
397
+ sourceSchemaFiles ,
398
+ archive ,
399
+ environment ,
400
+ enableGlobalObjectIdentification ,
401
+ cancellationToken ) ;
402
+
403
+ WriteCompositionLog (
404
+ compositionLog ,
405
+ writer : result . IsSuccess ? console . Out : console . Error ,
406
+ writeAsGraphQLComments : result . IsSuccess && printSchema ) ;
407
+
408
+ if ( result . IsFailure )
409
+ {
410
+ foreach ( var error in result . Errors )
411
+ {
412
+ console . Error . WriteLine ( error . Message ) ;
413
+ }
414
+
415
+ return 1 ;
416
+ }
417
+
418
+ console . Out . WriteLine ( printSchema
419
+ ? result . Value . ToString ( )
420
+ : string . Format ( ComposeCommand_CompositeSchemaFile_Written , archiveFile ) ) ;
421
+
422
+ return 0 ;
391
423
}
392
424
catch ( Exception e )
393
425
{
394
426
console . Error . WriteLine ( e . Message ) ;
395
427
return 1 ;
396
428
}
429
+ }
397
430
398
- using var archive = File . Exists ( archiveFile )
399
- ? FusionArchive . Open ( archiveFile , mode : FusionArchiveMode . Update )
400
- : FusionArchive . Create ( archiveFile ) ;
431
+ public static async Task < CompositionResult < MutableSchemaDefinition > > ComposeAsync (
432
+ ICompositionLog compositionLog ,
433
+ List < string > sourceSchemaFiles ,
434
+ FusionArchive archive ,
435
+ string ? environment ,
436
+ bool enableGlobalObjectIdentification ,
437
+ CancellationToken cancellationToken )
438
+ {
439
+ environment ??= Environment . GetEnvironmentVariable ( "ASPNETCORE_ENVIRONMENT" ) ?? "Development" ;
440
+
441
+ var sourceSchemas = await ReadSourceSchemasAsync ( sourceSchemaFiles , cancellationToken ) ;
401
442
var schemaNames = new SortedSet < string > ( StringComparer . Ordinal ) ;
402
443
403
444
await UpdateArchiveMetadata ( archive , schemaNames , cancellationToken ) ;
@@ -442,27 +483,16 @@ await archive.TryGetSourceSchemaConfigurationAsync(
442
483
EnableGlobalObjectIdentification = enableGlobalObjectIdentification
443
484
} ;
444
485
445
- var compositionLog = new CompositionLog ( ) ;
446
486
var schemaComposer = new SchemaComposer (
447
487
sourceSchemas . Values . Select ( t => t . Item1 ) ,
448
488
schemaComposerOptions ,
449
489
compositionLog ) ;
450
490
451
491
var result = schemaComposer . Compose ( ) ;
452
492
453
- WriteCompositionLog (
454
- compositionLog ,
455
- writer : result . IsSuccess ? console . Out : console . Error ,
456
- writeAsGraphQLComments : result . IsSuccess && printSchema ) ;
457
-
458
493
if ( result . IsFailure )
459
494
{
460
- foreach ( var error in result . Errors )
461
- {
462
- console . Error . WriteLine ( error . Message ) ;
463
- }
464
-
465
- return 1 ;
495
+ return result ;
466
496
}
467
497
468
498
bufferWriter . Reset ( ) ;
@@ -479,11 +509,7 @@ await archive.SetGatewayConfigurationAsync(
479
509
480
510
await archive . CommitAsync ( cancellationToken ) ;
481
511
482
- console . Out . WriteLine ( printSchema
483
- ? result . Value . ToString ( )
484
- : string . Format ( ComposeCommand_CompositeSchemaFile_Written , archiveFile ) ) ;
485
-
486
- return 0 ;
512
+ return result ;
487
513
}
488
514
489
515
private static async Task UpdateArchiveMetadata (
@@ -510,7 +536,7 @@ private static async Task UpdateArchiveMetadata(
510
536
await archive . SetArchiveMetadataAsync ( metadata , cancellationToken ) ;
511
537
}
512
538
513
- private static void WriteCompositionLog (
539
+ public static void WriteCompositionLog (
514
540
CompositionLog compositionLog ,
515
541
IStandardStreamWriter writer ,
516
542
bool writeAsGraphQLComments )
0 commit comments