1 | resource "aws_appsync_resolver" "Mutation_addComment" { |
2 | api_id = aws_appsync_graphql_api.appsync.id |
3 | type = "Mutation" |
4 | field = "addComment" |
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_addComment_1.function_id, |
24 | aws_appsync_function.Mutation_addComment_2.function_id, |
25 | aws_appsync_function.Mutation_addComment_3.function_id, |
26 | aws_appsync_function.Mutation_addComment_4.function_id, |
27 | ] |
28 | } |
29 | } |
30 | resource "aws_appsync_function" "Mutation_addComment_1" { |
31 | api_id = aws_appsync_graphql_api.appsync.id |
32 | data_source = aws_appsync_datasource.ddb_post.name |
33 | name = "Mutation_addComment_1" |
34 | runtime { |
35 | name = "APPSYNC_JS" |
36 | runtime_version = "1.0.0" |
37 | } |
38 | code = <<EOF |
39 | import {util} from "@aws-appsync/utils"; |
40 | export function request(ctx) { |
41 | return { |
42 | version : "2018-05-29", |
43 | operation : "GetItem", |
44 | key: { |
45 | id: {S: ctx.args.postId} |
46 | } |
47 | }; |
48 | } |
49 | export function response(ctx) { |
50 | if (ctx.error) { |
51 | return util.error(ctx.error.message, ctx.error.type); |
52 | } |
53 | return ctx.result; |
54 | } |
55 | EOF |
56 | } |
57 | |
58 | resource "aws_appsync_function" "Mutation_addComment_2" { |
59 | api_id = aws_appsync_graphql_api.appsync.id |
60 | data_source = aws_appsync_datasource.ddb_friend.name |
61 | name = "Mutation_addComment_2" |
62 | runtime { |
63 | name = "APPSYNC_JS" |
64 | runtime_version = "1.0.0" |
65 | } |
66 | code = <<EOF |
67 | import {util, runtime} from "@aws-appsync/utils"; |
68 | export function request(ctx) { |
69 | // skip friend check for same user |
70 | if (util.authType() !== "User Pool Authorization" || ctx.identity.sub === ctx.prev.result.userId) { |
71 | return runtime.earlyReturn(ctx.prev.result); |
72 | } |
73 | return { |
74 | version : "2018-05-29", |
75 | operation : "GetItem", |
76 | key: { |
77 | userId1: {S: ctx.identity.sub}, |
78 | userId2: {S: ctx.prev.result.userId}, |
79 | } |
80 | }; |
81 | } |
82 | export function response(ctx) { |
83 | if (ctx.error) { |
84 | return util.error(ctx.error.message, ctx.error.type); |
85 | } |
86 | if(!ctx.result) { |
87 | return util.unauthorized(); |
88 | } |
89 | return ctx.prev.result; |
90 | } |
91 | EOF |
92 | } |
93 | |
94 | resource "aws_appsync_function" "Mutation_addComment_3" { |
95 | api_id = aws_appsync_graphql_api.appsync.id |
96 | data_source = aws_appsync_datasource.ddb_comment.name |
97 | name = "Mutation_addComment_3" |
98 | runtime { |
99 | name = "APPSYNC_JS" |
100 | runtime_version = "1.0.0" |
101 | } |
102 | code = <<EOF |
103 | import {util} from "@aws-appsync/utils"; |
104 | export function request(ctx) { |
105 | return { |
106 | version : "2018-05-29", |
107 | operation : "PutItem", |
108 | key : { |
109 | id: {S: util.autoId()} |
110 | }, |
111 | attributeValues: { |
112 | userId: {S: ctx.args.userId}, |
113 | postId: {S: ctx.args.postId}, |
114 | date: {N: util.time.nowEpochMilliSeconds() + ""}, |
115 | text: {S: ctx.args.text}, |
116 | } |
117 | }; |
118 | } |
119 | export function response(ctx) { |
120 | if (ctx.error) { |
121 | return util.error(ctx.error.message, ctx.error.type); |
122 | } |
123 | return ctx.result; |
124 | } |
125 | EOF |
126 | } |
127 | |
128 | resource "aws_appsync_function" "Mutation_addComment_4" { |
129 | api_id = aws_appsync_graphql_api.appsync.id |
130 | data_source = aws_appsync_datasource.notify.name |
131 | name = "Mutation_addComment_4" |
132 | runtime { |
133 | name = "APPSYNC_JS" |
134 | runtime_version = "1.0.0" |
135 | } |
136 | code = <<EOF |
137 | import {util} from "@aws-appsync/utils"; |
138 | ${file("${path.module}/notifyComment.js")} |
139 | |
140 | export function request(ctx) { |
141 | return notifyComment({ |
142 | commentId: ctx.prev.result.id, |
143 | }); |
144 | } |
145 | |
146 | export function response(ctx) { |
147 | const result = JSON.parse(ctx.result.body); |
148 | if (result.errors) { |
149 | return util.error(result.errors[0].message); |
150 | } |
151 | |
152 | return ctx.prev.result; |
153 | } |
154 | EOF |
155 | } |
156 | |
157 | |