22using System . Text . Json ;
33using Aspire . Hosting ;
44using Aspire . Hosting . ApplicationModel ;
5+ using Aspire . Hosting . Eventing ;
56using Aspire . Hosting . Lifecycle ;
67using Microsoft . Extensions . Hosting ;
78using Microsoft . Extensions . Logging ;
@@ -11,47 +12,54 @@ namespace HotChocolate.Fusion.Aspire;
1112internal sealed class SchemaComposition (
1213 IHostApplicationLifetime lifetime ,
1314 ILogger < SchemaComposition > logger )
14- : IDistributedApplicationLifecycleHook
15+ : IDistributedApplicationEventingSubscriber
1516{
16- public async Task AfterResourcesCreatedAsync (
17- DistributedApplicationModel appModel ,
18- CancellationToken cancellationToken = default )
17+ public Task SubscribeAsync (
18+ IDistributedApplicationEventing eventing ,
19+ DistributedApplicationExecutionContext executionContext ,
20+ CancellationToken cancellationToken )
1921 {
20- var compositionFailed = false ;
21-
22- try
22+ eventing . Subscribe < BeforeStartEvent > ( async ( @event , ct ) =>
2323 {
24- // Find all resources that need schema composition
25- var compositionResources = appModel . GetGraphQLCompositionResources ( ) . ToList ( ) ;
24+ var model = @event . Model ;
25+ var compositionFailed = false ;
2626
27- if ( compositionResources . Count == 0 )
27+ try
2828 {
29- logger . LogDebug ( "No resources found that need GraphQL schema composition" ) ;
30- return ;
31- }
29+ // Find all resources that need schema composition
30+ var compositionResources = model . GetGraphQLCompositionResources ( ) . ToList ( ) ;
3231
33- logger . LogInformation ( "Starting GraphQL schema composition..." ) ;
32+ if ( compositionResources . Count == 0 )
33+ {
34+ logger . LogDebug ( "No resources found that need GraphQL schema composition" ) ;
35+ return ;
36+ }
3437
35- // Process each composition resource
36- foreach ( var compositionResource in compositionResources )
37- {
38- if ( ! await ComposeSchemaAsync ( compositionResource , appModel , cancellationToken ) )
38+ logger . LogInformation ( "Starting GraphQL schema composition..." ) ;
39+
40+ // Process each composition resource
41+ foreach ( var compositionResource in compositionResources )
3942 {
40- compositionFailed = true ;
43+ if ( ! await ComposeSchemaAsync ( compositionResource , model , ct ) )
44+ {
45+ compositionFailed = true ;
46+ }
4147 }
4248 }
43- }
44- catch
45- {
46- compositionFailed = true ;
47- }
49+ catch
50+ {
51+ compositionFailed = true ;
52+ }
4853
49- if ( compositionFailed )
50- {
51- logger . LogCritical ( "GraphQL schema composition failed - stopping application" ) ;
52- lifetime . StopApplication ( ) ;
53- throw new InvalidOperationException ( "GraphQL schema composition failed" ) ;
54- }
54+ if ( compositionFailed )
55+ {
56+ logger . LogCritical ( "GraphQL schema composition failed - stopping application" ) ;
57+ lifetime . StopApplication ( ) ;
58+ throw new InvalidOperationException ( "GraphQL schema composition failed" ) ;
59+ }
60+ } ) ;
61+
62+ return Task . CompletedTask ;
5563 }
5664
5765 private async Task < bool > ComposeSchemaAsync (
0 commit comments