Skip to content

Commit

Permalink
Fixed issue with the file watcher for the gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib committed Apr 2, 2023
1 parent 215d5e5 commit fb87acc
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 160 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,17 @@ public FileWatcherTypeModule(string fileName)
throw new ArgumentNullException(nameof(fileName));
}

_watcher = new FileSystemWatcher(fileName);
var directory = Path.GetDirectoryName(fileName);
if (directory is null)
{
throw new FileNotFoundException(
"The file name must contain a directory path.",
fileName);
}

_watcher = new FileSystemWatcher();
_watcher.Path = directory;
_watcher.Filter = fileName;

_watcher.NotifyFilter =
NotifyFilters.Attributes |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ private static async ValueTask<DocumentNode> LoadDocumentAsync(
// We first try to load the file name as a fusion graph package.
// This might fails as a the file that was provided is a fusion
// graph document.
await using var package = FusionGraphPackage.Open(fileName);
await using var package = FusionGraphPackage.Open(fileName, FileAccess.Read);
return await package.GetFusionGraphAsync(cancellationToken);
}
catch
Expand Down
167 changes: 167 additions & 0 deletions src/HotChocolate/Fusion/test/CommandLine.Tests/ComposeCommandTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
using System.Collections.Concurrent;
using System.CommandLine.Parsing;
using CookieCrumble;
using HotChocolate.Fusion;
using HotChocolate.Fusion.CommandLine;
using HotChocolate.Fusion.CommandLine.Helpers;
using HotChocolate.Fusion.Composition;
using HotChocolate.Fusion.Shared;
using static HotChocolate.Fusion.Shared.DemoProjectSchemaExtensions;

namespace CommandLine.Tests;

public class ComposeCommandTests : IDisposable
{
private readonly ConcurrentBag<string> _files = new();

[Fact]
public async Task Compose_Fusion_Graph()
{
// arrange
using var demoProject = await DemoProject.CreateAsync();
var accountConfig = demoProject.Accounts.ToConfiguration(AccountsExtensionSdl);
var account = CreateFiles(accountConfig);
var subgraphPackageFile = CreateTempFile();

await PackageHelper.CreateSubgraphPackageAsync(
subgraphPackageFile,
new SubgraphFiles(
account.SchemaFile,
account.TransportConfigFile,
account.ExtensionFiles));

var packageFile = CreateTempFile();

// act
var app = App.CreateBuilder().Build();
await app.InvokeAsync(new[]
{
"compose",
"-p",
packageFile,
"-s",
subgraphPackageFile
});

// assert
Assert.True(File.Exists(packageFile));

await using var package = FusionGraphPackage.Open(packageFile, FileAccess.Read);

var fusionGraph = await package.GetFusionGraphAsync();
var schema = await package.GetSchemaAsync();
var subgraphs = await package.GetSubgraphConfigurationsAsync();

var snapshot = new Snapshot();

snapshot.Add(schema, "Schema Document");
snapshot.Add(fusionGraph, "Fusion Graph Document");

foreach (var subgraph in subgraphs)
{
snapshot.Add(subgraph, $"{subgraph.Name} Subgraph Configuration");
}

snapshot.MatchSnapshot();
}

[Fact]
public async Task Compose_Fusion_Graph_Append_Subgraph()
{
// arrange
using var demoProject = await DemoProject.CreateAsync();
var accountConfig = demoProject.Accounts.ToConfiguration(AccountsExtensionSdl);
var account = CreateFiles(accountConfig);
var accountSubgraphPackageFile = CreateTempFile();

await PackageHelper.CreateSubgraphPackageAsync(
accountSubgraphPackageFile,
new SubgraphFiles(
account.SchemaFile,
account.TransportConfigFile,
account.ExtensionFiles));

var reviewConfig = demoProject.Reviews2.ToConfiguration(ReviewsExtensionSdl);
var review = CreateFiles(reviewConfig);
var reviewSubgraphPackageFile = CreateTempFile();

await PackageHelper.CreateSubgraphPackageAsync(
reviewSubgraphPackageFile,
new SubgraphFiles(
review.SchemaFile,
review.TransportConfigFile,
review.ExtensionFiles));

var packageFile = CreateTempFile();

var app = App.CreateBuilder().Build();
await app.InvokeAsync(new[]
{
"compose",
"-p",
packageFile,
"-s",
accountSubgraphPackageFile
});

// act
app = App.CreateBuilder().Build();
await app.InvokeAsync(new[]
{
"compose",
"-p",
packageFile,
"-s",
reviewSubgraphPackageFile
});

// assert
Assert.True(File.Exists(packageFile));

await using var package = FusionGraphPackage.Open(packageFile, FileAccess.Read);

var fusionGraph = await package.GetFusionGraphAsync();
var schema = await package.GetSchemaAsync();
var subgraphs = await package.GetSubgraphConfigurationsAsync();

var snapshot = new Snapshot();

snapshot.Add(schema, "Schema Document");
snapshot.Add(fusionGraph, "Fusion Graph Document");

foreach (var subgraph in subgraphs)
{
snapshot.Add(subgraph, $"{subgraph.Name} Subgraph Configuration");
}

snapshot.MatchSnapshot();
}

private Files CreateFiles(SubgraphConfiguration configuration)
{
var files = new Files(CreateTempFile(), CreateTempFile(), new[] { CreateTempFile() });
var configJson = PackageHelper.FormatSubgraphConfig(
new(configuration.Name, configuration.Clients));
File.WriteAllText(files.SchemaFile, configuration.Schema);
File.WriteAllText(files.TransportConfigFile, configJson);
File.WriteAllText(files.ExtensionFiles[0], configuration.Extensions[0]);
return files;
}

private string CreateTempFile()
{
var file = Path.GetTempFileName();
_files.Add(file);
return file;
}

public void Dispose()
{
while (_files.TryTake(out var file))
{
File.Delete(file);
}
}

public record Files(string SchemaFile, string TransportConfigFile, string[] ExtensionFiles);
}
158 changes: 0 additions & 158 deletions src/HotChocolate/Fusion/test/CommandLine.Tests/PackageHelperTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System.Collections.Concurrent;
using System.CommandLine.Parsing;
using CookieCrumble;
using HotChocolate.Fusion;
using HotChocolate.Fusion.CommandLine;
using HotChocolate.Fusion.CommandLine.Helpers;
using HotChocolate.Fusion.Composition;
using HotChocolate.Fusion.Shared;
Expand Down Expand Up @@ -67,158 +64,3 @@ public void Dispose()

