Skip to content

Commit 29fa15e

Browse files
authored
Added a way to invoke external functions. (#42)
* Added a way to invoke external functions. * Added specification of a method for an external functon. Added an example for usage of external functions * Moved function configuration into plugin config. * Updated readme docs for external invoke functions. Added validateStatus false for external lambda handler.
1 parent b984428 commit 29fa15e

File tree

7 files changed

+100
-0
lines changed

7 files changed

+100
-0
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ Put options under `custom.appsync-simulator` in your `serverless.yml` file
5656
| refMap | {} | A mapping of [resource resolutions](#resource-cloudformation-functions-resolution) for the `Ref` function |
5757
| getAttMap | {} | A mapping of [resource resolutions](#resource-cloudformation-functions-resolution) for the `GetAtt` function |
5858
| importValueMap | {} | A mapping of [resource resolutions](#resource-cloudformation-functions-resolution) for the `ImportValue` function |
59+
| functions | {} | A mapping of [external functions](#functions) for providing invoke url for external fucntions |
5960
| dynamoDb.endpoint | http://localhost:8000 | Dynamodb endpoint. Specify it if you're not using serverless-dynamodb-local. Otherwise, port is taken from dynamodb-local conf |
6061
| dynamoDb.region | localhost | Dynamodb region. Specify it if you're connecting to a remote Dynamodb intance. |
6162
| dynamoDb.accessKeyId | DEFAULT_ACCESS_KEY | AWS Access Key ID to access DynamoDB |
@@ -202,6 +203,20 @@ For now, the supported resources to be automatically resovled by `Ref:` are:
202203

203204
Feel free to open a PR or an issue to extend them as well.
204205

206+
# External functions
207+
When a function is not defined withing the current serverless file you can still call it by providing an invoke url which should point to a REST method (must be post).
208+
209+
```yaml
210+
custom:
211+
appsync-simulator:
212+
functions:
213+
addUser:
214+
url: http://localhost:3016/2015-03-31/functions/addUser/invocations
215+
addPost:
216+
url: https://jsonplaceholder.typicode.com/posts
217+
```
218+
219+
205220
# Supported Resolver types
206221

207222
This plugin supports resolvers implemented by `amplify-appsync-simulator`, as well as custom resolvers.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"version": "2018-05-29",
3+
"operation": "Invoke"
4+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$util.toJson($context.result)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "external-functions",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"scripts": {
7+
"offline": "sls offline start"
8+
},
9+
"dependencies": {
10+
"serverless-appsync-plugin": "^1.3.1",
11+
"serverless-offline": "^6.7.0"
12+
}
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
type Post {
2+
userId: Int!
3+
id: Int!
4+
title: String!
5+
body: String!
6+
}
7+
8+
type Query {
9+
getPosts: [Post]!
10+
}
11+
12+
schema {
13+
query: Query
14+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
service: mapping-templates
2+
provider:
3+
name: aws
4+
region: eu-west-1
5+
6+
plugins:
7+
- serverless-offline
8+
- serverless-appsync-simulator
9+
- serverless-appsync-plugin
10+
11+
custom:
12+
appsync-simulator:
13+
functions:
14+
myLambda:
15+
url: https://jsonplaceholder.typicode.com/posts
16+
method: GET
17+
appSync:
18+
name: Test
19+
schema: schema.graphql
20+
authenticationType: AWS_IAM
21+
mappingTemplatesLocation: mapping-templates
22+
mappingTemplates:
23+
- dataSource: MyLambda
24+
type: Query
25+
field: getPosts
26+
request: Query.getPosts.request.vtl
27+
response: Query.getPosts.response.vtl
28+
dataSources:
29+
- type: AWS_LAMBDA
30+
name: MyLambda
31+
config:
32+
functionName: myLambda
33+
lambdaFunctionArn: "arn:aws:lambda:${self:provider.region}:*:getPosts-graphql"

src/getAppSyncConfig.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
AmplifyAppSyncSimulatorAuthenticationType as AuthTypes,
33
} from 'amplify-appsync-simulator';
44
import { invoke } from 'amplify-nodejs-function-runtime-provider/lib/utils/invoke';
5+
import axios from 'axios';
56
import fs from 'fs';
67
import { forEach } from 'lodash';
78
import path from 'path';
@@ -60,6 +61,24 @@ export default function getAppSyncConfig(context, appSyncConfig) {
6061
);
6162
return null;
6263
}
64+
65+
const conf = context.options;
66+
if (conf.functions && conf.functions[functionName] !== undefined) {
67+
const func = conf.functions[functionName];
68+
return {
69+
...dataSource,
70+
invoke: async (payload) => {
71+
const result = await axios.request({
72+
url: func.url,
73+
method: func.method,
74+
data: payload,
75+
validateStatus: false,
76+
});
77+
return result.data;
78+
}
79+
}
80+
}
81+
6382
const func = context.serverless.service.functions[functionName];
6483
if (func === undefined) {
6584
context.plugin.log(
@@ -68,6 +87,7 @@ export default function getAppSyncConfig(context, appSyncConfig) {
6887
);
6988
return null;
7089
}
90+
7191
return {
7292
...dataSource,
7393
invoke: (payload) => invoke({

0 commit comments

Comments
 (0)