|
| 1 | +using System.Diagnostics; |
| 2 | +using System.Text.Json; |
| 3 | +using HotChocolate.Fusion.Composition.Settings; |
| 4 | +using HotChocolate.Language; |
| 5 | +using HotChocolate.Skimmed.Serialization; |
| 6 | + |
| 7 | +namespace HotChocolate.Fusion.Composition; |
| 8 | + |
| 9 | +public static class FusionGatewayConfigurationUtilities |
| 10 | +{ |
| 11 | + public static async Task ConfigureAsync( |
| 12 | + IReadOnlyList<GatewayInfo> gateways, |
| 13 | + CancellationToken cancellationToken = default) |
| 14 | + { |
| 15 | + ExportSubgraphSchemaDocs(gateways); |
| 16 | + await EnsureSubgraphHasConfigAsync(gateways, cancellationToken); |
| 17 | + await ComposeAsync(gateways, cancellationToken); |
| 18 | + } |
| 19 | + |
| 20 | + private static void ExportSubgraphSchemaDocs(IReadOnlyList<GatewayInfo> gateways) |
| 21 | + { |
| 22 | + var processed = new HashSet<string>(); |
| 23 | + |
| 24 | + foreach (var gateway in gateways) |
| 25 | + { |
| 26 | + foreach (var subgraph in gateway.Subgraphs) |
| 27 | + { |
| 28 | + if (!processed.Add(subgraph.Path)) |
| 29 | + { |
| 30 | + continue; |
| 31 | + } |
| 32 | + |
| 33 | + Console.WriteLine("Expoorting schema document for subgraph {0} ...", subgraph.Name); |
| 34 | + |
| 35 | + var workingDirectory = System.IO.Path.GetDirectoryName(subgraph.Path)!; |
| 36 | + |
| 37 | + var processStartInfo = new ProcessStartInfo |
| 38 | + { |
| 39 | + FileName = "dotnet", |
| 40 | + Arguments = "dotnet run --no-build --no-restore -- schema export --output schema.graphql", |
| 41 | + WorkingDirectory = workingDirectory, |
| 42 | + UseShellExecute = false, |
| 43 | + RedirectStandardOutput = true, |
| 44 | + RedirectStandardError = true, |
| 45 | + }; |
| 46 | + |
| 47 | + using (var process = Process.Start(processStartInfo)!) |
| 48 | + { |
| 49 | + var output = process.StandardOutput.ReadToEnd(); |
| 50 | + var errors = process.StandardError.ReadToEnd(); |
| 51 | + |
| 52 | + process.WaitForExit(); |
| 53 | + |
| 54 | + if (!string.IsNullOrEmpty(output)) |
| 55 | + { |
| 56 | + Console.WriteLine(output); |
| 57 | + } |
| 58 | + |
| 59 | + if (!string.IsNullOrEmpty(errors)) |
| 60 | + { |
| 61 | + Console.WriteLine(errors); |
| 62 | + } |
| 63 | + |
| 64 | + if (process.ExitCode != 0) |
| 65 | + { |
| 66 | + Console.WriteLine( |
| 67 | + "{0}(1,1): error HF1002: ; Failed to export schema document for subgraph {1} ...", |
| 68 | + subgraph.Path, |
| 69 | + subgraph.Name); |
| 70 | + Environment.Exit(-255); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private static async Task EnsureSubgraphHasConfigAsync( |
| 78 | + IReadOnlyList<GatewayInfo> gateways, |
| 79 | + CancellationToken ct) |
| 80 | + { |
| 81 | + foreach (var gateway in gateways) |
| 82 | + { |
| 83 | + foreach (var project in gateway.Subgraphs) |
| 84 | + { |
| 85 | + var projectRoot = System.IO.Path.GetDirectoryName(project.Path)!; |
| 86 | + var configFile = System.IO.Path.Combine(projectRoot, WellKnownFileNames.ConfigFile); |
| 87 | + |
| 88 | + if (File.Exists(configFile)) |
| 89 | + { |
| 90 | + continue; |
| 91 | + } |
| 92 | + |
| 93 | + var config = new SubgraphConfigurationDto(project.Name); |
| 94 | + var configJson = PackageHelper.FormatSubgraphConfig(config); |
| 95 | + await File.WriteAllTextAsync(configFile, configJson, ct); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private static async Task ComposeAsync( |
| 101 | + IReadOnlyList<GatewayInfo> gateways, |
| 102 | + CancellationToken ct) |
| 103 | + { |
| 104 | + foreach (var gateway in gateways) |
| 105 | + { |
| 106 | + await ComposeGatewayAsync(gateway.Path, gateway.Subgraphs.Select(t => t.Path), ct); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private static async Task ComposeGatewayAsync( |
| 111 | + string gatewayProject, |
| 112 | + IEnumerable<string> subgraphProjects, |
| 113 | + CancellationToken ct) |
| 114 | + { |
| 115 | + var gatewayDirectory = System.IO.Path.GetDirectoryName(gatewayProject)!; |
| 116 | + var packageFileName = System.IO.Path.Combine(gatewayDirectory, $"gateway{WellKnownFileExtensions.FusionPackage}"); |
| 117 | + var packageFile = new FileInfo(packageFileName); |
| 118 | + var settingsFileName = System.IO.Path.Combine(gatewayDirectory, "gateway-settings.json"); |
| 119 | + var settingsFile = new FileInfo(settingsFileName); |
| 120 | + var subgraphDirectories = subgraphProjects.Select(t => System.IO.Path.GetDirectoryName(t)!).ToArray(); |
| 121 | + |
| 122 | + // Ensure Gateway Project Directory Exists. |
| 123 | + if (!Directory.Exists(gatewayDirectory)) |
| 124 | + { |
| 125 | + Directory.CreateDirectory(gatewayDirectory); |
| 126 | + } |
| 127 | + |
| 128 | + await using var package = FusionGraphPackage.Open(packageFile.FullName); |
| 129 | + var subgraphConfigs = |
| 130 | + (await package.GetSubgraphConfigurationsAsync(ct)).ToDictionary(t => t.Name); |
| 131 | + await ResolveSubgraphPackagesAsync(subgraphDirectories, subgraphConfigs, ct); |
| 132 | + |
| 133 | + using var settingsJson = settingsFile.Exists |
| 134 | + ? JsonDocument.Parse(await File.ReadAllTextAsync(settingsFile.FullName, ct)) |
| 135 | + : await package.GetFusionGraphSettingsAsync(ct); |
| 136 | + var settings = settingsJson.Deserialize<PackageSettings>() ?? new PackageSettings(); |
| 137 | + |
| 138 | + var features = settings.CreateFeatures(); |
| 139 | + |
| 140 | + var composer = new FusionGraphComposer( |
| 141 | + settings.FusionTypePrefix, |
| 142 | + settings.FusionTypeSelf, |
| 143 | + () => new ConsoleLog()); |
| 144 | + |
| 145 | + var fusionGraph = await composer.TryComposeAsync(subgraphConfigs.Values, features, ct); |
| 146 | + |
| 147 | + if (fusionGraph is null) |
| 148 | + { |
| 149 | + Console.WriteLine("Fusion graph composition failed."); |
| 150 | + return; |
| 151 | + } |
| 152 | + |
| 153 | + var fusionGraphDoc = Utf8GraphQLParser.Parse(SchemaFormatter.FormatAsString(fusionGraph)); |
| 154 | + var typeNames = FusionTypeNames.From(fusionGraphDoc); |
| 155 | + var rewriter = new FusionGraphConfigurationToSchemaRewriter(); |
| 156 | + var schemaDoc = (DocumentNode)rewriter.Rewrite(fusionGraphDoc, typeNames)!; |
| 157 | + |
| 158 | + using var updateSettingsJson = JsonSerializer.SerializeToDocument( |
| 159 | + settings, |
| 160 | + new JsonSerializerOptions(JsonSerializerDefaults.Web)); |
| 161 | + |
| 162 | + await package.SetFusionGraphAsync(fusionGraphDoc, ct); |
| 163 | + await package.SetFusionGraphSettingsAsync(updateSettingsJson, ct); |
| 164 | + await package.SetSchemaAsync(schemaDoc, ct); |
| 165 | + |
| 166 | + foreach (var config in subgraphConfigs.Values) |
| 167 | + { |
| 168 | + await package.SetSubgraphConfigurationAsync(config, ct); |
| 169 | + } |
| 170 | + |
| 171 | + Console.WriteLine("Fusion graph composed."); |
| 172 | + } |
| 173 | + |
| 174 | + private static async Task ResolveSubgraphPackagesAsync( |
| 175 | + IReadOnlyList<string> subgraphDirectories, |
| 176 | + IDictionary<string, SubgraphConfiguration> subgraphConfigs, |
| 177 | + CancellationToken cancellationToken) |
| 178 | + { |
| 179 | + for (var i = 0; i < subgraphDirectories.Count; i++) |
| 180 | + { |
| 181 | + var path = subgraphDirectories[i]; |
| 182 | + |
| 183 | + if (!Directory.Exists(path)) |
| 184 | + { |
| 185 | + continue; |
| 186 | + } |
| 187 | + |
| 188 | + var configFile = System.IO.Path.Combine(path, WellKnownFileNames.ConfigFile); |
| 189 | + var schemaFile = System.IO.Path.Combine(path, WellKnownFileNames.SchemaFile); |
| 190 | + var extensionFile = System.IO.Path.Combine(path, WellKnownFileNames.ExtensionFile); |
| 191 | + |
| 192 | + if (!File.Exists(configFile) || !File.Exists(schemaFile)) |
| 193 | + { |
| 194 | + continue; |
| 195 | + } |
| 196 | + |
| 197 | + var conf = await PackageHelper.LoadSubgraphConfigAsync(configFile, cancellationToken); |
| 198 | + var schema = await File.ReadAllTextAsync(schemaFile, cancellationToken); |
| 199 | + var extensions = Array.Empty<string>(); |
| 200 | + |
| 201 | + if (File.Exists(extensionFile)) |
| 202 | + { |
| 203 | + extensions = [await File.ReadAllTextAsync(extensionFile, cancellationToken),]; |
| 204 | + } |
| 205 | + |
| 206 | + subgraphConfigs[conf.Name] = |
| 207 | + new SubgraphConfiguration( |
| 208 | + conf.Name, |
| 209 | + schema, |
| 210 | + extensions, |
| 211 | + conf.Clients, |
| 212 | + conf.Extensions); |
| 213 | + } |
| 214 | + } |
| 215 | +} |
0 commit comments