Skip to content

Commit

Permalink
Add doc, example and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bboure committed Feb 25, 2019
1 parent 54c8bdd commit 177701f
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ custom:
name: # data source name
description: 'Lambda DataSource'
config:
functionName: graphql # The function name in your serverless.yml. Ignore if lambdaFunctionArn is provided.
lambdaFunctionArn: { Fn::GetAtt: [GraphqlLambdaFunction, Arn] } # Where GraphqlLambdaFunction is the lambda function cloudformation resource created by serverless for the serverless function named graphql
serviceRoleArn: { Fn::GetAtt: [AppSyncLambdaServiceRole, Arn] } # Where AppSyncLambdaServiceRole is an IAM role defined in Resources
iamRoleStatements: # custom IAM Role statements for this DataSource. Ignored if `serviceRoleArn` is present. Auto-generated if both `serviceRoleArn` and `iamRoleStatements` are omitted
Expand Down
5 changes: 3 additions & 2 deletions example/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ custom:
- type: AWS_LAMBDA
name: Lambda_MeInfo
description: 'Lambda DataSource'
config:
lambdaFunctionArn: { Fn::GetAtt: [GraphqlLambdaFunction, Arn] }
config: # Either of functionName or lambdaFunctionArn mus tbe provided. When both are present, lambdaFunctionArn is used.
functionName: graphql
lambdaFunctionArn: { Fn::GetAtt: [GraphqlLambdaFunction, Arn] } #
serviceRoleArn: { Fn::GetAtt: [AppSyncLambdaServiceRole, Arn] }


Expand Down
99 changes: 98 additions & 1 deletion index.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
const Serverless = require('serverless/lib/Serverless');
const ServerlessAppsyncPlugin = require('.');
const AwsProvider = require('serverless/lib/plugins/aws/provider/awsProvider.js');

let serverless;
let plugin;
let config;

beforeEach(() => {
serverless = new Serverless();
const options = {
stage: 'dev',
region: 'us-east-1',
};
serverless.setProvider('aws', new AwsProvider(serverless, options));
plugin = new ServerlessAppsyncPlugin(serverless, {});
config = {
name: 'api',
Expand Down Expand Up @@ -97,6 +103,97 @@ describe("appsync config", () => {
const role = plugin.getCloudWatchLogsRole(config);
expect(role).toEqual({});
});

test("Datasource generates lambdaFunctionArn from functionName", () => {

Object.assign(
config,
{
dataSources: [
{
type: 'AWS_LAMBDA',
name: 'lambdaSource',
description: 'lambdaSource Desc',
config: {
functionName: "myFunc",
serviceRoleArn: "arn:aws:iam::123456789012:role/service-role/myLambdaRole",
}
},
],
},
);

const dataSources = plugin.getDataSourceResources(config);
expect(dataSources).toEqual({
"GraphQlDslambdaSource":
{
"Type": "AWS::AppSync::DataSource",
"Properties": {
Type: 'AWS_LAMBDA',
"ApiId": {
"Fn::GetAtt": [
"GraphQlApi",
"ApiId",
],
},
Name: 'lambdaSource',
ServiceRoleArn: "arn:aws:iam::123456789012:role/service-role/myLambdaRole",
Description: 'lambdaSource Desc',
LambdaConfig: {
LambdaFunctionArn: {
"Fn::GetAtt": ["MyFuncLambdaFunction", "Arn"]
},
}
},
},
});
});

test("Datasource uses lambdaFunctionArn when provided", () => {

Object.assign(
config,
{
dataSources: [
{
type: 'AWS_LAMBDA',
name: 'lambdaSource',
description: 'lambdaSource Desc',
config: {
functionName: "myDummyFunc",
lambdaFunctionArn: {"Fn::GetAtt": ["MyFuncLambdaFunction", "Arn"]},
serviceRoleArn: "arn:aws:iam::123456789012:role/service-role/myLambdaRole",
}
},
],
},
);

const dataSources = plugin.getDataSourceResources(config);
expect(dataSources).toEqual({
"GraphQlDslambdaSource":
{
"Type": "AWS::AppSync::DataSource",
"Properties": {
Type: 'AWS_LAMBDA',
"ApiId": {
"Fn::GetAtt": [
"GraphQlApi",
"ApiId",
],
},
Name: 'lambdaSource',
ServiceRoleArn: "arn:aws:iam::123456789012:role/service-role/myLambdaRole",
Description: 'lambdaSource Desc',
LambdaConfig: {
LambdaFunctionArn: {
"Fn::GetAtt": ["MyFuncLambdaFunction", "Arn"]
},
}
},
},
});
});

});

Expand Down Expand Up @@ -579,5 +676,5 @@ describe("iamRoleStatements", () => {
const roles = plugin.getDataSourceIamRolesResouces(config);
expect(roles).toEqual({});
});

});

0 comments on commit 177701f

Please sign in to comment.