UNPKG

@aws-amplify/graphql-model-transformer

Version:
140 lines • 22.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.generateSyncRequestTemplate = exports.generateListRequestTemplate = exports.generateGetResponseTemplate = exports.generateGetRequestTemplate = void 0; const graphql_mapping_template_1 = require("graphql-mapping-template"); const graphql_transformer_common_1 = require("graphql-transformer-common"); const authFilter = (0, graphql_mapping_template_1.ref)('ctx.stash.authFilter'); const generateGetRequestTemplate = () => { const statements = [ (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('GetRequest'), (0, graphql_mapping_template_1.obj)({ version: (0, graphql_mapping_template_1.str)('2018-05-29'), operation: (0, graphql_mapping_template_1.str)('Query') })), (0, graphql_mapping_template_1.ifElse)((0, graphql_mapping_template_1.ref)('ctx.stash.metadata.modelObjectKey'), (0, graphql_mapping_template_1.compoundExpression)([ (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('expression'), (0, graphql_mapping_template_1.str)('')), (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('expressionNames'), (0, graphql_mapping_template_1.obj)({})), (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('expressionValues'), (0, graphql_mapping_template_1.obj)({})), (0, graphql_mapping_template_1.forEach)((0, graphql_mapping_template_1.ref)('item'), (0, graphql_mapping_template_1.ref)('ctx.stash.metadata.modelObjectKey.entrySet()'), [ (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('expression'), (0, graphql_mapping_template_1.str)('$expression#keyCount$velocityCount = :valueCount$velocityCount AND ')), (0, graphql_mapping_template_1.qref)((0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('expressionNames.put'), (0, graphql_mapping_template_1.str)('#keyCount$velocityCount'), (0, graphql_mapping_template_1.ref)('item.key'))), (0, graphql_mapping_template_1.qref)((0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('expressionValues.put'), (0, graphql_mapping_template_1.str)(':valueCount$velocityCount'), (0, graphql_mapping_template_1.ref)('item.value'))), ]), (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('expression'), (0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('expression.replaceAll'), (0, graphql_mapping_template_1.str)('AND $'), (0, graphql_mapping_template_1.str)(''))), (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('query'), (0, graphql_mapping_template_1.obj)({ expression: (0, graphql_mapping_template_1.ref)('expression'), expressionNames: (0, graphql_mapping_template_1.ref)('expressionNames'), expressionValues: (0, graphql_mapping_template_1.ref)('expressionValues') })), ]), (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('query'), (0, graphql_mapping_template_1.obj)({ expression: (0, graphql_mapping_template_1.str)('id = :id'), expressionValues: (0, graphql_mapping_template_1.obj)({ ':id': (0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('util.parseJson'), (0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('util.dynamodb.toDynamoDBJson'), (0, graphql_mapping_template_1.ref)('ctx.args.id'))), }), }))), (0, graphql_mapping_template_1.qref)((0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('GetRequest.put'), (0, graphql_mapping_template_1.str)('query'), (0, graphql_mapping_template_1.ref)('query'))), (0, graphql_mapping_template_1.iff)((0, graphql_mapping_template_1.not)((0, graphql_mapping_template_1.isNullOrEmpty)(authFilter)), (0, graphql_mapping_template_1.qref)((0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('GetRequest.put'), (0, graphql_mapping_template_1.str)('filter'), (0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('util.parseJson'), (0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('util.transform.toDynamoDBFilterExpression'), authFilter))))), (0, graphql_mapping_template_1.toJson)((0, graphql_mapping_template_1.ref)('GetRequest')), ]; return (0, graphql_mapping_template_1.printBlock)('Get Request template')((0, graphql_mapping_template_1.compoundExpression)(statements)); }; exports.generateGetRequestTemplate = generateGetRequestTemplate; const generateGetResponseTemplate = (isSyncEnabled) => { const statements = new Array(); if (isSyncEnabled) { statements.push((0, graphql_mapping_template_1.iff)((0, graphql_mapping_template_1.ref)('ctx.error'), (0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('util.error'), (0, graphql_mapping_template_1.ref)('ctx.error.message'), (0, graphql_mapping_template_1.ref)('ctx.error.type'), (0, graphql_mapping_template_1.ref)('ctx.result')))); } else { statements.push((0, graphql_mapping_template_1.iff)((0, graphql_mapping_template_1.ref)('ctx.error'), (0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('util.error'), (0, graphql_mapping_template_1.ref)('ctx.error.message'), (0, graphql_mapping_template_1.ref)('ctx.error.type')))); } statements.push((0, graphql_mapping_template_1.ifElse)((0, graphql_mapping_template_1.and)([(0, graphql_mapping_template_1.not)((0, graphql_mapping_template_1.ref)('ctx.result.items.isEmpty()')), (0, graphql_mapping_template_1.equals)((0, graphql_mapping_template_1.ref)('ctx.result.scannedCount'), (0, graphql_mapping_template_1.int)(1))]), (0, graphql_mapping_template_1.toJson)((0, graphql_mapping_template_1.ref)('ctx.result.items[0]')), (0, graphql_mapping_template_1.compoundExpression)([ (0, graphql_mapping_template_1.iff)((0, graphql_mapping_template_1.and)([(0, graphql_mapping_template_1.ref)('ctx.result.items.isEmpty()'), (0, graphql_mapping_template_1.equals)((0, graphql_mapping_template_1.ref)('ctx.result.scannedCount'), (0, graphql_mapping_template_1.int)(1))]), (0, graphql_mapping_template_1.ref)('util.unauthorized()')), (0, graphql_mapping_template_1.toJson)((0, graphql_mapping_template_1.nul)()), ]))); return (0, graphql_mapping_template_1.printBlock)('Get Response template')((0, graphql_mapping_template_1.compoundExpression)(statements)); }; exports.generateGetResponseTemplate = generateGetResponseTemplate; const generateListRequestTemplate = () => { const requestVariable = 'ListRequest'; const modelQueryObj = 'ctx.stash.modelQueryExpression'; const indexNameVariable = 'ctx.stash.metadata.index'; const expression = (0, graphql_mapping_template_1.compoundExpression)([ graphql_transformer_common_1.setArgs, (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('limit'), (0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)(`util.defaultIfNull`), (0, graphql_mapping_template_1.ref)('args.limit'), (0, graphql_mapping_template_1.int)(100))), (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)(requestVariable), (0, graphql_mapping_template_1.obj)({ version: (0, graphql_mapping_template_1.str)('2018-05-29'), limit: (0, graphql_mapping_template_1.ref)('limit'), })), (0, graphql_mapping_template_1.iff)((0, graphql_mapping_template_1.ref)('args.nextToken'), (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)(`${requestVariable}.nextToken`), (0, graphql_mapping_template_1.ref)('args.nextToken'))), (0, graphql_mapping_template_1.ifElse)((0, graphql_mapping_template_1.not)((0, graphql_mapping_template_1.isNullOrEmpty)(authFilter)), (0, graphql_mapping_template_1.compoundExpression)([ (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('filter'), authFilter), (0, graphql_mapping_template_1.iff)((0, graphql_mapping_template_1.not)((0, graphql_mapping_template_1.isNullOrEmpty)((0, graphql_mapping_template_1.ref)('args.filter'))), (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('filter'), (0, graphql_mapping_template_1.obj)({ and: (0, graphql_mapping_template_1.list)([(0, graphql_mapping_template_1.ref)('filter'), (0, graphql_mapping_template_1.ref)('args.filter')]) }))), ]), (0, graphql_mapping_template_1.iff)((0, graphql_mapping_template_1.not)((0, graphql_mapping_template_1.isNullOrEmpty)((0, graphql_mapping_template_1.ref)('args.filter'))), (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('filter'), (0, graphql_mapping_template_1.ref)('args.filter')))), (0, graphql_mapping_template_1.iff)((0, graphql_mapping_template_1.not)((0, graphql_mapping_template_1.isNullOrEmpty)((0, graphql_mapping_template_1.ref)('filter'))), (0, graphql_mapping_template_1.compoundExpression)([ (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)(`filterExpression`), (0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('util.parseJson'), (0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('util.transform.toDynamoDBFilterExpression'), (0, graphql_mapping_template_1.ref)('filter')))), (0, graphql_mapping_template_1.iff)((0, graphql_mapping_template_1.isNullOrEmpty)((0, graphql_mapping_template_1.ref)('filterExpression')), (0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('util.error'), (0, graphql_mapping_template_1.str)('Unable to process the filter expression'), (0, graphql_mapping_template_1.str)('Unrecognized Filter'))), (0, graphql_mapping_template_1.iff)((0, graphql_mapping_template_1.not)((0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('util.isNullOrBlank'), (0, graphql_mapping_template_1.ref)('filterExpression.expression'))), (0, graphql_mapping_template_1.compoundExpression)([ (0, graphql_mapping_template_1.iff)((0, graphql_mapping_template_1.equals)((0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('filterExpression.expressionValues.size')), (0, graphql_mapping_template_1.int)(0)), (0, graphql_mapping_template_1.qref)((0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('filterExpression.remove'), (0, graphql_mapping_template_1.str)('expressionValues')))), (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)(`${requestVariable}.filter`), (0, graphql_mapping_template_1.ref)(`filterExpression`)), ])), ])), (0, graphql_mapping_template_1.ifElse)((0, graphql_mapping_template_1.and)([ (0, graphql_mapping_template_1.not)((0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('util.isNull'), (0, graphql_mapping_template_1.ref)(modelQueryObj))), (0, graphql_mapping_template_1.not)((0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('util.isNullOrEmpty'), (0, graphql_mapping_template_1.ref)(`${modelQueryObj}.expression`))), ]), (0, graphql_mapping_template_1.compoundExpression)([ (0, graphql_mapping_template_1.qref)((0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)(`${requestVariable}.put`), (0, graphql_mapping_template_1.str)('operation'), (0, graphql_mapping_template_1.str)('Query'))), (0, graphql_mapping_template_1.qref)((0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)(`${requestVariable}.put`), (0, graphql_mapping_template_1.str)('query'), (0, graphql_mapping_template_1.ref)(modelQueryObj))), (0, graphql_mapping_template_1.ifElse)((0, graphql_mapping_template_1.and)([(0, graphql_mapping_template_1.not)((0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('util.isNull'), (0, graphql_mapping_template_1.ref)('args.sortDirection'))), (0, graphql_mapping_template_1.equals)((0, graphql_mapping_template_1.ref)('args.sortDirection'), (0, graphql_mapping_template_1.str)('DESC'))]), (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)(`${requestVariable}.scanIndexForward`), (0, graphql_mapping_template_1.bool)(false)), (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)(`${requestVariable}.scanIndexForward`), (0, graphql_mapping_template_1.bool)(true))), ]), (0, graphql_mapping_template_1.qref)((0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)(`${requestVariable}.put`), (0, graphql_mapping_template_1.str)('operation'), (0, graphql_mapping_template_1.str)('Scan')))), (0, graphql_mapping_template_1.iff)((0, graphql_mapping_template_1.not)((0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('util.isNull'), (0, graphql_mapping_template_1.ref)(indexNameVariable))), (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)(`${requestVariable}.index`), (0, graphql_mapping_template_1.ref)(indexNameVariable))), (0, graphql_mapping_template_1.toJson)((0, graphql_mapping_template_1.ref)(requestVariable)), ]); return (0, graphql_mapping_template_1.printBlock)('List Request')(expression); }; exports.generateListRequestTemplate = generateListRequestTemplate; const generateSyncRequestTemplate = () => { const requestVariable = 'ctx.stash.QueryRequest'; return (0, graphql_mapping_template_1.printBlock)('Sync Request template')((0, graphql_mapping_template_1.compoundExpression)([ graphql_transformer_common_1.setArgs, (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('queryFilterContainsAuthField'), (0, graphql_mapping_template_1.bool)(false)), (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('authFilterContainsSortKey'), (0, graphql_mapping_template_1.bool)(false)), (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('useScan'), (0, graphql_mapping_template_1.bool)(true)), (0, graphql_mapping_template_1.iff)((0, graphql_mapping_template_1.and)([(0, graphql_mapping_template_1.isNullOrEmpty)(authFilter), (0, graphql_mapping_template_1.ref)('ctx.stash.QueryRequest')]), (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('useScan'), (0, graphql_mapping_template_1.bool)(false))), (0, graphql_mapping_template_1.ifElse)((0, graphql_mapping_template_1.not)((0, graphql_mapping_template_1.isNullOrEmpty)(authFilter)), (0, graphql_mapping_template_1.compoundExpression)([ (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('filter'), authFilter), (0, graphql_mapping_template_1.iff)((0, graphql_mapping_template_1.ref)('ctx.stash.QueryRequestVariables.partitionKey'), (0, graphql_mapping_template_1.compoundExpression)([ (0, graphql_mapping_template_1.forEach)((0, graphql_mapping_template_1.ref)('filterItem'), (0, graphql_mapping_template_1.ref)('ctx.stash.authFilter.or'), [ (0, graphql_mapping_template_1.iff)((0, graphql_mapping_template_1.raw)('$filterItem.get($ctx.stash.QueryRequestVariables.partitionKey)'), (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('queryFilterContainsAuthField'), (0, graphql_mapping_template_1.bool)(true))), ]), (0, graphql_mapping_template_1.ifElse)((0, graphql_mapping_template_1.not)((0, graphql_mapping_template_1.ref)('queryFilterContainsAuthField')), (0, graphql_mapping_template_1.compoundExpression)([ (0, graphql_mapping_template_1.forEach)((0, graphql_mapping_template_1.ref)('filterItem'), (0, graphql_mapping_template_1.ref)('ctx.stash.authFilter.or'), [ (0, graphql_mapping_template_1.forEach)((0, graphql_mapping_template_1.ref)('sortKey'), (0, graphql_mapping_template_1.ref)('ctx.stash.QueryRequestVariables.sortKeys'), [ (0, graphql_mapping_template_1.iff)((0, graphql_mapping_template_1.raw)('$filterItem.get($sortKey)'), (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('authFilterContainsSortKey'), (0, graphql_mapping_template_1.bool)(true))), ]), ]), (0, graphql_mapping_template_1.iff)((0, graphql_mapping_template_1.not)((0, graphql_mapping_template_1.ref)('authFilterContainsSortKey')), (0, graphql_mapping_template_1.compoundExpression)([ (0, graphql_mapping_template_1.ifElse)((0, graphql_mapping_template_1.not)((0, graphql_mapping_template_1.isNullOrEmpty)((0, graphql_mapping_template_1.ref)(`${requestVariable}.filter`))), (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)(`${requestVariable}.filter`), (0, graphql_mapping_template_1.obj)({ and: (0, graphql_mapping_template_1.list)([(0, graphql_mapping_template_1.ref)(`${requestVariable}.filter`), authFilter]) })), (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)(`${requestVariable}.filter`), authFilter)), (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('useScan'), (0, graphql_mapping_template_1.bool)(false)), ])), ]), (0, graphql_mapping_template_1.compoundExpression)([ (0, graphql_mapping_template_1.forEach)((0, graphql_mapping_template_1.ref)('filterItem'), (0, graphql_mapping_template_1.ref)('ctx.stash.authFilter.or'), [ (0, graphql_mapping_template_1.iff)((0, graphql_mapping_template_1.raw)('$util.toJson($filterItem) == $util.toJson($ctx.stash.QueryRequestVariables.partitionKeyFilter)'), (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('useScan'), (0, graphql_mapping_template_1.bool)(false))), ]), ])), ])), (0, graphql_mapping_template_1.iff)((0, graphql_mapping_template_1.not)((0, graphql_mapping_template_1.isNullOrEmpty)((0, graphql_mapping_template_1.ref)('args.filter'))), (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('filter'), (0, graphql_mapping_template_1.obj)({ and: (0, graphql_mapping_template_1.list)([(0, graphql_mapping_template_1.ref)('filter'), (0, graphql_mapping_template_1.ref)('args.filter')]) }))), ]), (0, graphql_mapping_template_1.iff)((0, graphql_mapping_template_1.not)((0, graphql_mapping_template_1.isNullOrEmpty)((0, graphql_mapping_template_1.ref)('args.filter'))), (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('filter'), (0, graphql_mapping_template_1.ref)('args.filter')))), (0, graphql_mapping_template_1.iff)((0, graphql_mapping_template_1.not)((0, graphql_mapping_template_1.isNullOrEmpty)((0, graphql_mapping_template_1.ref)('filter'))), (0, graphql_mapping_template_1.compoundExpression)([ (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)(`filterExpression`), (0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('util.parseJson'), (0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('util.transform.toDynamoDBFilterExpression'), (0, graphql_mapping_template_1.ref)('filter')))), (0, graphql_mapping_template_1.iff)((0, graphql_mapping_template_1.not)((0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('util.isNullOrBlank'), (0, graphql_mapping_template_1.ref)('filterExpression.expression'))), (0, graphql_mapping_template_1.compoundExpression)([ (0, graphql_mapping_template_1.iff)((0, graphql_mapping_template_1.equals)((0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('filterExpression.expressionValues.size')), (0, graphql_mapping_template_1.int)(0)), (0, graphql_mapping_template_1.qref)((0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('filterExpression.remove'), (0, graphql_mapping_template_1.str)('expressionValues')))), (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)('filter'), (0, graphql_mapping_template_1.ref)('filterExpression')), ])), ])), (0, graphql_mapping_template_1.ifElse)((0, graphql_mapping_template_1.not)((0, graphql_mapping_template_1.ref)('useScan')), (0, graphql_mapping_template_1.compoundExpression)([ (0, graphql_mapping_template_1.iff)((0, graphql_mapping_template_1.ref)(`${requestVariable}.filter`), (0, graphql_mapping_template_1.set)((0, graphql_mapping_template_1.ref)(`${requestVariable}.filter`), (0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('util.parseJson'), (0, graphql_mapping_template_1.methodCall)((0, graphql_mapping_template_1.ref)('util.transform.toDynamoDBFilterExpression'), (0, graphql_mapping_template_1.ref)(`${requestVariable}.filter`))))), (0, graphql_mapping_template_1.raw)(`$util.toJson($${requestVariable})`), ]), (0, graphql_mapping_template_1.obj)({ version: (0, graphql_mapping_template_1.str)('2018-05-29'), operation: (0, graphql_mapping_template_1.str)('Sync'), filter: (0, graphql_mapping_template_1.ifElse)((0, graphql_mapping_template_1.ref)('filter'), (0, graphql_mapping_template_1.ref)('util.toJson($filter)'), (0, graphql_mapping_template_1.nul)()), limit: (0, graphql_mapping_template_1.ref)(`util.defaultIfNull($args.limit, ${graphql_transformer_common_1.ResourceConstants.DEFAULT_SYNC_QUERY_PAGE_LIMIT})`), lastSync: (0, graphql_mapping_template_1.ref)('util.toJson($util.defaultIfNull($args.lastSync, null))'), nextToken: (0, graphql_mapping_template_1.ref)('util.toJson($util.defaultIfNull($args.nextToken, null))'), })), ])); }; exports.generateSyncRequestTemplate = generateSyncRequestTemplate; //# sourceMappingURL=query.js.map