Skip to content

Commit

Permalink
Better Error Handling in Fusion CommandLine
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib committed Jun 30, 2023
1 parent f67fa01 commit 980cbbe
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,15 @@ private static async Task ExecuteAsync(
}

var composer = new FusionGraphComposer(prefix, prefixSelf, () => new ConsoleLog(console));
var fusionGraph = await composer.ComposeAsync(configs.Values, flags, cancellationToken);
var fusionGraphDoc = Utf8GraphQLParser.Parse(SchemaFormatter.FormatAsString(fusionGraph));
var fusionGraph = await composer.TryComposeAsync(configs.Values, flags, cancellationToken);

if (fusionGraph is null)
{
console.WriteLine("Fusion graph composition failed.");
return;
}

var fusionGraphDoc = Utf8GraphQLParser.Parse(SchemaFormatter.FormatAsString(fusionGraph));
var typeNames = FusionTypeNames.From(fusionGraphDoc);
var rewriter = new Metadata.FusionGraphConfigurationToSchemaRewriter();
var schemaDoc = (DocumentNode)rewriter.Rewrite(fusionGraphDoc, new(typeNames))!;
Expand Down
39 changes: 39 additions & 0 deletions src/HotChocolate/Fusion/src/Composition/FusionGraphComposer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,43 @@ public async ValueTask<Schema> ComposeAsync(
// Return the resulting merged schema.
return context.FusionGraph;
}

/// <summary>
/// Composes the subgraph schemas into a single,
/// merged schema representing the fusion gateway configuration.
/// </summary>
/// <param name="configurations">
/// The subgraph configurations to compose.
/// </param>
/// <param name="features">
/// The composition feature flags.
/// </param>
/// <param name="cancellationToken">
/// A cancellation token that can be used to cancel the operation.
/// </param>
/// <returns>The fusion gateway configuration.</returns>
public async ValueTask<Schema?> TryComposeAsync(
IEnumerable<SubgraphConfiguration> configurations,
FusionFeatureFlags features = FusionFeatureFlags.None,
CancellationToken cancellationToken = default)
{
var log = new DefaultCompositionLog(_logFactory?.Invoke());

// Create a new composition context with the given subgraph configurations,
// fusion type prefix, and fusion type self option.
var context = new CompositionContext(
configurations.ToArray(),
log,
_fusionTypePrefix,
_fusionTypeSelf)
{
Features = features,
Abort = cancellationToken
};

// Run the merge pipeline on the composition context.
await _pipeline(context);

return log.HasErrors ? null : context.FusionGraph;
}
}

0 comments on commit 980cbbe

Please sign in to comment.