-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AppSync Api -> Lambda connector (#3145)
- Loading branch information
1 parent
c3c8bde
commit 7b5de1a
Showing
10 changed files
with
599 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
integration/resources/expected/combination/connector_appsync_api_to_lambda.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
[ | ||
{ | ||
"LogicalResourceId": "Api", | ||
"ResourceType": "AWS::AppSync::GraphQLApi" | ||
}, | ||
{ | ||
"LogicalResourceId": "ApiSchema", | ||
"ResourceType": "AWS::AppSync::GraphQLSchema" | ||
}, | ||
{ | ||
"LogicalResourceId": "NoneDataSource", | ||
"ResourceType": "AWS::AppSync::DataSource" | ||
}, | ||
{ | ||
"LogicalResourceId": "SayHelloResolver", | ||
"ResourceType": "AWS::AppSync::Resolver" | ||
}, | ||
{ | ||
"LogicalResourceId": "SayHelloFunc", | ||
"ResourceType": "AWS::AppSync::FunctionConfiguration" | ||
}, | ||
{ | ||
"LogicalResourceId": "Authorizer", | ||
"ResourceType": "AWS::Lambda::Function" | ||
}, | ||
{ | ||
"LogicalResourceId": "AuthorizerRole", | ||
"ResourceType": "AWS::IAM::Role" | ||
}, | ||
{ | ||
"LogicalResourceId": "TriggerFunction", | ||
"ResourceType": "AWS::Lambda::Function" | ||
}, | ||
{ | ||
"LogicalResourceId": "TriggerFunctionRole", | ||
"ResourceType": "AWS::IAM::Role" | ||
}, | ||
{ | ||
"LogicalResourceId": "GraphQlApiToLambdaConnectorWriteLambdaPermission", | ||
"ResourceType": "AWS::Lambda::Permission" | ||
} | ||
] |
169 changes: 169 additions & 0 deletions
169
integration/resources/templates/combination/connector_appsync_api_to_lambda.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
Resources: | ||
Api: | ||
Type: AWS::AppSync::GraphQLApi | ||
Properties: | ||
Name: Api | ||
AuthenticationType: AWS_LAMBDA | ||
LambdaAuthorizerConfig: | ||
AuthorizerUri: !GetAtt Authorizer.Arn | ||
|
||
ApiSchema: | ||
Type: AWS::AppSync::GraphQLSchema | ||
Properties: | ||
ApiId: !GetAtt Api.ApiId | ||
Definition: | | ||
type Query { | ||
sayHello: String! | ||
} | ||
schema { | ||
query: Query | ||
} | ||
NoneDataSource: | ||
Type: AWS::AppSync::DataSource | ||
Properties: | ||
Type: NONE | ||
ApiId: !GetAtt Api.ApiId | ||
Name: NoneDataSource | ||
|
||
SayHelloResolver: | ||
DependsOn: ApiSchema | ||
Type: AWS::AppSync::Resolver | ||
Properties: | ||
ApiId: !GetAtt Api.ApiId | ||
TypeName: Query | ||
FieldName: sayHello | ||
Kind: PIPELINE | ||
PipelineConfig: | ||
Functions: | ||
- !GetAtt SayHelloFunc.FunctionId | ||
Code: | | ||
export function request(ctx) { | ||
return {}; | ||
} | ||
export function response(ctx) { | ||
return ctx.prev.result; | ||
} | ||
Runtime: | ||
Name: APPSYNC_JS | ||
RuntimeVersion: 1.0.0 | ||
|
||
SayHelloFunc: | ||
Type: AWS::AppSync::FunctionConfiguration | ||
Properties: | ||
ApiId: !GetAtt Api.ApiId | ||
Name: SayHelloFunc | ||
DataSourceName: !GetAtt NoneDataSource.Name | ||
Code: | | ||
export function request(ctx) { | ||
return {}; | ||
} | ||
export function response(ctx) { | ||
return "Hello World"; | ||
} | ||
Runtime: | ||
Name: APPSYNC_JS | ||
RuntimeVersion: 1.0.0 | ||
|
||
GraphQlApiToLambdaConnector: | ||
Type: AWS::Serverless::Connector | ||
Properties: | ||
Source: | ||
Id: Api | ||
Destination: | ||
Id: Authorizer | ||
Permissions: | ||
- Write | ||
|
||
Authorizer: | ||
Type: AWS::Serverless::Function | ||
Properties: | ||
InlineCode: | | ||
exports.handler = async (_) => { | ||
return { | ||
isAuthorized: true, | ||
deniedFields: [], | ||
} | ||
} | ||
PackageType: Zip | ||
Runtime: nodejs14.x | ||
Handler: index.handler | ||
|
||
TriggerFunction: | ||
Type: AWS::Serverless::Function | ||
Properties: | ||
Environment: | ||
Variables: | ||
GRAPHQL_URL: !GetAtt Api.GraphQLUrl | ||
Runtime: nodejs14.x | ||
Handler: index.handler | ||
InlineCode: | | ||
const https = require("https"); | ||
exports.handler = async (_) => { | ||
const queries = { | ||
sayHello: /* GraphQL */ ` | ||
query { | ||
sayHello | ||
} | ||
`, | ||
}; | ||
const fetch = async (url, options) => | ||
new Promise((resolve, reject) => { | ||
const req = https.request(url, options, (res) => { | ||
const body = []; | ||
res.on("data", (chunk) => body.push(chunk)); | ||
res.on("end", () => { | ||
const resString = Buffer.concat(body).toString(); | ||
resolve(resString); | ||
}); | ||
}); | ||
req.on("error", (err) => { | ||
reject(err); | ||
}); | ||
req.on("timeout", () => { | ||
req.destroy(); | ||
reject(new Error("Request time out")); | ||
}); | ||
req.write(options.body); | ||
req.end(); | ||
}); | ||
const makeRequest = async (queryName) => { | ||
const options = { | ||
method: "POST", | ||
headers: { | ||
"Authorization": "n'importe quoi", | ||
}, | ||
body: JSON.stringify({ query: queries[queryName] }), | ||
timeout: 10000, // ms | ||
}; | ||
const response = await fetch(process.env.GRAPHQL_URL, options); | ||
const body = JSON.parse(response); | ||
const data = body.data?.[queryName]; | ||
if (body.errors !== undefined) { | ||
throw JSON.stringify(body.errors); | ||
} | ||
if (data !== "Hello World") { | ||
throw new Error(`${queryName} error: '${data}' must be 'Hello World'`); | ||
} | ||
return body.data; | ||
}; | ||
return await makeRequest("sayHello"); | ||
}; | ||
Metadata: | ||
SamTransformTest: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
tests/translator/input/connector_appsync_api_to_lambda.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
Resources: | ||
Api: | ||
Type: AWS::AppSync::GraphQLApi | ||
Properties: | ||
Name: Api | ||
AuthenticationType: AWS_LAMBDA | ||
LambdaAuthorizerConfig: | ||
AuthorizerUri: !GetAtt Authorizer.Arn | ||
|
||
GraphQlApiToLambdaConnector: | ||
Type: AWS::Serverless::Connector | ||
Properties: | ||
Source: | ||
Id: Api | ||
Destination: | ||
Id: Authorizer | ||
Permissions: | ||
- Write | ||
|
||
Authorizer: | ||
Type: AWS::Serverless::Function | ||
Properties: | ||
InlineCode: | | ||
exports.handler = async (_) => { | ||
return { | ||
isAuthorized: true, | ||
deniedFields: [], | ||
} | ||
} | ||
PackageType: Zip | ||
Runtime: nodejs14.x | ||
Handler: index.handler |
Oops, something went wrong.