Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public makeSearchResolver(type: string, nameOverride?: string, queryTypeName: string = 'Query') {
const fieldName = nameOverride ? nameOverride : graphqlName('search' + plurality(toUpper(type)))
return new AppSync.Resolver({
ApiId: Fn.GetAtt(ResourceConstants.RESOURCES.GraphQLAPILogicalID, 'ApiId'),
DataSourceName: Fn.GetAtt(ResourceConstants.RESOURCES.ElasticsearchDataSourceLogicalID, 'Name'),
FieldName: fieldName,
TypeName: queryTypeName,
RequestMappingTemplate: print(
compoundExpression([
set(ref('indexPath'), str(`/${type.toLowerCase()}/doc/_search`)),
ElasticsearchMappingTemplate.searchItem({
path: str('$indexPath'),
size: ifElse(
ref('context.args.limit'),
ref('context.args.limit'),
int(10),
true),
from: ifElse(
ref('context.args.nextToken'),
const resFileName = `${queryTypeName}.${fieldName}.res.vtl`;
const reqTemplate = print(
compoundExpression([
RelationalDBMappingTemplate.rdsQuery({
statements: list([str(sql)]),
}),
])
);
const resTemplate = print(ref('utils.toJson($utils.rds.toJsonObject($ctx.result)[0][0])'));
fs.writeFileSync(`${this.resolverFilePath}/${reqFileName}`, reqTemplate, 'utf8');
fs.writeFileSync(`${this.resolverFilePath}/${resFileName}`, resTemplate, 'utf8');
let resolver = new AppSync.Resolver({
ApiId: Fn.Ref(ResourceConstants.PARAMETERS.AppSyncApiId),
DataSourceName: Fn.GetAtt(ResourceConstants.RESOURCES.RelationalDatabaseDataSource, 'Name'),
FieldName: fieldName,
TypeName: queryTypeName,
RequestMappingTemplateS3Location: Fn.Sub(s3BaseUrl, {
[ResourceConstants.PARAMETERS.S3DeploymentBucket]: Fn.Ref(ResourceConstants.PARAMETERS.S3DeploymentBucket),
[ResourceConstants.PARAMETERS.S3DeploymentRootKey]: Fn.Ref(ResourceConstants.PARAMETERS.S3DeploymentRootKey),
[resolverFileName]: reqFileName,
}),
ResponseMappingTemplateS3Location: Fn.Sub(s3BaseUrl, {
[ResourceConstants.PARAMETERS.S3DeploymentBucket]: Fn.Ref(ResourceConstants.PARAMETERS.S3DeploymentBucket),
[ResourceConstants.PARAMETERS.S3DeploymentRootKey]: Fn.Ref(ResourceConstants.PARAMETERS.S3DeploymentRootKey),
[resolverFileName]: resFileName,
}),
}).dependsOn([ResourceConstants.RESOURCES.RelationalDatabaseDataSource]);
return resolver;
forEach(ref('entry'), ref(`ctx.args.update${toUpper(type)}Input.keySet()`), [
set(ref('discard'), ref(`updateList.put($entry, "'$ctx.args.update${toUpper(type)}Input[$entry]'")`)),
]),
set(ref('update'), ref(`updateList.toString().replace("{","").replace("}","")`)),
RelationalDBMappingTemplate.rdsQuery({
statements: list([str(updateSql), str(selectSql)]),
}),
])
);
const resTemplate = print(ref('utils.toJson($utils.parseJson($utils.rds.toJsonString($ctx.result))[1][0])'));
fs.writeFileSync(`${this.resolverFilePath}/${reqFileName}`, reqTemplate, 'utf8');
fs.writeFileSync(`${this.resolverFilePath}/${resFileName}`, resTemplate, 'utf8');
let resolver = new AppSync.Resolver({
ApiId: Fn.Ref(ResourceConstants.PARAMETERS.AppSyncApiId),
DataSourceName: Fn.GetAtt(ResourceConstants.RESOURCES.RelationalDatabaseDataSource, 'Name'),
TypeName: mutationTypeName,
FieldName: fieldName,
RequestMappingTemplateS3Location: Fn.Sub(s3BaseUrl, {
[ResourceConstants.PARAMETERS.S3DeploymentBucket]: Fn.Ref(ResourceConstants.PARAMETERS.S3DeploymentBucket),
[ResourceConstants.PARAMETERS.S3DeploymentRootKey]: Fn.Ref(ResourceConstants.PARAMETERS.S3DeploymentRootKey),
[resolverFileName]: reqFileName,
}),
ResponseMappingTemplateS3Location: Fn.Sub(s3BaseUrl, {
[ResourceConstants.PARAMETERS.S3DeploymentBucket]: Fn.Ref(ResourceConstants.PARAMETERS.S3DeploymentBucket),
[ResourceConstants.PARAMETERS.S3DeploymentRootKey]: Fn.Ref(ResourceConstants.PARAMETERS.S3DeploymentRootKey),
[resolverFileName]: resFileName,
}),
}).dependsOn([ResourceConstants.RESOURCES.RelationalDatabaseDataSource]);
return resolver;
private makeListRelationalResolver(type: string, queryTypeName: string = 'Query') {
const fieldName = graphqlName('list' + plurality(toUpper(type)));
const sql = `SELECT * FROM ${type}`;
const reqFileName = `${queryTypeName}.${fieldName}.req.vtl`;
const resFileName = `${queryTypeName}.${fieldName}.res.vtl`;
const reqTemplate = print(
RelationalDBMappingTemplate.rdsQuery({
statements: list([str(sql)]),
})
);
const resTemplate = print(ref('utils.toJson($utils.rds.toJsonObject($ctx.result)[0])'));
fs.writeFileSync(`${this.resolverFilePath}/${reqFileName}`, reqTemplate, 'utf8');
fs.writeFileSync(`${this.resolverFilePath}/${resFileName}`, resTemplate, 'utf8');
let resolver = new AppSync.Resolver({
ApiId: Fn.Ref(ResourceConstants.PARAMETERS.AppSyncApiId),
DataSourceName: Fn.GetAtt(ResourceConstants.RESOURCES.RelationalDatabaseDataSource, 'Name'),
TypeName: queryTypeName,
FieldName: fieldName,
RequestMappingTemplateS3Location: Fn.Sub(s3BaseUrl, {
[ResourceConstants.PARAMETERS.S3DeploymentBucket]: Fn.Ref(ResourceConstants.PARAMETERS.S3DeploymentBucket),
[ResourceConstants.PARAMETERS.S3DeploymentRootKey]: Fn.Ref(ResourceConstants.PARAMETERS.S3DeploymentRootKey),
[resolverFileName]: reqFileName,
}),
ResponseMappingTemplateS3Location: Fn.Sub(s3BaseUrl, {
[ResourceConstants.PARAMETERS.S3DeploymentBucket]: Fn.Ref(ResourceConstants.PARAMETERS.S3DeploymentBucket),
[ResourceConstants.PARAMETERS.S3DeploymentRootKey]: Fn.Ref(ResourceConstants.PARAMETERS.S3DeploymentRootKey),
[resolverFileName]: resFileName,
}),
}).dependsOn([ResourceConstants.RESOURCES.RelationalDatabaseDataSource]);
public makeSearchResolver(
type: string,
nonKeywordFields: Expression[],
primaryKey: string,
queryTypeName: string,
nameOverride?: string
) {
const fieldName = nameOverride ? nameOverride : graphqlName('search' + plurality(toUpper(type)));
return new AppSync.Resolver({
ApiId: Fn.GetAtt(ResourceConstants.RESOURCES.GraphQLAPILogicalID, 'ApiId'),
DataSourceName: Fn.GetAtt(ResourceConstants.RESOURCES.ElasticsearchDataSourceLogicalID, 'Name'),
FieldName: fieldName,
TypeName: queryTypeName,
RequestMappingTemplate: print(
compoundExpression([
set(ref('indexPath'), str(`/${type.toLowerCase()}/doc/_search`)),
set(ref('nonKeywordFields'), list(nonKeywordFields)),
ifElse(
ref('util.isNullOrEmpty($context.args.sort)'),
compoundExpression([set(ref('sortDirection'), str('desc')), set(ref('sortField'), str(primaryKey))]),
compoundExpression([
set(ref('sortDirection'), raw('$util.defaultIfNull($context.args.sort.direction, "desc")')),
set(ref('sortField'), raw(`$util.defaultIfNull($context.args.sort.field, "${primaryKey}")`)),
])
),
const deleteSql = `DELETE FROM ${type} WHERE ${this.typePrimaryKeyMap.get(type)}=$ctx.args.${this.typePrimaryKeyMap.get(type)}`;
const reqFileName = `${mutationTypeName}.${fieldName}.req.vtl`;
const resFileName = `${mutationTypeName}.${fieldName}.res.vtl`;
const reqTemplate = print(
compoundExpression([
RelationalDBMappingTemplate.rdsQuery({
statements: list([str(selectSql), str(deleteSql)]),
}),
])
);
const resTemplate = print(ref('utils.toJson($utils.rds.toJsonObject($ctx.result)[0][0])'));
fs.writeFileSync(`${this.resolverFilePath}/${reqFileName}`, reqTemplate, 'utf8');
fs.writeFileSync(`${this.resolverFilePath}/${resFileName}`, resTemplate, 'utf8');
let resolver = new AppSync.Resolver({
ApiId: Fn.Ref(ResourceConstants.PARAMETERS.AppSyncApiId),
DataSourceName: Fn.GetAtt(ResourceConstants.RESOURCES.RelationalDatabaseDataSource, 'Name'),
TypeName: mutationTypeName,
FieldName: fieldName,
RequestMappingTemplateS3Location: Fn.Sub(s3BaseUrl, {
[ResourceConstants.PARAMETERS.S3DeploymentBucket]: Fn.Ref(ResourceConstants.PARAMETERS.S3DeploymentBucket),
[ResourceConstants.PARAMETERS.S3DeploymentRootKey]: Fn.Ref(ResourceConstants.PARAMETERS.S3DeploymentRootKey),
[resolverFileName]: reqFileName,
}),
ResponseMappingTemplateS3Location: Fn.Sub(s3BaseUrl, {
[ResourceConstants.PARAMETERS.S3DeploymentBucket]: Fn.Ref(ResourceConstants.PARAMETERS.S3DeploymentBucket),
[ResourceConstants.PARAMETERS.S3DeploymentRootKey]: Fn.Ref(ResourceConstants.PARAMETERS.S3DeploymentRootKey),
[resolverFileName]: resFileName,
}),
}).dependsOn([ResourceConstants.RESOURCES.RelationalDatabaseDataSource]);
set(ref('discard'), ref(`vals.add("'$ctx.args.create${toUpper(type)}Input[$entry]'")`)),
]),
set(ref('valStr'), ref('vals.toString().replace("[","(").replace("]",")")')),
set(ref('colStr'), ref('cols.toString().replace("[","(").replace("]",")")')),
RelationalDBMappingTemplate.rdsQuery({
statements: list([str(createSql), str(selectSql)]),
}),
])
);
const resTemplate = print(ref('utils.toJson($utils.parseJson($utils.rds.toJsonString($ctx.result))[1][0])'));
fs.writeFileSync(`${this.resolverFilePath}/${reqFileName}`, reqTemplate, 'utf8');
fs.writeFileSync(`${this.resolverFilePath}/${resFileName}`, resTemplate, 'utf8');
let resolver = new AppSync.Resolver({
ApiId: Fn.Ref(ResourceConstants.PARAMETERS.AppSyncApiId),
DataSourceName: Fn.GetAtt(ResourceConstants.RESOURCES.RelationalDatabaseDataSource, 'Name'),
TypeName: mutationTypeName,
FieldName: fieldName,
RequestMappingTemplateS3Location: Fn.Sub(s3BaseUrl, {
[ResourceConstants.PARAMETERS.S3DeploymentBucket]: Fn.Ref(ResourceConstants.PARAMETERS.S3DeploymentBucket),
[ResourceConstants.PARAMETERS.S3DeploymentRootKey]: Fn.Ref(ResourceConstants.PARAMETERS.S3DeploymentRootKey),
[resolverFileName]: reqFileName,
}),
ResponseMappingTemplateS3Location: Fn.Sub(s3BaseUrl, {
[ResourceConstants.PARAMETERS.S3DeploymentBucket]: Fn.Ref(ResourceConstants.PARAMETERS.S3DeploymentBucket),
[ResourceConstants.PARAMETERS.S3DeploymentRootKey]: Fn.Ref(ResourceConstants.PARAMETERS.S3DeploymentRootKey),
[resolverFileName]: resFileName,
}),
}).dependsOn([ResourceConstants.RESOURCES.RelationalDatabaseDataSource]);
return resolver;