The AppSync API with schema and the backend Lambda function both are ready. Time to tie them together.
-
Goto AWS AppSync console, open dojoapi details. Click on the Data Sources menu in the left and then click on the Create data source button.
-
On the next screen, type in dojodatasource as the data source name. Select AWS Lambda Function for the data source type. Select EU-WEST-1 as the region. Select dojoworklamda function ARN. Finally choose New Role option and click on the Create button.
-
The datasource is ready. You now attach the data source to the methods in the schema. Click on the Schema menu option in the left and click on the Attach button next to addTask(…): String in the Resolvers section.
-
On the next screen, select dojodatasource as the data source. Enable both options Enable request mapping template and Enable response mapping template checkboxes. In the Configure the request mapping template field, copy-paste the configuration shown below.
{ "version" : "2017-02-28", "operation": "Invoke", "payload": { "method": "addTask", "arguments": $utils.toJson($context.arguments) } }
`
-
The configuration above, the addTask method would invoke the Lambda function and would pass two fields in the parameters - method and arguments. Remember when you defined the Lambda function. The event parameter in the Lambda function expected to carry two fields - method and arguments. This is the place where you are configuring for these two field values. The event parameter is populated with the value addTask. The arguments parameter is populated from the query context which will have fields id and description. For response, the Lambda function result is returned.
-
Click on the Save Resolver button to save the resolver configuration for the addTask method.
-
You need to repeat the same for the getTask method. Click on the Schema menu option in the left and click on the Attach button next to getTask(…): String in the Resolvers section.
-
On the next screen, select dojodatasource as the data source. Enable both options Enable request mapping template and Enable response mapping template checkboxes. In the Configure the request mapping template field, copy-paste the configuration shown below.
{ "version" : "2017-02-28", "operation": "Invoke", "payload": { "method": "getTask", "arguments": $utils.toJson($context.arguments) } }
`
-
The configuration above, the getTask method would invoke the Lambda function and would pass two fields in the parameters - method and arguments. The event parameter is populated with the value getTask. The arguments parameter is populated from the query context which will have id field. For response, the Lambda function result is returned.
-
Click on the Save Resolver button to save the resolver configuration for the getTask method.
-
The resolver has attached the backend Lambda function to the API and schema. API is ready - time to test it.