1 | resource "aws_appsync_resolver" "Mutation_createPost" { |
2 | api_id = aws_appsync_graphql_api.appsync.id |
3 | type = "Mutation" |
4 | field = "createPost" |
5 | runtime { |
6 | name = "APPSYNC_JS" |
7 | runtime_version = "1.0.0" |
8 | } |
9 | code = <<EOF |
10 | export function request(ctx) { |
11 | if (util.authType() === "User Pool Authorization" && ctx.identity.sub !== ctx.args.userId) { |
12 | return util.unauthorized(); |
13 | } |
14 | return {}; |
15 | } |
16 | export function response(ctx) { |
17 | return ctx.result; |
18 | } |
19 | EOF |
20 | kind = "PIPELINE" |
21 | pipeline_config { |
22 | functions = [ |
23 | aws_appsync_function.Mutation_createPost_1.function_id, |
24 | aws_appsync_function.Mutation_createPost_2.function_id, |
25 | ] |
26 | } |
27 | } |
28 | resource "aws_appsync_function" "Mutation_createPost_1" { |
29 | api_id = aws_appsync_graphql_api.appsync.id |
30 | data_source = aws_appsync_datasource.ddb_post.name |
31 | name = "Mutation_createPost_1" |
32 | runtime { |
33 | name = "APPSYNC_JS" |
34 | runtime_version = "1.0.0" |
35 | } |
36 | code = <<EOF |
37 | import {util} from "@aws-appsync/utils"; |
38 | export function request(ctx) { |
39 | return { |
40 | version : "2018-05-29", |
41 | operation : "PutItem", |
42 | key : { |
43 | id: {S: util.autoId()} |
44 | }, |
45 | attributeValues: { |
46 | userId: {S: ctx.args.userId}, |
47 | date: {N: util.time.nowEpochMilliSeconds() + ""}, |
48 | text: {S: ctx.args.text}, |
49 | } |
50 | }; |
51 | } |
52 | export function response(ctx) { |
53 | if (ctx.error) { |
54 | return util.error(ctx.error.message, ctx.error.type); |
55 | } |
56 | return ctx.result; |
57 | } |
58 | EOF |
59 | } |
60 | |
61 | resource "aws_appsync_function" "Mutation_createPost_2" { |
62 | api_id = aws_appsync_graphql_api.appsync.id |
63 | data_source = aws_appsync_datasource.notify.name |
64 | name = "Mutation_createPost_2" |
65 | runtime { |
66 | name = "APPSYNC_JS" |
67 | runtime_version = "1.0.0" |
68 | } |
69 | code = <<EOF |
70 | import {util} from "@aws-appsync/utils"; |
71 | ${file("${path.module}/notifyPost.js")} |
72 | |
73 | export function request(ctx) { |
74 | return notifyPost({ |
75 | postId: ctx.prev.result.id, |
76 | }); |
77 | } |
78 | |
79 | export function response(ctx) { |
80 | const result = JSON.parse(ctx.result.body); |
81 | if (result.errors) { |
82 | return util.error(result.errors[0].message); |
83 | } |
84 | |
85 | return ctx.prev.result; |
86 | } |
87 | EOF |
88 | } |
89 | |
90 | |