Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public makeGetResolver(type: string, nameOverride?: string, isSyncEnabled: boolean = false, queryTypeName: string = 'Query') {
const fieldName = nameOverride ? nameOverride : graphqlName('get' + toUpper(type));
return new AppSync.Resolver({
ApiId: Fn.GetAtt(ResourceConstants.RESOURCES.GraphQLAPILogicalID, 'ApiId'),
DataSourceName: Fn.GetAtt(ModelResourceIDs.ModelTableDataSourceID(type), 'Name'),
FieldName: fieldName,
TypeName: queryTypeName,
RequestMappingTemplate: print(
DynamoDBMappingTemplate.getItem({
key: ifElse(
ref(ResourceConstants.SNIPPETS.ModelObjectKey),
raw(`$util.toJson(\$${ResourceConstants.SNIPPETS.ModelObjectKey})`),
obj({
id: ref('util.dynamodb.toDynamoDBJson($ctx.args.id)'),
}),
true
),
isSyncEnabled,
})
),
ResponseMappingTemplate: isSyncEnabled ? print(DynamoDBMappingTemplate.dynamoDBResponse()) : print(ref('util.toJson($ctx.result)')),
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 makeDeleteResolver({ type, nameOverride, syncConfig, mutationTypeName = 'Mutation' }: MutationResolverInput) {
const fieldName = nameOverride ? nameOverride : graphqlName('delete' + 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(
ref(ResourceConstants.SNIPPETS.AuthCondition),
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)")'),
public makeCreateResolver({ type, nameOverride, syncConfig, mutationTypeName = 'Mutation' }: MutationResolverInput) {
const fieldName = nameOverride ? nameOverride : graphqlName('create' + toUpper(type));
return new AppSync.Resolver({
ApiId: Fn.GetAtt(ResourceConstants.RESOURCES.GraphQLAPILogicalID, 'ApiId'),
DataSourceName: Fn.GetAtt(ModelResourceIDs.ModelTableDataSourceID(type), 'Name'),
FieldName: fieldName,
TypeName: mutationTypeName,
RequestMappingTemplate: printBlock('Prepare DynamoDB PutItem Request')(
compoundExpression([
qref('$context.args.input.put("createdAt", $util.defaultIfNull($ctx.args.input.createdAt, $util.time.nowISO8601()))'),
qref('$context.args.input.put("updatedAt", $util.defaultIfNull($ctx.args.input.updatedAt, $util.time.nowISO8601()))'),
qref(`$context.args.input.put("__typename", "${type}")`),
set(
ref('condition'),
obj({
expression: str('attribute_not_exists(#id)'),
expressionNames: obj({
'#id': str('id'),
}),
})
),
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)")'),
function makeQueryResolver(definition: ObjectTypeDefinitionNode, directive: DirectiveNode, ctx: TransformerContext) {
const type = definition.name.value;
const directiveArgs: KeyArguments = getDirectiveArguments(directive);
const index = directiveArgs.name;
const fieldName = directiveArgs.queryField;
const queryTypeName = ctx.getQueryTypeName();
const defaultPageLimit = 10
const requestVariable = 'QueryRequest';
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([
setQuerySnippet(definition, directive, ctx),
set(ref('limit'),
ref(`util.defaultIfNull($context.args.limit, ${defaultPageLimit})`)),
set(
ref(requestVariable),
obj({
version: str('2017-02-28'),
operation: str('Query'),
limit: ref('limit'),
query: ref(ResourceConstants.SNIPPETS.ModelQueryExpression),
index: str(index)
})
public makeGetItemConnectionResolver(type: string, field: string, relatedType: string, connectionAttribute: string): Resolver {
return new Resolver({
ApiId: Fn.GetAtt(ResourceConstants.RESOURCES.GraphQLAPILogicalID, 'ApiId'),
DataSourceName: Fn.GetAtt(ModelResourceIDs.ModelTableDataSourceID(relatedType), 'Name'),
FieldName: field,
TypeName: type,
RequestMappingTemplate: print(
DynamoDBMappingTemplate.getItem({
key: obj({
id: ref(`util.dynamodb.toDynamoDBJson($util.defaultIfNullOrBlank($ctx.source.${connectionAttribute}, "${NONE_VALUE}"))`)
})
})
),
ResponseMappingTemplate: print(
ref('util.toJson($context.result)')
)
}).dependsOn(ResourceConstants.RESOURCES.GraphQLSchemaLogicalID)
}
public makeListResolver(type: string, nameOverride?: string, isSyncEnabled: boolean = false, queryTypeName: string = 'Query') {
const fieldName = nameOverride ? nameOverride : graphqlName('list' + plurality(toUpper(type)));
const defaultPageLimit = 10;
const requestVariable = 'ListRequest';
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})`)),
set(
ref(requestVariable),
obj({
version: isSyncEnabled ? str('2018-05-29') : str('2017-02-28'),
limit: ref('limit'),
})
),
iff(ref('context.args.nextToken'), set(ref(`${requestVariable}.nextToken`), str('$context.args.nextToken'))),
iff(
ref('context.args.filter'),
set(ref(`${requestVariable}.filter`), ref('util.parseJson("$util.transform.toDynamoDBFilterExpression($ctx.args.filter)")'))
keyObj.attributes.push([
sortKeyName,
ref(`util.dynamodb.toDynamoDBJson($util.defaultIfNullOrBlank("${condensedSortKeyValue}", "${NONE_VALUE}"))`)
]);
} else if (connectionAttributes[1]) {
const sortKeyName = keySchema[1].AttributeName as string
keyObj.attributes.push([
sortKeyName,
ref(`util.dynamodb.toDynamoDBJson($util.defaultIfNullOrBlank($ctx.source.${connectionAttributes[1]}, "${NONE_VALUE}"))`)
]);
}
return new Resolver({
ApiId: Fn.GetAtt(ResourceConstants.RESOURCES.GraphQLAPILogicalID, 'ApiId'),
DataSourceName: Fn.GetAtt(ModelResourceIDs.ModelTableDataSourceID(relatedType), 'Name'),
FieldName: field,
TypeName: type,
RequestMappingTemplate: print(
compoundExpression([
DynamoDBMappingTemplate.getItem({
key: keyObj
})
])
),
ResponseMappingTemplate: print(
ref('util.toJson($context.result)')
)
}).dependsOn(ResourceConstants.RESOURCES.GraphQLSchemaLogicalID)
}
str('$context.args.nextToken'),
nul()
),
index: indexName ? str(indexName) : undefined
}
if (!indexName) {
const indexArg = 'index'
delete queryArguments[indexArg];
}
const queryObj = DynamoDBMappingTemplate.query(queryArguments);
return new Resolver({
ApiId: Fn.GetAtt(ResourceConstants.RESOURCES.GraphQLAPILogicalID, 'ApiId'),
DataSourceName: Fn.GetAtt(ModelResourceIDs.ModelTableDataSourceID(relatedType.name.value), 'Name'),
FieldName: field,
TypeName: type,
RequestMappingTemplate: print(
compoundExpression([
...setup,
queryObj
])
),
ResponseMappingTemplate: print(
compoundExpression([
iff(raw('!$result'), set(ref('result'), ref('ctx.result'))),
raw('$util.toJson($result)')
])
)
}).dependsOn(ResourceConstants.RESOURCES.GraphQLSchemaLogicalID)
}