@@ -3,10 +3,18 @@ import {
33} from 'amplify-appsync-simulator' ;
44import { invoke } from 'amplify-util-mock/lib/utils/lambda/invoke' ;
55import fs from 'fs' ;
6- import { find } from 'lodash' ;
6+ import { find , get } from 'lodash' ;
77import path from 'path' ;
88
99export default function getAppSyncConfig ( context , appSyncConfig ) {
10+ // Flattening params
11+ const cfg = {
12+ ...appSyncConfig ,
13+ mappingTemplates : ( appSyncConfig . mappingTemplates || [ ] ) . flat ( ) ,
14+ functionConfigurations : ( appSyncConfig . functionConfigurations || [ ] ) . flat ( ) ,
15+ dataSources : ( appSyncConfig . dataSources || [ ] ) . flat ( ) ,
16+ } ;
17+
1018 const getFileMap = ( basePath , filePath ) => ( {
1119 path : filePath ,
1220 content : fs . readFileSync ( path . join ( basePath , filePath ) , { encoding : 'utf8' } ) ,
@@ -22,6 +30,22 @@ export default function getAppSyncConfig(context, appSyncConfig) {
2230 type : source . type ,
2331 } ;
2432
33+ /**
34+ * Returns the tableName resolving reference. Throws exception if reference is not found
35+ */
36+ const getTableName = ( table ) => {
37+ if ( table && table . Ref ) {
38+ const tableName = get ( context . serverless . service , `resources.Resources.${ table . Ref } .Properties.TableName` ) ;
39+
40+ if ( ! tableName ) {
41+ throw new Error ( `Unable to find table Reference for ${ table } inside Serverless resources` ) ;
42+ }
43+
44+ return tableName ;
45+ }
46+ return table ;
47+ } ;
48+
2549 switch ( source . type ) {
2650 case 'AMAZON_DYNAMODB' : {
2751 const { port } = context . options . dynamoDb ;
@@ -30,7 +54,7 @@ export default function getAppSyncConfig(context, appSyncConfig) {
3054 config : {
3155 endpoint : `http://localhost:${ port } ` ,
3256 region : 'localhost' ,
33- tableName : source . config . tableName , // FIXME: Handle Ref:
57+ tableName : getTableName ( source . config . tableName ) ,
3458 } ,
3559 } ;
3660 }
@@ -58,14 +82,22 @@ export default function getAppSyncConfig(context, appSyncConfig) {
5882 } ;
5983
6084 const makeResolver = ( resolver ) => ( {
61- kind : 'UNIT' ,
85+ kind : resolver . kind || 'UNIT' ,
6286 fieldName : resolver . field ,
6387 typeName : resolver . type ,
6488 dataSourceName : resolver . dataSource ,
89+ functions : resolver . functions ,
6590 requestMappingTemplateLocation : resolver . request ,
6691 responseMappingTemplateLocation : resolver . response ,
6792 } ) ;
6893
94+ const makeFunctionConfiguration = ( functionConfiguration ) => ( {
95+ dataSourceName : functionConfiguration . dataSource ,
96+ name : functionConfiguration . name ,
97+ requestMappingTemplateLocation : functionConfiguration . request ,
98+ responseMappingTemplateLocation : functionConfiguration . response ,
99+ } ) ;
100+
69101 const makeAuthType = ( authType ) => {
70102 const auth = {
71103 authenticationType : authType . authenticationType ,
@@ -85,25 +117,26 @@ export default function getAppSyncConfig(context, appSyncConfig) {
85117 return auth ;
86118 } ;
87119
88- const makeAppSync = ( ) => ( {
89- name : appSyncConfig . name ,
120+ const makeAppSync = ( config ) => ( {
121+ name : config . name ,
90122 apiKey : context . options . apiKey ,
91- defaultAuthenticationType : makeAuthType ( appSyncConfig ) ,
92- additionalAuthenticationProviders : ( appSyncConfig . additionalAuthenticationProviders || [ ] )
123+ defaultAuthenticationType : makeAuthType ( config ) ,
124+ additionalAuthenticationProviders : ( config . additionalAuthenticationProviders || [ ] )
93125 . map ( makeAuthType ) ,
94126 } ) ;
95127
96128 const mappingTemplatesLocation = path . join (
97129 context . serverless . config . servicePath ,
98- appSyncConfig . mappingTemplatesLocation || 'mapping-templates' ,
130+ cfg . mappingTemplatesLocation || 'mapping-templates' ,
99131 ) ;
100132
101- return {
102- appSync : makeAppSync ( ) ,
103- schema : getFileMap ( context . serverless . config . servicePath , appSyncConfig . schema || 'schema.graphql' ) ,
104- resolvers : appSyncConfig . mappingTemplates . map ( makeResolver ) ,
105- dataSources : appSyncConfig . dataSources . map ( makeDataSource ) . filter ( ( v ) => v !== null ) ,
106- mappingTemplates : appSyncConfig . mappingTemplates . reduce ( ( acc , template ) => {
133+ const makeMappingTemplates = ( config ) => {
134+ const sources = [ ] . concat (
135+ config . mappingTemplates ,
136+ config . functionConfigurations ,
137+ ) ;
138+
139+ return sources . reduce ( ( acc , template ) => {
107140 const requestTemplate = template . request || `${ template . type } .${ template . field } .request.vtl` ;
108141 if ( ! find ( acc , ( e ) => e . path === requestTemplate ) ) {
109142 acc . push ( getFileMap ( mappingTemplatesLocation , requestTemplate ) ) ;
@@ -114,6 +147,15 @@ export default function getAppSyncConfig(context, appSyncConfig) {
114147 }
115148
116149 return acc ;
117- } , [ ] ) ,
150+ } , [ ] ) ;
151+ } ;
152+
153+ return {
154+ appSync : makeAppSync ( cfg ) ,
155+ schema : getFileMap ( context . serverless . config . servicePath , cfg . schema || 'schema.graphql' ) ,
156+ resolvers : cfg . mappingTemplates . map ( makeResolver ) ,
157+ dataSources : cfg . dataSources . map ( makeDataSource ) . filter ( ( v ) => v !== null ) ,
158+ functions : cfg . functionConfigurations . map ( makeFunctionConfiguration ) ,
159+ mappingTemplates : makeMappingTemplates ( cfg ) ,
118160 } ;
119161}
0 commit comments