Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
let attributeName = keySchema[0].AttributeName as string
let keyObj : ObjectNode = obj({
[attributeName] :
ref(`util.dynamodb.toDynamoDBJson($util.defaultIfNullOrBlank($ctx.source.${connectionAttributes[0]}, "${NONE_VALUE}"))`)
})
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: ModelResourceIDs.ModelTableResourceID(relatedType),
FieldName: field,
TypeName: type,
RequestMappingTemplate: print(
compoundExpression([
DynamoDBMappingTemplate.getItem({
key: keyObj
})
])
),
ResponseMappingTemplate: print(
ref('util.toJson($context.result)')
)
}).dependsOn([ResourceConstants.RESOURCES.GraphQLSchemaLogicalID, ModelResourceIDs.ModelTableResourceID(relatedType)])
}
rangeKeyFields.map(keyField => `\${ctx.source.${keyField}}`)
)
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)
}
ref('util.transform.toDynamoDBFilterExpression($ctx.args.filter)'),
nul()
),
limit: ref('limit'),
nextToken: ifElse(
ref('context.args.nextToken'),
str('$context.args.nextToken'),
nul()
)
}
if (indexName) {
let indexArg = "index";
queryArguments[indexArg] = str(indexName);
}
return new Resolver({
ApiId: Fn.GetAtt(ResourceConstants.RESOURCES.GraphQLAPILogicalID, 'ApiId'),
DataSourceName: ModelResourceIDs.ModelTableResourceID(relatedType.name.value),
FieldName: field,
TypeName: type,
RequestMappingTemplate: print(
compoundExpression([
...setup,
DynamoDBMappingTemplate.query(queryArguments)
])
),
ResponseMappingTemplate: print(
compoundExpression([
iff(raw('!$result'), set(ref('result'), ref('ctx.result'))),
raw('$util.toJson($result)')
])
)
nextToken: ifElse(
ref('context.args.nextToken'),
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)')
])
)
set(ref('query'), obj({
'expression': str('#connectionAttribute = :connectionAttribute'),
'expressionNames': obj({
'#connectionAttribute': str(connectionAttribute)
}),
'expressionValues': obj({
':connectionAttribute': obj({
'S': str('$context.source.id')
})
})
}))
];
if (sortKeyInfo) {
setup.push(applyKeyConditionExpression(sortKeyInfo.fieldName, sortKeyInfo.attributeType, 'query'));
}
return new Resolver({
ApiId: Fn.GetAtt(ResourceConstants.RESOURCES.GraphQLAPILogicalID, 'ApiId'),
DataSourceName: Fn.GetAtt(ModelResourceIDs.ModelTableDataSourceID(relatedType), 'Name'),
FieldName: field,
TypeName: type,
RequestMappingTemplate: print(
compoundExpression([
...setup,
DynamoDBMappingTemplate.query({
query: raw('$util.toJson($query)'),
scanIndexForward: ifElse(
ref('context.args.sortDirection'),
ifElse(
equals(ref('context.args.sortDirection'), str('ASC')),
bool(true),
bool(false)
),
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)
}