Repository: sid88in/serverless-appsync-plugin
Pull request #461 (2022-01-13 21:34:56)

Support MaxBatchSize


Patch:
diff --git a/src/resources/PipelineFunction.ts b/src/resources/PipelineFunction.ts
index 556e17a..6648517 100644
--- a/src/resources/PipelineFunction.ts
+++ b/src/resources/PipelineFunction.ts
@@ -32,6 +32,7 @@ export class PipelineFunction {
       DataSourceName: { 'Fn::GetAtt': [logicalIdDataSource, 'Name'] },
       Description: this.config.description,
       FunctionVersion: '2018-05-29',
+      MaxBatchSize: this.config.maxBatchSize,
     };
 
     const requestMappingTemplates = this.resolveMappingTemplate('request');
diff --git a/src/resources/Resolver.ts b/src/resources/Resolver.ts
index cebeb1b..cf6e732 100644
--- a/src/resources/Resolver.ts
+++ b/src/resources/Resolver.ts
@@ -98,6 +98,7 @@ export class Resolver {
         ...Properties,
         Kind: 'UNIT',
         DataSourceName: { 'Fn::GetAtt': [logicalIdDataSource, 'Name'] },
+        MaxBatchSize: this.config.maxBatchSize,
       };
     }
 
diff --git a/src/types/cloudFormation.ts b/src/types/cloudFormation.ts
index 002d439..ff11f5c 100644
--- a/src/types/cloudFormation.ts
+++ b/src/types/cloudFormation.ts
@@ -106,6 +106,7 @@ export type CfnResolver = {
         LambdaConflictHandlerArn: string | IntrinsicFunction;
       };
     };
+    MaxBatchSize?: number;
   };
 };
 
@@ -119,6 +120,7 @@ export type CfnFunctionResolver = {
     FunctionVersion?: string;
     RequestMappingTemplate?: string | IntrinsicFunction;
     ResponseMappingTemplate?: string | IntrinsicFunction;
+    MaxBatchSize?: number;
   };
 };
 
