Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public makeDynamoDBStreamingFunction() {
return new Lambda.Function({
Code: {
S3Bucket: Fn.Ref(ResourceConstants.PARAMETERS.S3DeploymentBucket),
S3Key: Fn.Join('/', [
Fn.Ref(ResourceConstants.PARAMETERS.S3DeploymentRootKey),
'functions',
Fn.Join('.', [ResourceConstants.RESOURCES.ElasticsearchStreamingLambdaFunctionLogicalID, 'zip']),
]),
},
FunctionName: this.joinWithEnv('-', [
Fn.Ref(ResourceConstants.PARAMETERS.ElasticsearchStreamingFunctionName),
Fn.GetAtt(ResourceConstants.RESOURCES.GraphQLAPILogicalID, 'ApiId'),
]),
Handler: Fn.Ref(ResourceConstants.PARAMETERS.ElasticsearchStreamingLambdaHandlerName),
Role: Fn.GetAtt(ResourceConstants.RESOURCES.ElasticsearchStreamingLambdaIAMRoleLogicalID, 'Arn'),
Runtime: Fn.Ref(ResourceConstants.PARAMETERS.ElasticsearchStreamingLambdaRuntime),
Layers: [Fn.FindInMap('LayerResourceMapping', Fn.Ref('AWS::Region'), 'layerRegion')],
Environment: {
Variables: {
ES_ENDPOINT: Fn.Join('', ['https://', Fn.GetAtt(ResourceConstants.RESOURCES.ElasticsearchDomainLogicalID, 'DomainEndpoint')]),
ES_REGION: Fn.Select(3, Fn.Split(':', Fn.GetAtt(ResourceConstants.RESOURCES.ElasticsearchDomainLogicalID, 'DomainArn'))),
DEBUG: Fn.Ref(ResourceConstants.PARAMETERS.ElasticsearchDebugStreamingLambda),
},
},
}).dependsOn([
ResourceConstants.RESOURCES.ElasticsearchStreamingLambdaIAMRoleLogicalID,
ResourceConstants.RESOURCES.ElasticsearchDomainLogicalID,
]);
public makeQueryResolver(type: string, nameOverride?: string, isSyncEnabled: boolean = false, queryTypeName: string = 'Query') {
const fieldName = nameOverride ? nameOverride : graphqlName(`query${toUpper(type)}`);
const defaultPageLimit = 10;
return new AppSync.Resolver({
ApiId: Fn.GetAtt(ResourceConstants.RESOURCES.GraphQLAPILogicalID, 'ApiId'),
DataSourceName: Fn.GetAtt(ModelResourceIDs.ModelTableDataSourceID(type), 'Name'),
FieldName: fieldName,
TypeName: queryTypeName,
RequestMappingTemplate: print(
compoundExpression([
set(ref('limit'), ref(`util.defaultIfNull($context.args.limit, ${defaultPageLimit})`)),
DynamoDBMappingTemplate.query({
query: obj({
expression: str('#typename = :typename'),
expressionNames: obj({
'#typename': str('__typename'),
}),
expressionValues: obj({
':typename': obj({
S: str(type),
}),
}),
public makeUpdateResolver({ type, nameOverride, syncConfig, mutationTypeName = 'Mutation' }: MutationResolverInput) {
const fieldName = nameOverride ? nameOverride : graphqlName(`update` + toUpper(type));
const isSyncEnabled = syncConfig ? true : false;
return new AppSync.Resolver({
ApiId: Fn.GetAtt(ResourceConstants.RESOURCES.GraphQLAPILogicalID, 'ApiId'),
DataSourceName: Fn.GetAtt(ModelResourceIDs.ModelTableDataSourceID(type), 'Name'),
FieldName: fieldName,
TypeName: mutationTypeName,
RequestMappingTemplate: print(
compoundExpression([
ifElse(
raw(`$${ResourceConstants.SNIPPETS.AuthCondition} && $${ResourceConstants.SNIPPETS.AuthCondition}.expression != ""`),
compoundExpression([
set(ref('condition'), ref(ResourceConstants.SNIPPETS.AuthCondition)),
ifElse(
ref(ResourceConstants.SNIPPETS.ModelObjectKey),
forEach(ref('entry'), ref(`${ResourceConstants.SNIPPETS.ModelObjectKey}.entrySet()`), [
qref('$condition.put("expression", "$condition.expression AND attribute_exists(#keyCondition$velocityCount)")'),
qref('$condition.expressionNames.put("#keyCondition$velocityCount", "$entry.key")'),
]),
compoundExpression([
qref('$condition.put("expression", "$condition.expression AND attribute_exists(#id)")'),
appendFunctionToResolver(resolver: any, functionId: string) {
if (
resolver.Properties.PipelineConfig &&
resolver.Properties.PipelineConfig.Functions &&
Array.isArray(resolver.Properties.PipelineConfig.Functions)
) {
resolver.Properties.PipelineConfig.Functions.push(Fn.GetAtt(functionId, 'FunctionId'));
}
return resolver;
}
}
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}")`)),
])
),
ElasticsearchMappingTemplate.searchItem({
path: str('$indexPath'),
private dynamoDBTableName(typeName: string): IntrinsicFunction {
return Fn.If(
ResourceConstants.CONDITIONS.HasEnvironmentParameter,
Fn.Join('-', [
typeName,
Fn.GetAtt(ResourceConstants.RESOURCES.GraphQLAPILogicalID, 'ApiId'),
Fn.Ref(ResourceConstants.PARAMETERS.Env),
]),
Fn.Join('-', [typeName, Fn.GetAtt(ResourceConstants.RESOURCES.GraphQLAPILogicalID, 'ApiId')])
);
}
public makeAppSyncSchema(schema?: string) {
if (schema) {
return new AppSync.GraphQLSchema({
ApiId: Fn.GetAtt(ResourceConstants.RESOURCES.GraphQLAPILogicalID, 'ApiId'),
Definition: schema,
});
}
return new AppSync.GraphQLSchema({
ApiId: Fn.GetAtt(ResourceConstants.RESOURCES.GraphQLAPILogicalID, 'ApiId'),
DefinitionS3Location: Fn.Sub('s3://${S3DeploymentBucket}/${S3DeploymentRootKey}/schema.graphql', {
S3DeploymentBucket: Fn.Ref(ResourceConstants.PARAMETERS.S3DeploymentBucket),
S3DeploymentRootKey: Fn.Ref(ResourceConstants.PARAMETERS.S3DeploymentRootKey),
}),
});
}
}
private domainArn() {
return Fn.GetAtt(ResourceConstants.RESOURCES.ElasticsearchDomainLogicalID, 'DomainArn');
}
public makePutResolver(baseURL: string, path: string, type: string, field: string, nonNullArgs: string[], headers: HttpHeader[]) {
const parsedHeaders = headers.map(header => qref(`$headers.put("${header.key}", "${header.value}")`));
return new AppSync.Resolver({
ApiId: Fn.GetAtt(ResourceConstants.RESOURCES.GraphQLAPILogicalID, 'ApiId'),
DataSourceName: Fn.GetAtt(HttpResourceIDs.HttpDataSourceID(baseURL), 'Name'),
FieldName: field,
TypeName: type,
RequestMappingTemplate: this.replaceEnv(
print(
compoundExpression([
nonNullArgs.length > 0 ? this.makeNonNullChecks(nonNullArgs) : null,
set(ref('headers'), ref('utils.http.copyHeaders($ctx.request.headers)')),
qref('$headers.put("Content-Type", "application/json")'),
qref('$headers.put("accept-encoding", "application/json")'),
...parsedHeaders,
HttpMappingTemplate.putRequest({
resourcePath: path,
params: obj({
body: ref('util.toJson($ctx.args.body)'),
query: ref('util.toJson($ctx.args.query)'),
headers: ref('util.toJson($headers)'),
public makeApiKeyOutput(): any {
return {
Description: "Your GraphQL API key. Provide via 'x-api-key' header.",
Value: Fn.GetAtt(ResourceConstants.RESOURCES.APIKeyLogicalID, 'ApiKey'),
Export: {
Name: Fn.Join(':', [Refs.StackName, 'GraphQLApiKey']),
},
Condition: ResourceConstants.CONDITIONS.ShouldCreateAPIKey,
};
}