From d25be7fd524633d4cc0f0c063448672ccbc3ae2d Mon Sep 17 00:00:00 2001 From: Thorsten Hoeger Date: Thu, 6 May 2021 00:00:49 +0200 Subject: [PATCH] feat(graphql): make VTL for different datasources possible --- src/constructs/graphql-api.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/constructs/graphql-api.ts b/src/constructs/graphql-api.ts index 86fe9b4..94eb0fc 100644 --- a/src/constructs/graphql-api.ts +++ b/src/constructs/graphql-api.ts @@ -229,6 +229,13 @@ export class GraphQlApi extends cdk.Construct { } public addDynamoDbVtlResolver(typeName: TYPE['__typename'], fieldName: keyof Omit): void { + if (!this.tableDataSource) { + throw new Error('DynamoDB is not initialized'); + } + this.addVtlResolver(typeName, fieldName, this.tableDataSource); + } + + public addVtlResolver(typeName: TYPE['__typename'], fieldName: keyof Omit, dataSource: appsync.BaseDataSource): void { const operationId = `${typeName}.${fieldName}`; const mappingReqFile = `./src/vtl/${operationId}.req.vm`; @@ -244,7 +251,7 @@ export class GraphQlApi extends cdk.Construct { api: this.api, typeName, fieldName: fieldName as string, - dataSource: this.tableDataSource, + dataSource, requestMappingTemplate: appsync.MappingTemplate.fromFile(mappingReqFile), responseMappingTemplate: appsync.MappingTemplate.fromFile(mappingResFile), });