@@ -7,6 +7,16 @@ import { mergeTypeDefs } from '@graphql-tools/merge';
7
7
import * as globby from 'globby' ;
8
8
import directLambdaRequest from './templates/direct-lambda.request.vtl' ;
9
9
import 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' ;
10
20
11
21
const directLambdaMappingTemplates = {
12
22
request : directLambdaRequest ,
@@ -24,15 +34,32 @@ export default function getAppSyncConfig(context, appSyncConfig) {
24
34
25
35
const mappingTemplatesLocation = path . join (
26
36
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 ,
28
43
) ;
29
44
30
45
const { defaultMappingTemplates = { } } = cfg ;
31
46
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
+ }
36
63
} ;
37
64
38
65
const toAbsolutePosixPath = ( basePath , filePath ) =>
@@ -58,7 +85,7 @@ export default function getAppSyncConfig(context, appSyncConfig) {
58
85
const getFileMap = ( basePath , filePath ) => ( {
59
86
path : filePath ,
60
87
content : fs . readFileSync ( toAbsolutePosixPath ( basePath , filePath ) , {
61
- encoding : 'utf8' ,
88
+ encoding : DEFAULT_ENCODING ,
62
89
} ) ,
63
90
} ) ;
64
91
@@ -73,7 +100,7 @@ export default function getAppSyncConfig(context, appSyncConfig) {
73
100
} ;
74
101
75
102
switch ( source . type ) {
76
- case ' AMAZON_DYNAMODB' : {
103
+ case SourceType . AMAZON_DYNAMODB : {
77
104
return {
78
105
...dataSource ,
79
106
config : {
@@ -82,13 +109,13 @@ export default function getAppSyncConfig(context, appSyncConfig) {
82
109
} ,
83
110
} ;
84
111
}
85
- case ' RELATIONAL_DATABASE' : {
112
+ case SourceType . RELATIONAL_DATABASE : {
86
113
return {
87
114
...dataSource ,
88
115
rds : context . options . rds ,
89
116
} ;
90
117
}
91
- case ' AWS_LAMBDA' : {
118
+ case SourceType . AWS_LAMBDA : {
92
119
const { functionName } = source . config ;
93
120
if ( functionName === undefined ) {
94
121
context . plugin . log ( `${ source . name } does not have a functionName` , {
@@ -121,7 +148,7 @@ export default function getAppSyncConfig(context, appSyncConfig) {
121
148
invoke : async ( payload ) => {
122
149
const result = await axios . request ( {
123
150
url,
124
- method : method || 'POST' ,
151
+ method : method || DEFAULT_HTTP_METHOD ,
125
152
data : payload ,
126
153
headers : payload ?. request ?. headers ,
127
154
validateStatus : false ,
@@ -156,8 +183,8 @@ export default function getAppSyncConfig(context, appSyncConfig) {
156
183
} ,
157
184
} ;
158
185
}
159
- case ' AMAZON_ELASTICSEARCH' :
160
- case ' HTTP' : {
186
+ case SourceType . AMAZON_ELASTICSEARCH :
187
+ case SourceType . HTTP : {
161
188
return {
162
189
...dataSource ,
163
190
endpoint : source . config . endpoint ,
@@ -168,7 +195,7 @@ export default function getAppSyncConfig(context, appSyncConfig) {
168
195
}
169
196
} ;
170
197
171
- const makeMappingTemplate = ( resolver , type ) => {
198
+ const makeMappingTemplate = ( resolver , type , templateType ) => {
172
199
const { name, type : parent , field, substitutions = { } } = resolver ;
173
200
174
201
const defaultTemplatePrefix = name || `${ parent } .${ field } ` ;
@@ -185,7 +212,7 @@ export default function getAppSyncConfig(context, appSyncConfig) {
185
212
if ( templatePath === false ) {
186
213
mappingTemplate = directLambdaMappingTemplates [ type ] ;
187
214
} else {
188
- mappingTemplate = getMappingTemplate ( templatePath ) ;
215
+ mappingTemplate = getMappingTemplate ( templatePath , templateType ) ;
189
216
// Substitutions
190
217
const allSubstitutions = { ...cfg . substitutions , ...substitutions } ;
191
218
forEach ( allSubstitutions , ( value , variable ) => {
@@ -198,23 +225,43 @@ export default function getAppSyncConfig(context, appSyncConfig) {
198
225
} ;
199
226
200
227
const makeResolver = ( resolver ) => {
228
+ let templateType = MappingTemplateType . MAPPING_TEMPLATE ;
201
229
return {
202
- kind : resolver . kind || 'UNIT' ,
230
+ kind : resolver . kind || DEFAULT_RESOLVER_TYPE ,
203
231
fieldName : resolver . field ,
204
232
typeName : resolver . type ,
205
233
dataSourceName : resolver . dataSource ,
206
234
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
+ ) ,
209
245
} ;
210
246
} ;
211
247
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
+ } ;
218
265
219
266
const makeAuthType = ( authType ) => {
220
267
const auth = {
@@ -247,7 +294,7 @@ export default function getAppSyncConfig(context, appSyncConfig) {
247
294
// Load the schema. If multiple provided, merge them
248
295
const schemaPaths = Array . isArray ( cfg . schema )
249
296
? cfg . schema
250
- : [ cfg . schema || 'schema.graphql' ] ;
297
+ : [ cfg . schema || DEFAULT_SCHEMA_FILE ] ;
251
298
const basePath = context . serverless . config . servicePath ;
252
299
const schemas = globFilePaths ( basePath , schemaPaths ) . map ( ( schemaPath ) =>
253
300
getFileMap ( basePath , schemaPath ) ,
0 commit comments