public record Files(string SchemaFile, string TransportConfigFile, string[] ExtensionFiles);
}

public class ComposeHelperTests : IDisposable
{
private readonly ConcurrentBag<string> _files = new();

[Fact]
public async Task Compose_Fusion_Graph()
{
// arrange
using var demoProject = await DemoProject.CreateAsync();
var accountConfig = demoProject.Accounts.ToConfiguration(AccountsExtensionSdl);
var account = CreateFiles(accountConfig);
var subgraphPackageFile = CreateTempFile();

await CreateSubgraphPackageAsync(
subgraphPackageFile,
new SubgraphFiles(
account.SchemaFile,
account.TransportConfigFile,
account.ExtensionFiles));

var packageFile = CreateTempFile();

// act
var app = App.CreateBuilder().Build();
await app.InvokeAsync(new[]
{
"compose",
"-p",
packageFile,
"-s",
subgraphPackageFile
});

// assert
Assert.True(File.Exists(packageFile));

await using var package = FusionGraphPackage.Open(packageFile, FileAccess.Read);

var fusionGraph = await package.GetFusionGraphAsync();
var schema = await package.GetSchemaAsync();
var subgraphs = await package.GetSubgraphConfigurationsAsync();

var snapshot = new Snapshot();

snapshot.Add(schema, "Schema Document");
snapshot.Add(fusionGraph, "Fusion Graph Document");

foreach (var subgraph in subgraphs)
{
snapshot.Add(subgraph, $"{subgraph.Name} Subgraph Configuration");
}

snapshot.MatchSnapshot();
}

[Fact]
public async Task Compose_Fusion_Graph_Append_Subgraph()
{
// arrange
using var demoProject = await DemoProject.CreateAsync();
var accountConfig = demoProject.Accounts.ToConfiguration(AccountsExtensionSdl);
var account = CreateFiles(accountConfig);
var accountSubgraphPackageFile = CreateTempFile();

await CreateSubgraphPackageAsync(
accountSubgraphPackageFile,
new SubgraphFiles(
account.SchemaFile,
account.TransportConfigFile,
account.ExtensionFiles));

var reviewConfig = demoProject.Reviews2.ToConfiguration(ReviewsExtensionSdl);
var review = CreateFiles(reviewConfig);
var reviewSubgraphPackageFile = CreateTempFile();

await CreateSubgraphPackageAsync(
reviewSubgraphPackageFile,
new SubgraphFiles(
review.SchemaFile,
review.TransportConfigFile,
review.ExtensionFiles));

var packageFile = CreateTempFile();

var app = App.CreateBuilder().Build();
await app.InvokeAsync(new[]
{
"compose",
"-p",
packageFile,
"-s",
accountSubgraphPackageFile
});

// act
app = App.CreateBuilder().Build();
await app.InvokeAsync(new[]
{
"compose",
"-p",
packageFile,
"-s",
reviewSubgraphPackageFile
});

// assert
Assert.True(File.Exists(packageFile));

await using var package = FusionGraphPackage.Open(packageFile, FileAccess.Read);

var fusionGraph = await package.GetFusionGraphAsync();
var schema = await package.GetSchemaAsync();
var subgraphs = await package.GetSubgraphConfigurationsAsync();

var snapshot = new Snapshot();

snapshot.Add(schema, "Schema Document");
snapshot.Add(fusionGraph, "Fusion Graph Document");

foreach (var subgraph in subgraphs)
{
snapshot.Add(subgraph, $"{subgraph.Name} Subgraph Configuration");
}

snapshot.MatchSnapshot();
}

private Files CreateFiles(SubgraphConfiguration configuration)
{
var files = new Files(CreateTempFile(), CreateTempFile(), new[] { CreateTempFile() });
var configJson = FormatSubgraphConfig(new(configuration.Name, configuration.Clients));
File.WriteAllText(files.SchemaFile, configuration.Schema);
File.WriteAllText(files.TransportConfigFile, configJson);
File.WriteAllText(files.ExtensionFiles[0], configuration.Extensions[0]);
return files;
}

private string CreateTempFile()
{
var file = Path.GetTempFileName();
_files.Add(file);
return file;
}

public void Dispose()
{
while (_files.TryTake(out var file))
{
File.Delete(file);
}
}

public record Files(string SchemaFile, string TransportConfigFile, string[] ExtensionFiles);
}

0 comments on commit fb87acc

Please sign in to comment.