-
Notifications
You must be signed in to change notification settings - Fork 332
Expand file tree
/
Copy pathExporter.cs
More file actions
255 lines (225 loc) · 11.3 KB
/
Exporter.cs
File metadata and controls
255 lines (225 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.IO.Abstractions;
using System.Runtime.CompilerServices;
using Azure.DataApiBuilder.Config;
using Azure.DataApiBuilder.Config.ObjectModel;
using Azure.DataApiBuilder.Core.Generator;
using Cli.Commands;
using HotChocolate.Utilities.Introspection;
using Microsoft.Extensions.Logging;
using static Cli.Utils;
// This assembly is used to create dynamic proxy objects at runtime for the purpose of mocking dependencies in the tests.
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]
namespace Cli
{
/// <summary>
/// Provides functionality for exporting GraphQL schemas, either by generating from an Azure Cosmos DB database or fetching from a GraphQL API.
/// </summary>
internal class Exporter
{
private const int COSMOS_DB_RETRY_COUNT = 1;
private const int DAB_SERVICE_RETRY_COUNT = 5;
private static readonly CancellationTokenSource _cancellationTokenSource = new();
private static readonly CancellationToken _cancellationToken = _cancellationTokenSource.Token;
/// <summary>
/// Exports the GraphQL schema to a file based on the provided options.
/// </summary>
/// <param name="options">The options for exporting, including output directory, schema file name, and other settings.</param>
/// <param name="logger">The logger instance for logging information and errors.</param>
/// <param name="loader">The loader for runtime configuration files.</param>
/// <param name="fileSystem">The file system abstraction for handling file operations.</param>
/// <returns>Returns 0 if the export is successful, otherwise returns -1.</returns>
public static bool Export(ExportOptions options, ILogger logger, FileSystemRuntimeConfigLoader loader, IFileSystem fileSystem)
{
// Attempt to locate the runtime configuration file based on CLI options
if (!TryGetConfigFileBasedOnCliPrecedence(loader: loader, userProvidedConfigFile: options.Config, runtimeConfigFile: out string runtimeConfigFile))
{
logger.LogError("Failed to find the config file provided, check your options and try again.");
return false;
}
// Load the runtime configuration from the file
DeserializationVariableReplacementSettings replacementSettings = new(azureKeyVaultOptions: null, doReplaceEnvVar: true, doReplaceAkvVar: true);
if (!loader.TryLoadConfig(runtimeConfigFile, out RuntimeConfig? runtimeConfig, replacementSettings: replacementSettings))
{
logger.LogError("Failed to read the config file: {0}.", runtimeConfigFile);
return false;
}
// Do not retry if schema generation logic is running
int retryCount = options.Generate ? COSMOS_DB_RETRY_COUNT : DAB_SERVICE_RETRY_COUNT;
bool isSuccess = false;
if (options.GraphQL)
{
int tries = 0;
while (tries < retryCount)
{
try
{
ExportGraphQL(options, runtimeConfig, fileSystem, loader, logger).Wait();
isSuccess = true;
break;
}
catch
{
tries++;
}
}
if (tries == retryCount)
{
logger.LogError("Failed to export GraphQL schema.");
}
}
else
{
logger.LogError("Exporting GraphQL schema is not enabled. You need to pass --graphql.");
}
_cancellationTokenSource.Cancel();
return isSuccess;
}
/// <summary>
/// Exports the GraphQL schema either by generating it from an Azure Cosmos DB database or fetching it from a GraphQL API.
/// </summary>
/// <param name="options">The options for exporting, including sampling mode and schema file name.</param>
/// <param name="runtimeConfig">The runtime configuration for the export process.</param>
/// <param name="fileSystem">The file system abstraction for handling file operations.</param>
/// <param name="loader">The loader for runtime configuration files.</param>
/// <param name="logger">The logger instance for logging information and errors.</param>
/// <returns>A task representing the asynchronous operation.</returns>
private static async Task ExportGraphQL(
ExportOptions options,
RuntimeConfig runtimeConfig,
IFileSystem fileSystem,
FileSystemRuntimeConfigLoader loader,
ILogger logger)
{
string schemaText;
if (options.Generate)
{
schemaText = await ExportGraphQLFromCosmosDB(options, runtimeConfig, logger);
}
else
{
StartOptions startOptions = new(
verbose: false,
logLevel: LogLevel.None,
isHttpsRedirectionDisabled: false,
config: options.Config!,
mcpStdio: false,
mcpRole: null);
Task dabService = Task.Run(() =>
{
_ = ConfigGenerator.TryStartEngineWithOptions(startOptions, loader, fileSystem);
}, _cancellationToken);
Exporter exporter = new();
schemaText = exporter.ExportGraphQLFromDabService(runtimeConfig, logger);
}
if (string.IsNullOrEmpty(schemaText))
{
logger.LogError("Generated GraphQL schema is empty. Please ensure data is available to generate the schema.");
return;
}
// Write the schema content to a file
WriteSchemaFile(options, fileSystem, schemaText, logger);
logger.LogInformation("Schema file exported successfully at {0}", options.OutputDirectory);
}
/// <summary>
/// Fetches the GraphQL schema from the DAB service, attempting to use the HTTPS endpoint first.
/// If the HTTPS endpoint fails, it falls back to using the HTTP endpoint.
/// Logs the process of fetching the schema and any fallback actions taken.
/// </summary>
/// <param name="runtimeConfig">The runtime config object containing the GraphQL path.</param>
/// <param name="logger">The logger instance used to log information and errors during the schema fetching process.</param>
/// <returns>The GraphQL schema as a string.</returns>
internal string ExportGraphQLFromDabService(RuntimeConfig runtimeConfig, ILogger logger)
{
string schemaText;
// Fetch the schema from the GraphQL API
logger.LogInformation("Fetching schema from GraphQL API.");
try
{
logger.LogInformation("Trying to fetch schema from DAB Service using HTTPS endpoint.");
schemaText = GetGraphQLSchema(runtimeConfig, useFallbackUrl: false);
}
catch
{
logger.LogInformation("Failed to fetch schema from DAB Service using HTTPS endpoint. Trying with HTTP endpoint.");
schemaText = GetGraphQLSchema(runtimeConfig, useFallbackUrl: true);
}
return schemaText;
}
/// <summary>
/// Retrieves the GraphQL schema from the DAB service using either the HTTPS or HTTP endpoint based on the specified fallback option.
/// </summary>
/// <param name="runtimeConfig">The runtime configuration containing the GraphQL path and other settings.</param>
/// <param name="useFallbackUrl">A boolean flag indicating whether to use the fallback HTTP endpoint. If false, the method attempts to use the HTTPS endpoint.</param>
internal virtual string GetGraphQLSchema(RuntimeConfig runtimeConfig, bool useFallbackUrl = false)
{
HttpClient client = CreateIntrospectionClient(runtimeConfig.GraphQLPath, useFallbackUrl);
Task<HotChocolate.Language.DocumentNode> response = IntrospectionClient.IntrospectServerAsync(client);
response.Wait();
HotChocolate.Language.DocumentNode node = response.Result;
return node.ToString();
}
private static HttpClient CreateIntrospectionClient(string path, bool useFallbackUrl)
{
if (useFallbackUrl)
{
return new HttpClient { BaseAddress = new Uri($"http://localhost:5000{path}") };
}
// CodeQL[SM02185] Loading internal server connection
return new HttpClient(
new HttpClientHandler
{
ServerCertificateCustomValidationCallback =
HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
})
{
BaseAddress = new Uri($"https://localhost:5001{path}")
};
}
private static async Task<string> ExportGraphQLFromCosmosDB(ExportOptions options, RuntimeConfig runtimeConfig, ILogger logger)
{
// Generate the schema from Azure Cosmos DB database
logger.LogInformation("Generating schema from the Azure Cosmos DB database using {0}", options.SamplingMode);
try
{
return await SchemaGeneratorFactory.Create(runtimeConfig,
options.SamplingMode,
options.NumberOfRecords,
options.PartitionKeyPath,
options.MaxDays,
options.GroupCount,
logger);
}
catch (Exception e)
{
logger.LogError("Failed to generate schema from Azure Cosmos DB database: {0}", e.Message);
logger.LogDebug(e.StackTrace);
return string.Empty;
}
}
/// <summary>
/// Writes the generated schema to a file in the specified output directory.
/// </summary>
/// <param name="options">The options containing the output directory and schema file name.</param>
/// <param name="fileSystem">The file system abstraction for handling file operations.</param>
/// <param name="content">The schema content to be written to the file.</param>
/// <param name="logger">The logger instance for logging information and errors.</param>
private static void WriteSchemaFile(ExportOptions options, IFileSystem fileSystem, string content, ILogger logger)
{
if (string.IsNullOrEmpty(content))
{
logger.LogError("There is nothing to write");
return;
}
// Ensure the output directory exists
if (!fileSystem.Directory.Exists(options.OutputDirectory))
{
fileSystem.Directory.CreateDirectory(options.OutputDirectory);
}
// Construct the path for the schema file and write the content to it
string outputPath = fileSystem.Path.Combine(options.OutputDirectory, options.GraphQLSchemaFile);
fileSystem.File.WriteAllText(outputPath, content);
}
}
}