-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.ts
440 lines (389 loc) · 12.3 KB
/
index.ts
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
import { Construct } from 'constructs';
import {
AppsyncFunction,
Code,
FunctionRuntime,
GraphqlApi,
BaseDataSource,
Resolver,
SchemaFile,
GraphqlApiBase,
DataSourceOptions,
ExtendedResolverProps,
HttpDataSourceOptions,
GraphqlApiProps,
CfnDataSource,
CfnFunctionConfiguration,
} from 'aws-cdk-lib/aws-appsync';
import { IDomain as IOpenSearchDomain } from 'aws-cdk-lib/aws-opensearchservice';
import * as fs from 'node:fs';
import { GraphQLObjectType, isObjectType } from 'graphql';
import { makeExecutableSchema } from '@graphql-tools/schema';
import * as esbuild from 'esbuild';
import { readFileSync } from 'node:fs';
import { CfnResource } from 'aws-cdk-lib';
import { ITable } from 'aws-cdk-lib/aws-dynamodb';
import { IEventBus } from 'aws-cdk-lib/aws-events';
import { IFunction } from 'aws-cdk-lib/aws-lambda';
import { IServerlessCluster } from 'aws-cdk-lib/aws-rds';
import { ISecret } from 'aws-cdk-lib/aws-secretsmanager';
import path = require('node:path');
const SCHEMA_DEFINITIONS = `
scalar AWSTime
scalar AWSDateTime
scalar AWSTimestamp
scalar AWSEmail
scalar AWSJSON
scalar AWSURL
scalar AWSPhone
scalar AWSIPAddress
scalar BigInt
scalar Double
directive @aws_subscribe(mutations: [String!]!) on FIELD_DEFINITION
# Allows transformer libraries to deprecate directive arguments.
directive @deprecated(reason: String!) on INPUT_FIELD_DEFINITION | ENUM
directive @aws_auth(cognito_groups: [String!]!) on FIELD_DEFINITION
directive @aws_api_key on FIELD_DEFINITION | OBJECT
directive @aws_iam on FIELD_DEFINITION | OBJECT
directive @aws_oidc on FIELD_DEFINITION | OBJECT
directive @aws_cognito_user_pools(
cognito_groups: [String!]
) on FIELD_DEFINITION | OBJECT
`;
const DEF_RESOLVER_CODE = `
export function request(){ return {} }
export function response(ctx){ return ctx.prev.result}
`.trim();
const TS_CONFIG =
`{ "compilerOptions": { "target": "es2021", "module": "Node16", "noEmit": true, "moduleResolution": "node" } }`.trim();
type BaseResolver = {
key: string;
kind: 'UNIT' | 'PIPELINE';
typeName: string;
fieldName: string;
};
type UnitResolverFile = BaseResolver & {
kind: 'UNIT';
dsName: string;
};
type PipelineResolverGroup = BaseResolver & {
kind: 'PIPELINE';
hasCode?: boolean;
fns: AppSyncFn[];
};
type AppSyncFn = {
name: string;
description?: string;
key: string;
dsName: string;
order: number;
};
export interface AppSyncHelperProps extends Omit<GraphqlApiProps, 'schema' | 'name'> {
name?: string;
basedir: string;
}
export class AppSyncHelper extends GraphqlApiBase {
private basedir: string;
private bindCalled = false;
public readonly api: GraphqlApi;
/**
* a map of datasource names to datasources.
*/
private readonly datasources: Record<string, BaseDataSource> = {};
/**
* a map of resolver names to resolvers.
*/
public readonly resolvers: Record<string, Resolver> = {};
/**
* a map of resolver names to a sparse array of functions.
*/
public readonly functions: Record<string, (AppsyncFunction | undefined)[]> = {};
constructor(scope: Construct, id: string, props: AppSyncHelperProps) {
super(scope, id);
const { basedir, name: propsName, ...rest } = props;
const name = propsName ?? id;
this.basedir = basedir;
const apiId = path.basename(basedir);
this.api = new GraphqlApi(this, apiId, {
name,
...rest,
schema: SchemaFile.fromAsset(path.join(basedir, 'schema.graphql')),
});
}
public get apiId() {
return this.api.apiId;
}
public get arn() {
return this.api.arn;
}
private connect(datasource: BaseDataSource) {
this.datasources[datasource.name] = datasource;
}
public addCfnDataSource(ds: CfnDataSource) {
const datasource = {
name: ds.name,
ds,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
createResolver: (id: string, props: unknown) => {
throw new Error('not implemented');
return;
},
// eslint-disable-next-line @typescript-eslint/no-unused-vars
createFunction: (id: string, props: unknown) => {
throw new Error('not implemented');
return;
},
} as BaseDataSource;
this.connect(datasource);
return datasource;
}
public bind() {
if (this.bindCalled) {
throw new Error('bind() already called');
}
const schemaStr = readFileSync(path.join(this.basedir, 'schema.graphql'), 'utf-8');
const typeDefs = SCHEMA_DEFINITIONS + schemaStr;
const schema = makeExecutableSchema({ typeDefs });
const types = schema.getTypeMap();
const resolverFiles = this.getUnitResolvers();
const pipelines = this.getPipelineResolvers();
for (const resolverFile of [...resolverFiles, ...pipelines]) {
// make sure the type exists on the schema
const type = types[resolverFile.typeName];
if (!type || !isObjectType(type)) {
throw new Error(`Type ${resolverFile.typeName} not defined in schema`);
}
// make sure the field exists on the type
const fields = (type as GraphQLObjectType).getFields();
const field = fields[resolverFile.fieldName];
if (!field) {
throw new Error(
`Field ${resolverFile.fieldName} not defined on type ${resolverFile.typeName}`
);
}
if (resolverFile.kind === 'UNIT') {
this.buildResolver(resolverFile);
} else {
this.buildPieplineResolver(resolverFile);
}
}
this.bindCalled = true;
}
private build(key: string) {
const result = esbuild.buildSync({
bundle: true,
write: false,
outdir: path.dirname(key),
// outbase: path.dirname(fn.key),
entryPoints: [key],
format: 'esm',
platform: 'node',
target: 'node16',
sourcemap: 'inline',
sourcesContent: false,
tsconfigRaw: TS_CONFIG,
external: ['@aws-appsync/utils'],
});
if (result.errors.length) {
throw new Error('Could not build' + key + ': ' + result.errors.join('\n'));
}
fs.writeFileSync(result.outputFiles[0].path, result.outputFiles[0].text);
return result.outputFiles[0];
}
private buildPieplineResolver(resolverFile: PipelineResolverGroup) {
const { typeName, fieldName } = resolverFile;
const fns: { order: number; fn: AppsyncFunction }[] = [];
const resId = `${typeName}_${fieldName}`;
for (const fn of resolverFile.fns) {
const dataSource = this.datasources[fn.dsName];
if (!dataSource) {
throw new Error('datasource undefined: ' + fn.dsName);
}
const buildResult = this.build(fn.key);
const fnId = `FN_${typeName}_${fieldName}_${fn.order}`;
const fnR = new AppsyncFunction(this.api, fnId, {
api: this.api,
name: fnId,
description: fn.description,
dataSource,
code: Code.fromInline(buildResult.text),
runtime: FunctionRuntime.JS_1_0_0,
});
// make sure function version is not set
delete (fnR.node.defaultChild as CfnFunctionConfiguration)?.functionVersion;
fns.push({ order: fn.order, fn: fnR });
}
const pipelineConfig = fns.sort((a, b) => a.order - b.order).map((obj) => obj.fn);
const max = fns[fns.length - 1].order;
this.functions[resId] = new Array(max + 1);
fns.forEach((fn) => (this.functions[resId][fn.order] = fn.fn));
let code: Code;
if (resolverFile.hasCode) {
const buildResult = this.build(path.join(resolverFile.key, 'index.ts'));
code = Code.fromInline(buildResult.text);
} else {
code = Code.fromInline(DEF_RESOLVER_CODE);
}
const resolver = new Resolver(this.api, resId, {
api: this.api,
typeName,
fieldName,
code,
runtime: FunctionRuntime.JS_1_0_0,
pipelineConfig,
});
this.resolvers[resId] = resolver;
}
private buildResolver(resolverFile: UnitResolverFile) {
const { typeName, fieldName } = resolverFile;
const dataSource = this.datasources[resolverFile.dsName];
if (!dataSource) {
throw new Error('datasource undefined: ' + resolverFile.dsName);
}
const buildResult = this.build(resolverFile.key);
const resId = `${typeName}_${fieldName}`;
const resolver = new Resolver(this.api, resId, {
api: this.api,
dataSource,
typeName,
fieldName,
code: Code.fromInline(buildResult.text),
runtime: FunctionRuntime.JS_1_0_0,
});
this.resolvers[resId] = resolver;
}
/**
* Finds available resolvers files in the resolvers folder
* @returns a list of resolver locations
*/
getUnitResolvers() {
const resolverPath = path.join(this.basedir, 'resolvers');
if (!fs.existsSync(resolverPath)) {
// noop
return [];
}
let folders: fs.Dirent[] = [];
folders = fs.readdirSync(resolverPath, { withFileTypes: true });
const RES_LOC_REG = /^(?<typeName>\w+)\.(?<fieldName>\w+)\.\[(?<ds>\w+)\]\.(ts)$/;
// find all the resolvers and create a single function pipeline resolver
const resolvers =
folders
.filter((d) => d.isFile)
.filter((f) => f.name.match(RES_LOC_REG))
.map<UnitResolverFile>((d) => {
const name = d.name;
const m = name.match(RES_LOC_REG)!;
const key: string = path.join(this.basedir, 'resolvers', name);
return {
key,
kind: 'UNIT',
dsName: m.groups!.ds,
typeName: m.groups!.typeName,
fieldName: m.groups!.fieldName,
};
}) ?? [];
// resolverMappings.push(...directMappings)
// return resolverMappings
return resolvers;
}
/**
* Finds pipeline resolvers files in the resolvers folder
* @returns a list of resolver locations
*/
getPipelineResolvers() {
const resolverPath = path.join(this.basedir, 'resolvers');
if (!fs.existsSync(resolverPath)) {
// noop
return [];
}
let folders: fs.Dirent[] = [];
folders = fs.readdirSync(resolverPath, { withFileTypes: true });
const RES_DIR_REG = /^(?<typeName>\w+)\.(?<fieldName>\w+)$/;
const FN_LOC_REG = /^((?<description>\w+)\.)?(?<order>\d+)\.\[(?<ds>\w+)\]\.(ts)$/;
// find all the resolvers and create a single function pipeline resolver
const resolvers =
folders
.filter((entry) => entry.isDirectory())
.filter((entry) => entry.name.match(RES_DIR_REG))
.map<PipelineResolverGroup>((entry) => {
const name = entry.name;
const m = name.match(RES_DIR_REG)!;
const key = path.join(this.basedir, 'resolvers', name);
return {
key,
kind: 'PIPELINE',
typeName: m.groups!.typeName,
fieldName: m.groups!.fieldName,
fns: [],
};
}) ?? [];
// for each pipeline function directory, find the functions
for (const resolver of resolvers) {
const entries = fs.readdirSync(resolver.key);
// check if there is an index file for this pipeline
const index = entries.filter((i) => i.match(/index\.ts/));
resolver.hasCode = index.length > 0;
const fnEntries = entries.filter((i) => i.match(FN_LOC_REG));
const fns = fnEntries.map<AppSyncFn>((entry) => {
const m = entry.match(FN_LOC_REG)!;
return {
name: `${resolver.typeName}_${resolver.fieldName}_${m.groups!.order}`,
description: m.groups!.description,
key: path.join(resolver.key, entry),
dsName: m.groups!.ds,
order: parseInt(m.groups!.order),
};
});
resolver.fns.push(...fns);
}
return resolvers;
}
// from base
addNoneDataSource(id: string, options?: DataSourceOptions) {
const ds = this.api.addNoneDataSource(id, options);
this.connect(ds);
return ds;
}
addDynamoDbDataSource(id: string, table: ITable, options?: DataSourceOptions) {
const ds = this.api.addDynamoDbDataSource(id, table, options);
this.connect(ds);
return ds;
}
addHttpDataSource(id: string, endpoint: string, options?: HttpDataSourceOptions) {
const ds = this.api.addHttpDataSource(id, endpoint, options);
this.connect(ds);
return ds;
}
addLambdaDataSource(id: string, lambdaFunction: IFunction, options?: DataSourceOptions) {
const ds = this.api.addLambdaDataSource(id, lambdaFunction, options);
this.connect(ds);
return ds;
}
addRdsDataSource(
id: string,
serverlessCluster: IServerlessCluster,
secretStore: ISecret,
databaseName?: string,
options?: DataSourceOptions
) {
const ds = this.api.addRdsDataSource(id, serverlessCluster, secretStore, databaseName, options);
this.connect(ds);
return ds;
}
addEventBridgeDataSource(id: string, eventBus: IEventBus, options?: DataSourceOptions) {
const ds = this.api.addEventBridgeDataSource(id, eventBus, options);
this.connect(ds);
return ds;
}
addOpenSearchDataSource(id: string, domain: IOpenSearchDomain, options?: DataSourceOptions) {
const ds = this.api.addOpenSearchDataSource(id, domain, options);
this.connect(ds);
return ds;
}
createResolver(id: string, props: ExtendedResolverProps) {
return this.api.createResolver(id, props);
}
addSchemaDependency(construct: CfnResource) {
return this.api.addSchemaDependency(construct);
}
}