@@ -7,6 +7,16 @@ import { mergeTypeDefs } from '@graphql-tools/merge';
77import * as globby from 'globby' ;
88import directLambdaRequest from './templates/direct-lambda.request.vtl' ;
99import directLambdaResponse from './templates/direct-lambda.response.vtl' ;
10+ import {
11+ DEFAULT_MAPPING_TEMPLATE_LOCATION ,
12+ DEFAULT_ENCODING ,
13+ DEFAULT_SCHEMA_FILE ,
14+ DEFAULT_HTTP_METHOD ,
15+ DEFAULT_RESOLVER_TYPE ,
16+ HTTPMessage ,
17+ MappingTemplateType ,
18+ SourceType ,
19+ } from './constants' ;
1020
1121const directLambdaMappingTemplates = {
1222 request : directLambdaRequest ,
@@ -24,15 +34,32 @@ export default function getAppSyncConfig(context, appSyncConfig) {
2434
2535 const mappingTemplatesLocation = path . join (
2636 context . serverless . config . servicePath ,
27- cfg . mappingTemplatesLocation || 'mapping-templates' ,
37+ cfg . mappingTemplatesLocation || DEFAULT_MAPPING_TEMPLATE_LOCATION ,
38+ ) ;
39+
40+ const functionConfigurationsLocation = path . join (
41+ context . serverless . config . servicePath ,
42+ cfg . functionConfigurationsLocation || DEFAULT_MAPPING_TEMPLATE_LOCATION ,
2843 ) ;
2944
3045 const { defaultMappingTemplates = { } } = cfg ;
3146
32- const getMappingTemplate = ( filePath ) => {
33- return fs . readFileSync ( path . join ( mappingTemplatesLocation , filePath ) , {
34- encoding : 'utf8' ,
35- } ) ;
47+ const getMappingTemplate = ( filePath , type ) => {
48+ switch ( type ) {
49+ case MappingTemplateType . MAPPING_TEMPLATE :
50+ return fs . readFileSync ( path . join ( mappingTemplatesLocation , filePath ) , {
51+ encoding : DEFAULT_ENCODING ,
52+ } ) ;
53+ case MappingTemplateType . FUNCTION_CONFIGURATION :
54+ return fs . readFileSync (
55+ path . join ( functionConfigurationsLocation , filePath ) ,
56+ {
57+ encoding : DEFAULT_ENCODING ,
58+ } ,
59+ ) ;
60+ default :
61+ return null ;
62+ }
3663 } ;
3764
3865 const toAbsolutePosixPath = ( basePath , filePath ) =>
@@ -58,7 +85,7 @@ export default function getAppSyncConfig(context, appSyncConfig) {
5885 const getFileMap = ( basePath , filePath ) => ( {
5986 path : filePath ,
6087 content : fs . readFileSync ( toAbsolutePosixPath ( basePath , filePath ) , {
61- encoding : 'utf8' ,
88+ encoding : DEFAULT_ENCODING ,
6289 } ) ,
6390 } ) ;
6491
@@ -73,7 +100,7 @@ export default function getAppSyncConfig(context, appSyncConfig) {
73100 } ;
74101
75102 switch ( source . type ) {
76- case ' AMAZON_DYNAMODB' : {
103+ case SourceType . AMAZON_DYNAMODB : {
77104 return {
78105 ...dataSource ,
79106 config : {
@@ -82,13 +109,13 @@ export default function getAppSyncConfig(context, appSyncConfig) {
82109 } ,
83110 } ;
84111 }
85- case ' RELATIONAL_DATABASE' : {
112+ case SourceType . RELATIONAL_DATABASE : {
86113 return {
87114 ...dataSource ,
88115 rds : context . options . rds ,
89116 } ;
90117 }
91- case ' AWS_LAMBDA' : {
118+ case SourceType . AWS_LAMBDA : {
92119 const { functionName } = source . config ;
93120 if ( functionName === undefined ) {
94121 context . plugin . log ( `${ source . name } does not have a functionName` , {
@@ -121,7 +148,7 @@ export default function getAppSyncConfig(context, appSyncConfig) {
121148 invoke : async ( payload ) => {
122149 const result = await axios . request ( {
123150 url,
124- method : method || 'POST' ,
151+ method : method || DEFAULT_HTTP_METHOD ,
125152 data : payload ,
126153 headers : payload ?. request ?. headers ,
127154 validateStatus : false ,
@@ -156,8 +183,8 @@ export default function getAppSyncConfig(context, appSyncConfig) {
156183 } ,
157184 } ;
158185 }
159- case ' AMAZON_ELASTICSEARCH' :
160- case ' HTTP' : {
186+ case SourceType . AMAZON_ELASTICSEARCH :
187+ case SourceType . HTTP : {
161188 return {
162189 ...dataSource ,
163190 endpoint : source . config . endpoint ,
@@ -168,7 +195,7 @@ export default function getAppSyncConfig(context, appSyncConfig) {
168195 }
169196 } ;
170197
171- const makeMappingTemplate = ( resolver , type ) => {
198+ const makeMappingTemplate = ( resolver , type , templateType ) => {
172199 const { name, type : parent , field, substitutions = { } } = resolver ;
173200
174201 const defaultTemplatePrefix = name || `${ parent } .${ field } ` ;
@@ -185,7 +212,7 @@ export default function getAppSyncConfig(context, appSyncConfig) {
185212 if ( templatePath === false ) {
186213 mappingTemplate = directLambdaMappingTemplates [ type ] ;
187214 } else {
188- mappingTemplate = getMappingTemplate ( templatePath ) ;
215+ mappingTemplate = getMappingTemplate ( templatePath , templateType ) ;
189216 // Substitutions
190217 const allSubstitutions = { ...cfg . substitutions , ...substitutions } ;
191218 forEach ( allSubstitutions , ( value , variable ) => {
@@ -198,23 +225,43 @@ export default function getAppSyncConfig(context, appSyncConfig) {
198225 } ;
199226
200227 const makeResolver = ( resolver ) => {
228+ let templateType = MappingTemplateType . MAPPING_TEMPLATE ;
201229 return {
202- kind : resolver . kind || 'UNIT' ,
230+ kind : resolver . kind || DEFAULT_RESOLVER_TYPE ,
203231 fieldName : resolver . field ,
204232 typeName : resolver . type ,
205233 dataSourceName : resolver . dataSource ,
206234 functions : resolver . functions ,
207- requestMappingTemplate : makeMappingTemplate ( resolver , 'request' ) ,
208- responseMappingTemplate : makeMappingTemplate ( resolver , 'response' ) ,
235+ requestMappingTemplate : makeMappingTemplate (
236+ resolver ,
237+ HTTPMessage . REQUEST ,
238+ templateType ,
239+ ) ,
240+ responseMappingTemplate : makeMappingTemplate (
241+ resolver ,
242+ HTTPMessage . RESPONSE ,
243+ templateType ,
244+ ) ,
209245 } ;
210246 } ;
211247
212- const makeFunctionConfiguration = ( config ) => ( {
213- dataSourceName : config . dataSource ,
214- name : config . name ,
215- requestMappingTemplate : makeMappingTemplate ( config , 'request' ) ,
216- responseMappingTemplate : makeMappingTemplate ( config , 'response' ) ,
217- } ) ;
248+ const makeFunctionConfiguration = ( config ) => {
249+ let templateType = MappingTemplateType . FUNCTION_CONFIGURATION ;
250+ return {
251+ dataSourceName : config . dataSource ,
252+ name : config . name ,
253+ requestMappingTemplate : makeMappingTemplate (
254+ config ,
255+ HTTPMessage . REQUEST ,
256+ templateType ,
257+ ) ,
258+ responseMappingTemplate : makeMappingTemplate (
259+ config ,
260+ HTTPMessage . RESPONSE ,
261+ templateType ,
262+ ) ,
263+ } ;
264+ } ;
218265
219266 const makeAuthType = ( authType ) => {
220267 const auth = {
@@ -247,7 +294,7 @@ export default function getAppSyncConfig(context, appSyncConfig) {
247294 // Load the schema. If multiple provided, merge them
248295 const schemaPaths = Array . isArray ( cfg . schema )
249296 ? cfg . schema
250- : [ cfg . schema || 'schema.graphql' ] ;
297+ : [ cfg . schema || DEFAULT_SCHEMA_FILE ] ;
251298 const basePath = context . serverless . config . servicePath ;
252299 const schemas = globFilePaths ( basePath , schemaPaths ) . map ( ( schemaPath ) =>
253300 getFileMap ( basePath , schemaPath ) ,
0 commit comments