forked from aws/amazon-neptune-for-graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
614 lines (551 loc) · 24.1 KB
/
main.js
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
/*
Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License.
A copy of the License is located at
http://www.apache.org/licenses/LICENSE-2.0
or in the "license" file accompanying this file. This file is distributed
on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express or implied. See the License for the specific language governing
permissions and limitations under the License.
*/
import { readFileSync, writeFileSync, mkdirSync} from 'fs';
import { helpTxt } from './help.js';
import { graphDBInferenceSchema } from './graphdb.js';
import { changeGraphQLSchema } from './changes.js';
import { schemaParser, schemaStringify } from './schemaParser.js';
import { validatedSchemaModel} from './schemaModelValidator.js';
import { resolverJS } from './resolverJS.js';
import { getNeptuneSchema, setGetNeptuneSchemaParameters } from './NeptuneSchema.js';
import { createUpdateAWSpipeline, removeAWSpipelineResources } from './pipelineResources.js'
import { createAWSpipelineCDK } from './CDKPipelineApp.js'
import { createLambdaDeploymentPackage } from './lambdaZip.js'
import ora from 'ora';
let spinner = null;
function yellow(text) {
return '\x1b[33m' + text + '\x1b[0m';
}
// find global installation dir
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// get version
const version = JSON.parse(readFileSync(__dirname + '/../package.json')).version;
// Input
let quiet = false;
let inputGraphQLSchema = '';
let inputGraphQLSchemaFile = '';
let inputGraphQLSchemaChanges = '';
let inputGraphQLSchemaChangesFile = '';
let inputGraphDBSchema = '';
let inputGraphDBSchemaFile = '';
let inputGraphDBSchemaNeptuneEndpoint = '';
let queryLanguage = 'opencypher'; // or TODO 'gremlin' or 'sparql'
let queryClient = 'sdk'; // or 'http'
let isNeptuneIAMAuth = false;
let createUpdatePipeline = false;
let createUpdatePipelineName = '';
let createUpdatePipelineEndpoint = '';
let createUpdatePipelineRegion = 'us-east-1';
let createUpdatePipelineNeptuneDatabaseName = '';
let removePipelineName = '';
let inputCDKpipeline = false;
let inputCDKpipelineName = '';
let inputCDKpipelineEnpoint = '';
let inputCDKpipelineFile = '';
let inputCDKpipelineRegion = '';
let inputCDKpipelineDatabaseName = '';
let createLambdaZip = true;
let outputFolderPath = './output';
// Outputs
let outputSchema = '';
let outputSchemaFile = '';
let outputSourceSchema = '';
let outputSourceSchemaFile = '';
let outputSchemaMutations = true;
let outputLambdaResolver = '';
let outputLambdaResolverFile = '';
let outputJSResolver = '';
let outputJSResolverFile = '';
let outputNeptuneSchemaFile = '';
let outputLambdaResolverZipName = '';
let outputLambdaResolverZipFile = '';
let outputLambdaPackagePath = '';
// Schema model
let schemaModel = {};
// Parse input arguments
function processArgs() {
process.argv.forEach(function (val, index, array) {
switch(val) {
case '--help':
case '--h':
case '-help':
case '-h':
console.log(helpTxt);
process.exit(0);
break;
case '--version':
case '--v':
case '-version':
case '-v':
console.log(version);
process.exit(0);
break;
case '-q':
case '--quiet':
quiet = true;
break;
case '-is':
case '--input-schema':
inputGraphQLSchema = array[index + 1];
break;
case '-isf':
case '--input-schema-file':
inputGraphQLSchemaFile = array[index + 1];
break;
case '-ig':
case '--input-graphdb-schema':
inputGraphDBSchema = array[index + 1];
break;
case '-igf':
case '--input-graphdb-schema-file':
inputGraphDBSchemaFile = array[index + 1];
break;
case '-isc':
case '--input-schema-changes-file':
inputGraphQLSchemaChangesFile = array[index + 1];
break;
case '-ie':
case '--input-graphdb-schema-neptune-endpoint':
inputGraphDBSchemaNeptuneEndpoint = array[index + 1];
break;
case '-os':
case '--output-schema-file':
outputSchemaFile = array[index + 1];
break;
case '-oss':
case '--output-source-schema-file':
outputSourceSchemaFile = array[index + 1];
break;
case '-onm':
case '--output-schema-no-mutations':
outputSchemaMutations = false;
break;
case '-olr':
case '--output-lambda-resolver-file':
outputLambdaResolverFile = array[index + 1];
break;
case '-or':
case '--output-js-resolver-file':
outputJSResolverFile = array[index + 1];
break;
case '-og':
case '--output-neptune-schema-file':
outputNeptuneSchemaFile = array[index + 1];
break;
case '-org':
case '--output-resolver-query-gremlin':
queryLanguage = 'gremlin';
break;
case '-oro':
case '--output-resolver-query-opencypher':
queryLanguage = 'opencypher';
break;
case '-orc':
case '--output-resolver-query-client':
queryClient = 'client';
break;
case '-orh':
case '--output-resolver-query-https':
queryClient = 'http';
break;
case '-ors':
case '--output-resolver-query-sdk':
queryClient = 'sdk';
break;
case '-p':
case '--create-update-aws-pipeline':
createUpdatePipeline = true;
break;
case '-pn':
case '--create-update-aws-pipeline-name':
createUpdatePipelineName = array[index + 1];
break;
case '-pr':
case '--create-update-aws-pipeline-region':
createUpdatePipelineRegion = array[index + 1];
break;
case '-pe':
case '--create-update-aws-pipeline-neptune-endpoint':
createUpdatePipelineEndpoint = array[index + 1];
break;
case '-pd':
case '--create-update-aws-pipeline-neptune-database-name':
createUpdatePipelineNeptuneDatabaseName = array[index + 1];
break;
case '-rp':
case '--remove-aws-pipeline-name':
removePipelineName = array[index + 1];
break;
case '-c':
case '--output-aws-pipeline-cdk':
inputCDKpipeline = true;
break;
case '-ce':
case '--output-aws-pipeline-cdk-neptune-endpoint':
inputCDKpipelineEnpoint = array[index + 1];
break;
case '-cd':
case '--output-aws-pipeline-cdk-neptune-database-name':
inputCDKpipelineDatabaseName = array[index + 1];
break;
case '-cn':
case '--output-aws-pipeline-cdk-name':
inputCDKpipelineName = array[index + 1];
break;
case '-cr':
case '--output-aws-pipeline-cdk-region':
inputCDKpipelineRegion = array[index + 1];
break;
case '-cf':
case '--output-aws-pipeline-cdk-file':
inputCDKpipelineFile = array[index + 1];
break;
case '-oln':
case '--output-lambda-resolver-zip-name':
outputLambdaResolverZipName = array[index + 1];
break;
case '-olf':
case '--output-lambda-resolver-zip-file':
outputLambdaResolverZipFile = array[index + 1];
break;
case '-pi':
case '--create-update-aws-pipeline-neptune-IAM':
isNeptuneIAMAuth = true;
break;
case '-ci':
case '--output-aws-pipeline-cdk-neptune-IAM':
isNeptuneIAMAuth = true;
break;
case '-onl':
case '--output-no-lambda-zip':
createLambdaZip = false;
break;
case '-o':
case '--output-folder-path':
outputFolderPath = array[index + 1];
break;
}
});
}
async function main() {
if (process.argv.length <= 2) {
console.log("--h for help.\n");
process.exit(0);
}
processArgs();
// Get graphDB schema from file
if (inputGraphDBSchemaFile != '' && inputGraphQLSchema == '' && inputGraphQLSchemaFile == '') {
try {
inputGraphDBSchema = readFileSync(inputGraphDBSchemaFile, 'utf8');
if (!quiet) console.log('Loaded graphDB schema from file: ' + inputGraphDBSchemaFile);
} catch (err) {
console.error('Error reading graphDB schema file: ' + inputGraphDBSchemaFile);
process.exit(1);
}
}
// Get Neptune schema from endpoint
if (inputGraphDBSchemaNeptuneEndpoint != '' && inputGraphDBSchema == '' && inputGraphDBSchemaFile == '') {
let endpointParts = inputGraphDBSchemaNeptuneEndpoint.split(':');
if (endpointParts.length < 2) {
console.error('Neptune endpoint must be in the form of host:port');
process.exit(1);
}
let neptuneHost = endpointParts[0];
let neptunePort = endpointParts[1];
let neptuneRegionParts = inputGraphDBSchemaNeptuneEndpoint.split('.');
let neptuneRegion = neptuneRegionParts[2];
if (!quiet) console.log('Getting Neptune schema from endpoint: ' + yellow(neptuneHost + ':' + neptunePort));
setGetNeptuneSchemaParameters(neptuneHost, neptunePort, neptuneRegion, true);
let startTime = performance.now();
inputGraphDBSchema = await getNeptuneSchema(quiet);
let endTime = performance.now();
let executionTime = endTime - startTime;
if (!quiet) console.log(`Execution time: ${(executionTime/1000).toFixed(2)} seconds`);
if (!quiet) console.log('');
}
// Option 2: inference GraphQL schema from graphDB schema
if (inputGraphDBSchema != '' && inputGraphQLSchema == '' && inputGraphQLSchemaFile == '') {
if (!quiet) console.log('Inferencing GraphQL schema from graphDB schema');
inputGraphQLSchema = graphDBInferenceSchema(inputGraphDBSchema, outputSchemaMutations);
if (!quiet) console.log('');
}
// Option 1: load
if (inputGraphQLSchema == '' && inputGraphQLSchemaFile != '') {
try {
inputGraphQLSchema = readFileSync(inputGraphQLSchemaFile, 'utf8');
if (!quiet) console.log('Loaded GraphQL schema from file: ' + inputGraphQLSchemaFile);
} catch (err) {
console.error('Error reading GraphQL schema file: ' + inputGraphQLSchemaFile);
process.exit(1);
}
}
// Changes
if (inputGraphQLSchemaChangesFile != '') {
try {
inputGraphQLSchemaChanges = readFileSync(inputGraphQLSchemaChangesFile, 'utf8');
if (!quiet) console.log('Loaded GraphQL schema changes from file: ' + inputGraphQLSchemaChangesFile);
} catch (err) {
console.error('Error reading GraphQL schema changes file: ' + inputGraphQLSchemaChangesFile);
process.exit(1);
}
}
// AWS pipeline
if (createUpdatePipeline) {
if (!inputGraphDBSchemaNeptuneEndpoint == '' && createUpdatePipelineEndpoint == '') {
createUpdatePipelineEndpoint = inputGraphDBSchemaNeptuneEndpoint;
}
if (createUpdatePipelineEndpoint == '' &&
createUpdatePipelineRegion == '' && createUpdatePipelineNeptuneDatabaseName == '') {
console.error('AWS pipeline: is required a Neptune endpoint, or a Neptune database name and region.');
process.exit(1);
}
if (createUpdatePipelineEndpoint == '' &&
!createUpdatePipelineRegion == '' && createUpdatePipelineNeptuneDatabaseName == '') {
console.error('AWS pipeline: a Neptune database name is required.');
process.exit(1);
}
if (createUpdatePipelineEndpoint == '' &&
createUpdatePipelineRegion == '' && !createUpdatePipelineNeptuneDatabaseName == '') {
console.error('AWS pipeline: a Neptune database region is required.');
process.exit(1);
}
if (createUpdatePipelineEndpoint != '') {
let parts = createUpdatePipelineEndpoint.split('.');
createUpdatePipelineNeptuneDatabaseName = parts[0];
createUpdatePipelineRegion = parts[2];
}
if (createUpdatePipelineName == '') {
createUpdatePipelineName = createUpdatePipelineNeptuneDatabaseName;
}
}
// CDK
if (inputCDKpipeline) {
if (!inputGraphDBSchemaNeptuneEndpoint == '') {
inputCDKpipelineEnpoint = inputGraphDBSchemaNeptuneEndpoint;
}
if (inputCDKpipelineEnpoint == '' &&
inputCDKpipelineRegion == '' && inputCDKpipelineDatabaseName == '') {
console.error('AWS CDK: is required a Neptune endpoint, or a Neptune database name and region.');
process.exit(1);
}
if (inputCDKpipelineEnpoint == '' &&
!inputCDKpipelineRegion == '' && inputCDKpipelineDatabaseName == '') {
console.error('AWS CDK: a Neptune database name is required.');
process.exit(1);
}
if (inputCDKpipelineEnpoint == '' &&
inputCDKpipelineRegion == '' && !inputCDKpipelineDatabaseName == '') {
console.error('AWS CDK: a Neptune database region is required.');
process.exit(1);
}
if (inputCDKpipelineEnpoint != '') {
let parts = inputCDKpipelineEnpoint.split('.');
inputCDKpipelineDatabaseName = parts[0];
inputCDKpipelineRegion = parts[2];
}
if (inputCDKpipelineName == '') {
inputCDKpipelineName = inputCDKpipelineDatabaseName;
}
}
// Apply changes
if (inputGraphQLSchemaChanges != '') {
inputGraphQLSchema = changeGraphQLSchema(inputGraphQLSchema, inputGraphQLSchemaChanges);
}
if (inputGraphQLSchema != '') {
// Parse schema
schemaModel = schemaParser(inputGraphQLSchema);
// Validate schema
schemaModel = validatedSchemaModel(schemaModel, quiet);
// Generate AWS Lambda resolver for AppSync and Amazon Neptune
outputLambdaResolver = resolverJS(schemaModel, queryLanguage, queryClient, __dirname);
// Generate generic Javascript resolver
outputJSResolver = resolverJS(schemaModel, queryLanguage, queryClient, __dirname);
}
// ****************************************************************************
// Outputs
// ****************************************************************************
mkdirSync(outputFolderPath, { recursive: true });
// Output GraphQL schema no directives
if (inputGraphQLSchema != '') {
outputSchema = schemaStringify(schemaModel, false);
if ( outputSchemaFile == '' ) {
if (createUpdatePipelineName == '') {
outputSchemaFile = outputFolderPath + '/output.schema.graphql';
} else {
outputSchemaFile = `${outputFolderPath}/${createUpdatePipelineName}.schema.graphql`;
}
}
try {
writeFileSync(outputSchemaFile, outputSchema);
if (!quiet) console.log('Wrote GraphQL schema to file: ' + yellow(outputSchemaFile));
} catch (err) {
console.error('Error writing GraphQL schema to file: ' + outputSchemaFile);
}
// Output GraphQL schema with directives
outputSourceSchema = schemaStringify(schemaModel, true);
if ( outputSourceSchemaFile == '' ) {
if (createUpdatePipelineName == '') {
outputSourceSchemaFile = outputFolderPath + '/output.source.schema.graphql';
} else {
outputSourceSchemaFile = `${outputFolderPath}/${createUpdatePipelineName}.source.schema.graphql`;
}
}
try {
writeFileSync(outputSourceSchemaFile, outputSourceSchema);
if (!quiet) console.log('Wrote GraphQL schema to file: ' + yellow(outputSourceSchemaFile));
} catch (err) {
console.error('Error writing GraphQL schema to file: ' + outputSourceSchemaFile);
}
// Output Neptune schema
if (outputNeptuneSchemaFile == '') {
if (createUpdatePipelineName == '') {
outputNeptuneSchemaFile = outputFolderPath + '/output.neptune.schema.json';
} else {
outputNeptuneSchemaFile = `${outputFolderPath}/${createUpdatePipelineName}.neptune.schema.json`;
}
}
try {
writeFileSync(outputNeptuneSchemaFile, inputGraphDBSchema);
if (!quiet) console.log('Wrote Neptune schema to file: ' + yellow(outputNeptuneSchemaFile));
} catch (err) {
console.error('Error writing Neptune schema to file: ' + outputNeptuneSchemaFile);
}
// Output Lambda resolver
if (outputLambdaResolverFile == '') {
outputLambdaResolverFile = outputFolderPath + '/output.resolver.graphql.js';
}
try {
writeFileSync(outputLambdaResolverFile, outputLambdaResolver);
if (!quiet) console.log('Wrote Lambda resolver to file: ' + yellow(outputLambdaResolverFile));
} catch (err) {
console.error('Error writing Lambda resolver to file: ' + outputLambdaResolverFile);
}
// Output Javascript resolver
if (outputJSResolverFile == '') {
if (createUpdatePipelineName == '') {
outputJSResolverFile = outputFolderPath + '/output.resolver.graphql.js';
} else {
outputJSResolverFile = `${outputFolderPath}/${createUpdatePipelineName}.resolver.graphql.js`;
}
}
try {
writeFileSync(outputJSResolverFile, outputJSResolver);
//writeFileSync('./test/output.resolver.graphql.js', outputJSResolver); // Remove, for development and test only
if (!quiet) console.log('Wrote Javascript resolver to file: ' + yellow(outputJSResolverFile));
} catch (err) {
console.error('Error writing Javascript resolver to file: ' + outputJSResolverFile);
}
// Output Lambda Zip file
switch (queryClient) {
case 'http':
outputLambdaPackagePath = '/../templates/Lambda4AppSyncHTTP';
break;
case 'sdk':
outputLambdaPackagePath = '/../templates/Lambda4AppSyncSDK';
break;
}
if ( !(createUpdatePipeline || inputCDKpipeline) && createLambdaZip) {
if (outputLambdaResolverZipFile == '' && outputLambdaResolverZipName == '')
outputLambdaResolverZipFile = outputFolderPath + '/output.lambda.zip';
if (outputLambdaResolverZipFile == '' && outputLambdaResolverZipName != '')
outputLambdaResolverZipFile = outputFolderPath + '/' + outputLambdaResolverZipName + '.zip';
try {
if (!quiet) spinner = ora('Creating Lambda ZIP ...').start();
await createLambdaDeploymentPackage(__dirname + outputLambdaPackagePath, outputLambdaResolverZipFile);
if (!quiet) {
spinner.stop();
console.log('Wrote Lambda ZIP file: ' + yellow(outputLambdaResolverZipFile));
}
} catch (err) {
console.error('Error creating Lambda ZIP file: ' + err);
}
}
// Create Update AWS Pipeline
if (createUpdatePipeline) {
try {
let endpointParts = createUpdatePipelineEndpoint.split(':');
if (endpointParts.length < 2) {
console.error('Neptune endpoint must be in the form of host:port');
process.exit(1);
}
let neptuneHost = endpointParts[0];
let neptunePort = endpointParts[1];
if (!quiet) console.log('\nCreating AWS pipeline resources')
await createUpdateAWSpipeline( createUpdatePipelineName,
createUpdatePipelineNeptuneDatabaseName,
createUpdatePipelineRegion,
outputSchema,
schemaModel,
__dirname + outputLambdaPackagePath,
outputSchemaMutations,
quiet,
__dirname,
isNeptuneIAMAuth,
neptuneHost,
neptunePort,
outputFolderPath );
} catch (err) {
console.error('Error creating AWS pipeline: ' + err);
}
}
// Output CDK
if (inputCDKpipeline) {
try {
if (!quiet) console.log('\nCreating CDK File')
let endpointParts = inputCDKpipelineEnpoint.split(':');
if (endpointParts.length < 2) {
console.error('Neptune endpoint must be in the form of host:port');
process.exit(1);
}
let neptuneHost = endpointParts[0];
let neptunePort = endpointParts[1];
if (inputCDKpipelineFile == '') {
inputCDKpipelineFile = `${outputFolderPath}/${inputCDKpipelineName}-cdk.js`;
}
await createAWSpipelineCDK( inputCDKpipelineName,
inputCDKpipelineDatabaseName,
inputCDKpipelineRegion,
outputSchema,
schemaModel,
__dirname + outputLambdaPackagePath,
inputCDKpipelineFile,
__dirname,
quiet,
isNeptuneIAMAuth,
neptuneHost,
neptunePort,
outputFolderPath );
} catch (err) {
console.error('Error creating CDK File: ' + err);
}
}
if (!quiet) console.log('\nDone\n');
}
// Remove AWS Pipeline
if ( removePipelineName != '') {
if (!quiet) console.log('\nRemoving pipeline AWS resources, name: ' + yellow(removePipelineName))
let resourcesToRemove = null;
let resourcesFile = `${outputFolderPath}/${removePipelineName}-resources.json`;
if (!quiet) console.log('Using file: ' + yellow(resourcesFile));
try {
resourcesToRemove = readFileSync(resourcesFile, 'utf8');
} catch (err) {
console.error('Error reading AWS pipeline resources file: ' + resourcesFile + ' ' + err);
process.exit(1);
}
await removeAWSpipelineResources(JSON.parse(resourcesToRemove), quiet);
}
}
export { main };