3
3
} from 'amplify-appsync-simulator' ;
4
4
import { invoke } from 'amplify-util-mock/lib/utils/lambda/invoke' ;
5
5
import fs from 'fs' ;
6
- import { find } from 'lodash' ;
6
+ import { forEach } from 'lodash' ;
7
7
import path from 'path' ;
8
8
9
9
export default function getAppSyncConfig ( context , appSyncConfig ) {
@@ -15,8 +15,8 @@ export default function getAppSyncConfig(context, appSyncConfig) {
15
15
dataSources : ( appSyncConfig . dataSources || [ ] ) . flat ( ) ,
16
16
} ;
17
17
18
- const getFileMap = ( basePath , filePath ) => ( {
19
- path : filePath ,
18
+ const getFileMap = ( basePath , filePath , substitutionPath = null ) => ( {
19
+ path : substitutionPath || filePath ,
20
20
content : fs . readFileSync ( path . join ( basePath , filePath ) , { encoding : 'utf8' } ) ,
21
21
} ) ;
22
22
@@ -94,21 +94,26 @@ export default function getAppSyncConfig(context, appSyncConfig) {
94
94
}
95
95
} ;
96
96
97
+ const getDefaultTemplatePrefix = ( template ) => {
98
+ const { name, type, field } = template ;
99
+ return name ? `${ name } ` : `${ type } .${ field } ` ;
100
+ } ;
101
+
97
102
const makeResolver = ( resolver ) => ( {
98
103
kind : resolver . kind || 'UNIT' ,
99
104
fieldName : resolver . field ,
100
105
typeName : resolver . type ,
101
106
dataSourceName : resolver . dataSource ,
102
107
functions : resolver . functions ,
103
- requestMappingTemplateLocation : resolver . request || `${ resolver . type } . ${ resolver . field } .request.vtl` ,
104
- responseMappingTemplateLocation : resolver . response || `${ resolver . type } . ${ resolver . field } .response.vtl` ,
108
+ requestMappingTemplateLocation : `${ getDefaultTemplatePrefix ( resolver ) } .request.vtl` ,
109
+ responseMappingTemplateLocation : `${ getDefaultTemplatePrefix ( resolver ) } .response.vtl` ,
105
110
} ) ;
106
111
107
112
const makeFunctionConfiguration = ( functionConfiguration ) => ( {
108
113
dataSourceName : functionConfiguration . dataSource ,
109
114
name : functionConfiguration . name ,
110
- requestMappingTemplateLocation : functionConfiguration . request ,
111
- responseMappingTemplateLocation : functionConfiguration . response ,
115
+ requestMappingTemplateLocation : ` ${ getDefaultTemplatePrefix ( functionConfiguration ) } .request.vtl` ,
116
+ responseMappingTemplateLocation : ` ${ getDefaultTemplatePrefix ( functionConfiguration ) } .response.vtl` ,
112
117
} ) ;
113
118
114
119
const makeAuthType = ( authType ) => {
@@ -143,23 +148,42 @@ export default function getAppSyncConfig(context, appSyncConfig) {
143
148
cfg . mappingTemplatesLocation || 'mapping-templates' ,
144
149
) ;
145
150
151
+ const makeMappingTemplate = ( filePath , substitutionPath = null , substitutions = { } ) => {
152
+ const mapping = getFileMap ( mappingTemplatesLocation , filePath , substitutionPath ) ;
153
+
154
+ forEach ( substitutions , ( value , variable ) => {
155
+ const regExp = new RegExp ( `\\$\{?${ variable } }?` , 'g' ) ;
156
+ mapping . content = mapping . content . replace ( regExp , value ) ;
157
+ } ) ;
158
+
159
+ return mapping ;
160
+ } ;
161
+
146
162
const makeMappingTemplates = ( config ) => {
147
163
const sources = [ ] . concat (
148
164
config . mappingTemplates ,
149
165
config . functionConfigurations ,
150
166
) ;
151
167
152
168
return sources . reduce ( ( acc , template ) => {
153
- const requestTemplate = template . request || `${ template . type } .${ template . field } .request.vtl` ;
154
- if ( ! find ( acc , ( e ) => e . path === requestTemplate ) ) {
155
- acc . push ( getFileMap ( mappingTemplatesLocation , requestTemplate ) ) ;
156
- }
157
- const responseTemplate = template . response || `${ template . type } .${ template . field } .response.vtl` ;
158
- if ( ! find ( acc , ( e ) => e . path === responseTemplate ) ) {
159
- acc . push ( getFileMap ( mappingTemplatesLocation , responseTemplate ) ) ;
160
- }
161
-
162
- return acc ;
169
+ const {
170
+ substitutions = { } ,
171
+ request,
172
+ response,
173
+ } = template ;
174
+
175
+ const defaultTemplatePrefix = getDefaultTemplatePrefix ( template ) ;
176
+
177
+ const requestTemplate = request || `${ defaultTemplatePrefix } .request.vtl` ;
178
+ const responseTemplate = response || `${ defaultTemplatePrefix } .response.vtl` ;
179
+
180
+ // Substitutions
181
+ const allSubstitutions = { ...config . substitutions , ...substitutions } ;
182
+ return [
183
+ ...acc ,
184
+ makeMappingTemplate ( requestTemplate , `${ defaultTemplatePrefix } .request.vtl` , allSubstitutions ) ,
185
+ makeMappingTemplate ( responseTemplate , `${ defaultTemplatePrefix } .response.vtl` , allSubstitutions ) ,
186
+ ] ;
163
187
} , [ ] ) ;
164
188
} ;
165
189
0 commit comments