diff --git a/src/types/plugin.ts b/src/types/plugin.ts
index 70c0ba4..d451ba2 100644
--- a/src/types/plugin.ts
+++ b/src/types/plugin.ts
@@ -175,6 +175,7 @@ export type ResolverConfig = UnitResolverConfig | PipelineResolverConfig;
 export type UnitResolverConfig = BaseResolverConfig & {
   kind?: 'UNIT';
   dataSource: string;
+  maxBatchSize?: number;
 };
 
 export type PipelineResolverConfig = BaseResolverConfig & {
@@ -190,6 +191,7 @@ export type PipelineFunctionConfig = {
   description?: string;
   request?: string | false;
   response?: string | false;
+  maxBatchSize?: number;
   substitutions?: Substitutions;
 };
 
diff --git a/src/validation.ts b/src/validation.ts
index f29d3ad..3396f9b 100644
--- a/src/validation.ts
+++ b/src/validation.ts
@@ -276,6 +276,7 @@ export const appSyncSchema = {
         },
         type: { type: 'string' },
         field: { type: 'string' },
+        maxBatchSize: { type: 'number', minimum: 1, maximum: 2000 },
         dataSource: { $ref: '#/definitions/dataSource' },
         functions: { type: 'array', items: { type: 'string' } },
         request: { $ref: '#/definitions/mappingTemplate' },
@@ -329,6 +330,7 @@ export const appSyncSchema = {
         description: { type: 'string' },
         request: { $ref: '#/definitions/mappingTemplate' },
         response: { $ref: '#/definitions/mappingTemplate' },
+        maxBatchSize: { type: 'number', minimum: 1, maximum: 2000 },
         substitutions: { $ref: '#/definitions/substitutions' },
       },
       required: ['dataSource'],


Test patch:
diff --git a/src/__tests__/__snapshots__/resolvers.test.ts.snap b/src/__tests__/__snapshots__/resolvers.test.ts.snap
index 5be5c4f..2efee50 100644
--- a/src/__tests__/__snapshots__/resolvers.test.ts.snap
+++ b/src/__tests__/__snapshots__/resolvers.test.ts.snap
@@ -101,6 +101,7 @@ Object {
       },
       "FieldName": "user",
       "Kind": "UNIT",
+      "MaxBatchSize": undefined,
       "RequestMappingTemplate": "Content of path/to/mappingTemplates/Query.user.request.vtl",
       "ResponseMappingTemplate": "Content of path/to/mappingTemplates/Query.user.response.vtl",
       "SyncConfig": Object {
diff --git a/src/__tests__/resolvers.test.ts b/src/__tests__/resolvers.test.ts
index b2a2dd0..dcfe140 100644
--- a/src/__tests__/resolvers.test.ts
+++ b/src/__tests__/resolvers.test.ts
@@ -63,6 +63,60 @@ describe('Resolvers', () => {
               },
               "FieldName": "user",
               "Kind": "UNIT",
+              "MaxBatchSize": undefined,
+              "RequestMappingTemplate": "Content of path/to/mappingTemplates/Query.user.request.vtl",
+              "ResponseMappingTemplate": "Content of path/to/mappingTemplates/Query.user.response.vtl",
+              "TypeName": "Query",
+            },
+            "Type": "AWS::AppSync::Resolver",
+          },
+        }
+      `);
+    });
+
+    it('should generate Resources with maxBatchSize', () => {
+      const api = new Api(
+        given.appSyncConfig({
+          dataSources: {
+            myFunction: {
+              name: 'myFunction',
+              type: 'AWS_LAMBDA',
+              config: { functionName: 'myFunction' },
+            },
+          },
+        }),
+        plugin,
+      );
+      expect(
+        api.compileResolver({
+          dataSource: 'myFunction',
+          kind: 'UNIT',
+          type: 'Query',
+          field: 'user',
+          maxBatchSize: 200,
+        }),
+      ).toMatchInlineSnapshot(`
+        Object {
+          "GraphQlResolverQueryuser": Object {
+            "DependsOn": Array [
+              "GraphQlSchema",
+            ],
+            "Properties": Object {
+              "ApiId": Object {
+                "Fn::GetAtt": Array [
+                  "GraphQlApi",
+                  "ApiId",
+                ],
+              },
+              "DataSourceName": Object {
+                "Fn::GetAtt": Array [
+                  "GraphQlDsmyFunction",
+                  "Name",
+                ],
+              },
+              "FieldName": "user",
+              "Kind": "UNIT",
+              "MaxBatchSize": 200,
               "RequestMappingTemplate": "Content of path/to/mappingTemplates/Query.user.request.vtl",
               "ResponseMappingTemplate": "Content of path/to/mappingTemplates/Query.user.response.vtl",
               "TypeName": "Query",
@@ -116,6 +170,7 @@ describe('Resolvers', () => {
               },
               "FieldName": "user",
               "Kind": "UNIT",
+              "MaxBatchSize": undefined,
               "RequestMappingTemplate": "Content of path/to/mappingTemplates/specific.request.tpl",
               "ResponseMappingTemplate": "Content of path/to/mappingTemplates/specific.response.tpl",
               "TypeName": "Query",
@@ -169,6 +224,7 @@ describe('Resolvers', () => {
               },
               "FieldName": "user",
               "Kind": "UNIT",
+              "MaxBatchSize": undefined,
               "TypeName": "Query",
             },
             "Type": "AWS::AppSync::Resolver",
@@ -367,6 +423,56 @@ describe('Resolvers', () => {
               },
               "Description": "Function1 Pipeline Resolver",
               "FunctionVersion": "2018-05-29",
+              "MaxBatchSize": undefined,
+              "Name": "function1",
+              "RequestMappingTemplate": "Content of path/to/mappingTemplates/function1.request.vtl",
+              "ResponseMappingTemplate": "Content of path/to/mappingTemplates/function1.response.vtl",
+            },
+            "Type": "AWS::AppSync::FunctionConfiguration",
+          },
+        }
+      `);
+    });
+
+    it('should generate Pipeline Function Resources with maxBatchSize', () => {
+      const api = new Api(
+        given.appSyncConfig({
+          dataSources: {
+            myFunction: {
+              name: 'myFunction',
+              type: 'AWS_LAMBDA',
+              config: { functionName: 'myFunction' },
+            },
+          },
+        }),
+        plugin,
+      );
+      expect(
+        api.compilePipelineFunctionResource({
+          name: 'function1',
+          dataSource: 'myFunction',
+          description: 'Function1 Pipeline Resolver',
+          maxBatchSize: 200,
+        }),
+      ).toMatchInlineSnapshot(`
+        Object {
+          "GraphQlFunctionConfigurationfunction1": Object {
+            "Properties": Object {
+              "ApiId": Object {
+                "Fn::GetAtt": Array [
+                  "GraphQlApi",
+                  "ApiId",
+                ],
+              },
+              "DataSourceName": Object {
+                "Fn::GetAtt": Array [
+                  "GraphQlDsmyFunction",
+                  "Name",
+                ],
+              },
+              "Description": "Function1 Pipeline Resolver",
+              "FunctionVersion": "2018-05-29",
+              "MaxBatchSize": 200,
               "Name": "function1",
               "RequestMappingTemplate": "Content of path/to/mappingTemplates/function1.request.vtl",
               "ResponseMappingTemplate": "Content of path/to/mappingTemplates/function1.response.vtl",
@@ -416,6 +522,7 @@ describe('Resolvers', () => {
               },
               "Description": "Function1 Pipeline Resolver",
               "FunctionVersion": "2018-05-29",
+              "MaxBatchSize": undefined,
               "Name": "function1",
               "RequestMappingTemplate": "Content of path/to/mappingTemplates/specific.request.tpl",
               "ResponseMappingTemplate": "Content of path/to/mappingTemplates/specific.response.tpl",
@@ -465,6 +572,7 @@ describe('Resolvers', () => {
               },
               "Description": "Function1 Pipeline Resolver",
               "FunctionVersion": "2018-05-29",
+              "MaxBatchSize": undefined,
               "Name": "function1",
             },
             "Type": "AWS::AppSync::FunctionConfiguration",
@@ -543,6 +651,7 @@ describe('Resolvers', () => {
               },
               "FieldName": "user",
               "Kind": "UNIT",
+              "MaxBatchSize": undefined,
               "RequestMappingTemplate": "Content of path/to/mappingTemplates/Query.user.request.vtl",
               "ResponseMappingTemplate": "Content of path/to/mappingTemplates/Query.user.response.vtl",
               "TypeName": "Query",
@@ -608,6 +717,7 @@ describe('Resolvers', () => {
               },
               "FieldName": "user",
               "Kind": "UNIT",
+              "MaxBatchSize": undefined,
               "RequestMappingTemplate": "Content of path/to/mappingTemplates/Query.user.request.vtl",
               "ResponseMappingTemplate": "Content of path/to/mappingTemplates/Query.user.response.vtl",
               "TypeName": "Query",
diff --git a/src/__tests__/validation/__snapshots__/pipelineFunctions.test.ts.snap b/src/__tests__/validation/__snapshots__/pipelineFunctions.test.ts.snap
index 22d2e20..6b910f3 100644
--- a/src/__tests__/validation/__snapshots__/pipelineFunctions.test.ts.snap
+++ b/src/__tests__/validation/__snapshots__/pipelineFunctions.test.ts.snap
@@ -5,6 +5,7 @@ exports[`Basic Invalid should validate: Invalid 1`] = `
 /pipelineFunctions/function1/description: must be string
 /pipelineFunctions/function1/request: must be a string or false
 /pipelineFunctions/function1/response: must be a string or false
+/pipelineFunctions/function1/maxBatchSize: must be <= 2000
 /pipelineFunctions: contains invalid pipeline function definitions"
 `;
 
diff --git a/src/__tests__/validation/__snapshots__/resolvers.test.ts.snap b/src/__tests__/validation/__snapshots__/resolvers.test.ts.snap
index 25ee58e..71e3578 100644
--- a/src/__tests__/validation/__snapshots__/resolvers.test.ts.snap
+++ b/src/__tests__/validation/__snapshots__/resolvers.test.ts.snap
@@ -4,6 +4,7 @@ exports[`Basic Invalid should validate: Invalid 1`] = `
 "/resolvers/myResolver/kind: must be \\"UNIT\\" or \\"PIPELINE\\"
 /resolvers/myResolver/type: must be string
 /resolvers/myResolver/field: must be string
+/resolvers/myResolver/maxBatchSize: must be <= 2000
 /resolvers/myResolver/dataSource: must be a string or data source definition
 /resolvers/myResolver/request: must be a string or false
 /resolvers/myResolver/response: must be a string or false
diff --git a/src/__tests__/validation/pipelineFunctions.test.ts b/src/__tests__/validation/pipelineFunctions.test.ts
index 981fe8d..3f113d3 100644
--- a/src/__tests__/validation/pipelineFunctions.test.ts
+++ b/src/__tests__/validation/pipelineFunctions.test.ts
@@ -15,6 +15,7 @@ describe('Basic', () => {
             function2: {
               description: 'My Function',
               dataSource: 'ds1',
+              maxBatchSize: 200,
               request: 'request.vtl',
               response: 'response.vtl',
             },
@@ -72,6 +73,7 @@ describe('Basic', () => {
               dataSource: 789,
               request: 123,
               response: 456,
+              maxBatchSize: 5000,
             },
           },
         },
diff --git a/src/__tests__/validation/resolvers.test.ts b/src/__tests__/validation/resolvers.test.ts
index 3ddcdad..6ef41da 100644
--- a/src/__tests__/validation/resolvers.test.ts
+++ b/src/__tests__/validation/resolvers.test.ts
@@ -30,6 +30,7 @@ describe('Basic', () => {
                 conflictHandler: 'LAMBDA',
                 function: { handler: 'index.handler' },
               },
+              maxBatchSize: 200,
             },
             getPosts: {
               type: 'Query',
@@ -126,6 +127,7 @@ describe('Basic', () => {
               field: 456,
               request: 123,
               response: 456,
+              maxBatchSize: 5000,
             },
           },
         },