Skip to content

Commit 3569b38

Browse files
authored
Added composition watch mode refinements. (#8471)
1 parent a830577 commit 3569b38

File tree

1 file changed

+51
-17
lines changed

1 file changed

+51
-17
lines changed

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

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ private static async Task<int> WatchComposeAsync(
147147
cancellationToken);
148148

149149
// set up file watcher for source schema files
150-
fileWatcher.Filter = "*.graphqls";
150+
fileWatcher.Filter = "*.graphql*";
151151
fileWatcher.IncludeSubdirectories = true;
152152
fileWatcher.NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.LastWrite | NotifyFilters.FileName;
153153

@@ -208,10 +208,20 @@ private static async Task<int> WatchComposeAsync(
208208

209209
void OnSourceSchemaFileChanged(FileSystemEventArgs e)
210210
{
211+
// skip if this is the gateway file as this is handled somewhere else.
212+
if (compositeSchemaFile is not null)
213+
{
214+
var compositeSchemaPath = Path.Combine(workingDirectory, compositeSchemaFile);
215+
if (string.Equals(e.FullPath, compositeSchemaPath, StringComparison.OrdinalIgnoreCase))
216+
{
217+
return;
218+
}
219+
}
220+
211221
// only process files that are in our source schema paths or match the pattern
212222
var isRelevantFile = sourceSchemaFilePaths.Any(path =>
213223
string.Equals(path, e.FullPath, StringComparison.OrdinalIgnoreCase))
214-
|| e.Name?.EndsWith(".graphqls", StringComparison.OrdinalIgnoreCase) == true;
224+
|| IsGraphQLSchemaFile(e.Name);
215225

216226
if (!isRelevantFile)
217227
{
@@ -222,7 +232,17 @@ void OnSourceSchemaFileChanged(FileSystemEventArgs e)
222232
}
223233

224234
void OnGatewayFileDeleted(FileSystemEventArgs e)
225-
=> TriggerComposition($"Gateway file deleted: {e.Name}");
235+
{
236+
// we wait 500ms to see if the gateway file is recreated.
237+
Thread.Sleep(500);
238+
239+
// if it's still missing we trigger a new composition.
240+
var compositeSchemaPath = Path.Combine(workingDirectory, compositeSchemaFile);
241+
if (!File.Exists(compositeSchemaPath))
242+
{
243+
TriggerComposition($"Gateway file deleted: {e.Name}");
244+
}
245+
}
226246

227247
void TriggerComposition(string reason)
228248
=> compositionChannel.Writer.TryWrite(reason);
@@ -255,29 +275,44 @@ private static async Task ProcessCompositionRequestsAsync(
255275
lastComposition = DateTime.UtcNow;
256276

257277
console.Out.WriteLine($"\n🔄 {reason}");
258-
console.Out.WriteLine("Recomposing schema...");
259278

260279
// Add a small delay to ensure file operations are complete
261-
await Task.Delay(100, cancellationToken);
262-
263-
await ComposeAsync(console, workingDirectory, sourceSchemaFiles, compositeSchemaFile, cancellationToken);
264-
265-
console.Out.WriteLine("✅ Recomposition complete");
266-
console.Out.WriteLine("👀 Watching for changes...");
280+
await Task.Delay(200, cancellationToken);
281+
282+
await ComposeAsync(
283+
console,
284+
workingDirectory,
285+
sourceSchemaFiles,
286+
compositeSchemaFile,
287+
cancellationToken);
267288
}
268289
catch (Exception ex) when (ex is not OperationCanceledException)
269290
{
270291
console.Error.WriteLine($"❌ Error during recomposition: {ex.Message}");
292+
}
293+
finally
294+
{
271295
console.Out.WriteLine("👀 Watching for changes...");
272296
}
273297
}
274298
}
275299

300+
private static bool IsGraphQLSchemaFile(string? fileName)
301+
{
302+
if (fileName is null)
303+
{
304+
return false;
305+
}
306+
307+
return fileName.EndsWith(".graphql", StringComparison.OrdinalIgnoreCase)
308+
|| fileName.EndsWith(".graphqls", StringComparison.OrdinalIgnoreCase);
309+
}
310+
276311
private static ImmutableSortedSet<string> GetSourceSchemaFilePaths(
277312
List<string> sourceSchemaFiles,
278313
string workingDirectory)
279314
{
280-
// If no source schema files were specified, scan the working directory for *.graphqls files
315+
// if no source schema files were specified, scan the working directory for *.graphql* files
281316
if (sourceSchemaFiles.Count > 0)
282317
{
283318
return sourceSchemaFiles
@@ -286,7 +321,8 @@ private static ImmutableSortedSet<string> GetSourceSchemaFilePaths(
286321
}
287322

288323
var foundFiles = new DirectoryInfo(workingDirectory)
289-
.GetFiles("*.graphqls", SearchOption.AllDirectories)
324+
.GetFiles("*.graphql*", SearchOption.AllDirectories)
325+
.Where(f => IsGraphQLSchemaFile(f.Name))
290326
.Select(i => i.FullName)
291327
.ToImmutableSortedSet();
292328

@@ -317,13 +353,11 @@ private static async Task<int> ComposeAsync(
317353
catch (Exception e)
318354
{
319355
console.Error.WriteLine(e.Message);
320-
321356
return 1;
322357
}
323358

324359
var compositionLog = new CompositionLog();
325360
var schemaComposer = new SchemaComposer(sourceSchemas, compositionLog);
326-
327361
var result = schemaComposer.Compose();
328362

329363
WriteCompositionLog(
@@ -416,13 +450,13 @@ private static async Task<IEnumerable<string>> ReadSourceSchemasAsync(
416450
{
417451
ImmutableSortedSet<string> sourceSchemaFilePaths;
418452

419-
// If no source schema files were specified, scan the working directory for *.graphqls
420-
// files.
453+
// If no source schema files were specified, scan the working directory for *.graphql* files
421454
if (sourceSchemaFiles.Count == 0)
422455
{
423456
sourceSchemaFilePaths =
424457
new DirectoryInfo(workingDirectory)
425-
.GetFiles("*.graphqls")
458+
.GetFiles("*.graphql*")
459+
.Where(f => IsGraphQLSchemaFile(f.Name))
426460
.Select(i => i.FullName)
427461
.ToImmutableSortedSet();
428462

0 commit comments

Comments
 (0)