Skip to content

Commit 7b59f5d

Browse files
grzegorz-wcisloshahidhk
authored andcommitted
Add ruby examples for triggers on lambda (address hasura#1740) (hasura#3007)
1 parent c5c314a commit 7b59f5d

File tree

5 files changed

+174
-6
lines changed

5 files changed

+174
-6
lines changed

community/boilerplates/event-triggers/aws-lambda/README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ These are organized in language-specific folders.
77
**NOTE**
88
Some of the language/platforms are work in progress. We welcome contributions for the WIP langauages. See issues and the following checklist:
99

10-
| Folder name | Use-case| Node.js(6) | Python | Java | Go | C#
11-
|-------------|---------|:--------:|:------:|:----:|:---:|:---:
12-
| echo | echo the trigger payload | ✅ | ✅ | ❌ | ✅ | ❌
13-
| mutation | insert related data on an insert event using graphql mutation | ✅ | ✅ | ❌ | ✅ | ❌
14-
| push-notification | send push notification on database event | ❌ | ❌ | ❌ | ❌ | ❌
15-
| etl | transform the trigger payload and update an algolia index | ❌ | ❌ | ❌ | ❌ | ❌
10+
| Folder name | Use-case| Node.js(6) | Python | Java | Go | C# | Ruby
11+
|-------------|---------|:--------:|:------:|:----:|:---:|:---:|:---:
12+
| echo | echo the trigger payload | ✅ | ✅ | ❌ | ✅ | ❌ | ✅
13+
| mutation | insert related data on an insert event using graphql mutation | ✅ | ✅ | ❌ | ✅ | ❌ | ✅
14+
| push-notification | send push notification on database event | ❌ | ❌ | ❌ | ❌ | ❌ | ❌
15+
| etl | transform the trigger payload and update an algolia index | ❌ | ❌ | ❌ | ❌ | ❌ | ❌
1616

1717

1818

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Setup tables
2+
3+
1. Create table:
4+
5+
```
6+
profile (
7+
id INT PRIMARY KEY,
8+
name TEXT
9+
)
10+
```
11+
12+
# Setup AWS Lambda
13+
14+
Create a lambda function in AWS. This will be our webhook.
15+
16+
1. Create a function.
17+
1. Select Ruby 2.5 as the runtime.
18+
1. Select "Author from scratch".
19+
1. Select "handler" as the function name.
20+
1. Press "Create function".
21+
1. Add API gateway as a trigger (in this example you can use Open as the security option).
22+
1. Add an API to API gateway.
23+
1. Add the code in `lambda_function.rb` to the lambda function editor. The handler function of your lambda will be the lambda_handler.
24+
25+
26+
# Add the trigger in Hasura GraphQL
27+
28+
1. In events tab, add a trigger
29+
1. Select all insert, update, delete operations for the trigger.
30+
1. Paste the API endpoint of your AWS lambda as the webhook.
31+
32+
# Test your integration
33+
34+
1. Create a record.
35+
1. Update a record.
36+
1. Delete a record.
37+
1. Check the logs (in the Events tab) to see what is going on.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
require 'json'
2+
3+
def lambda_handler(event:, context:)
4+
body = JSON.parse(event['body'])
5+
table_name = body['table']['name']
6+
op = body['event']['op']
7+
data = body['event']['data']
8+
9+
response = case op
10+
when 'INSERT' then insert_message(data, table_name)
11+
when 'UPDATE' then update_message(data, table_name)
12+
when 'DELETE' then delete_message(data, table_name)
13+
end
14+
15+
{ statusCode: 200, body: response }
16+
rescue
17+
{ statusCode: 400, body: 'Cannot parse hasura event' }
18+
end
19+
20+
def insert_message(data, table_name)
21+
"New record #{new(data)} inserted into table #{table_name}"
22+
end
23+
24+
def update_message(data, table_name)
25+
"Record changed from #{old(data)} to #{new(data)} in table #{table_name}"
26+
end
27+
28+
def delete_message(data, table_name)
29+
"Record #{old(data)} was deleted from table #{table_name}"
30+
end
31+
32+
def new(data)
33+
JSON.generate(data['new'])
34+
end
35+
36+
def old(data)
37+
JSON.generate(data['old'])
38+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Setup tables
2+
3+
1. Create the table using the console:
4+
5+
```
6+
Table name: notes
7+
8+
Columns:
9+
id: Integer auto-increment
10+
note: Text
11+
12+
Table name: note_revision
13+
14+
Columns:
15+
id: Integer auto-increment
16+
note: Text
17+
note_id: Integer (foreign key to notes.id)
18+
update_at: Timestamp, default `now()`
19+
20+
```
21+
22+
# Setup AWS Lambda
23+
24+
Create a lambda function in AWS. This will be our webhook.
25+
26+
1. Create a function.
27+
1. Select Ruby 2.5 as the runtime.
28+
1. Select "Author from scratch".
29+
1. Select "handler" as the function name.
30+
1. Press "Create function".
31+
1. Add API gateway as a trigger (in this example you can use Open as the security option).
32+
1. Add an API to API gateway.
33+
1. Add the code in `lambda_function.rb` to the lambda function editor. The handler function of your lambda will be the lambda_handler.
34+
1. Add the following enviroment variables in your lambda config:
35+
1. `ACCESS_KEY`: this is the access key you configured when you setup HGE (`HASURA_GRAPHQL_ADMIN_SECRET` env variable).
36+
1. `HGE_ENDPOINT`: the URL on which you HGE instance is running.
37+
38+
# Add the trigger in Hasura GraphQL
39+
40+
1. In events tab, add a trigger
41+
1. Select the "notes" table and the update trigger.
42+
1. Paste the API endpoint of your AWS lambda as the webhook.
43+
44+
# Test your integration
45+
46+
1. Create a note.
47+
1. Change the contents of the note.
48+
1. Select all note revisions, previous note value should be visible.
49+
1. Check the logs (in the Events tab) to see what is going on.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require 'json'
2+
require 'net/http'
3+
4+
def lambda_handler(event:, context:)
5+
access_key = ENV['ACCESS_KEY']
6+
hge_endpoint = ENV['HGE_ENDPOINT']
7+
query = <<~QUERY
8+
mutation updateNoteRevision ($noteId: Int!, $data: String!) {
9+
insert_note_revision (objects: [
10+
{
11+
note_id: $noteId,
12+
note: $data
13+
}
14+
]) {
15+
affected_rows
16+
}
17+
}
18+
QUERY
19+
20+
note_id = JSON.parse(event['body'])['event']['data']['old']['id']
21+
data = JSON.parse(event['body'])['event']['data']['old']['note']
22+
23+
uri = URI.parse(hge_endpoint + '/v1/graphql')
24+
use_ssl = (uri.scheme == 'https')
25+
26+
res = Net::HTTP.start(uri.host, uri.port, use_ssl: use_ssl) do |http|
27+
req = Net::HTTP::Post.new(uri)
28+
req['Content-Type'] = 'application/json'
29+
req['x-hasura-access-key'] = access_key
30+
req.body = JSON.generate(
31+
query: query,
32+
variables: { noteId: note_id, data: data }
33+
)
34+
http.request(req)
35+
end
36+
37+
if errors = JSON.parse(res.body)['errors']
38+
return { statusCode: 400, body: JSON.generate(errors) }
39+
end
40+
41+
{ statusCode: 200, body: 'success' }
42+
rescue
43+
{ statusCode: 400, body: 'cannot parse hasura event' }
44+
end

0 commit comments

Comments
 (0)