1 | resource "aws_appsync_resolver" "User_friends" { |
2 | api_id = aws_appsync_graphql_api.appsync.id |
3 | type = "User" |
4 | field = "friends" |
5 | runtime { |
6 | name = "APPSYNC_JS" |
7 | runtime_version = "1.0.0" |
8 | } |
9 | code = <<EOF |
10 | export function request(ctx) { |
11 | return {}; |
12 | } |
13 | export function response(ctx) { |
14 | if (ctx.stash.earlyReturnNull) { |
15 | return null; |
16 | }else { |
17 | return ctx.result; |
18 | } |
19 | } |
20 | EOF |
21 | kind = "PIPELINE" |
22 | pipeline_config { |
23 | functions = [ |
24 | aws_appsync_function.User_friends_1.function_id, |
25 | aws_appsync_function.User_friends_2.function_id, |
26 | aws_appsync_function.User_friends_3.function_id, |
27 | ] |
28 | } |
29 | } |
30 | resource "aws_appsync_function" "User_friends_1" { |
31 | api_id = aws_appsync_graphql_api.appsync.id |
32 | data_source = aws_appsync_datasource.ddb_friend.name |
33 | name = "User_friends_1" |
34 | runtime { |
35 | name = "APPSYNC_JS" |
36 | runtime_version = "1.0.0" |
37 | } |
38 | code = <<EOF |
39 | import {util, runtime} from "@aws-appsync/utils"; |
40 | export function request(ctx) { |
41 | // skip friend check for same user |
42 | if (util.authType() !== "User Pool Authorization" || ctx.identity.sub === ctx.source.id) { |
43 | return runtime.earlyReturn({}); |
44 | } |
45 | return { |
46 | version : "2018-05-29", |
47 | operation : "GetItem", |
48 | key: { |
49 | userId1: {S: ctx.identity.sub}, |
50 | userId2: {S: ctx.source.id}, |
51 | } |
52 | }; |
53 | } |
54 | export function response(ctx) { |
55 | if (ctx.error) { |
56 | return util.error(ctx.error.message, ctx.error.type); |
57 | } |
58 | if(!ctx.result) { |
59 | ctx.stash.earlyReturnNull = true; |
60 | runtime.earlyReturn("null"); |
61 | } |
62 | return ctx.prev.result; |
63 | } |
64 | EOF |
65 | } |
66 | resource "aws_appsync_function" "User_friends_2" { |
67 | api_id = aws_appsync_graphql_api.appsync.id |
68 | data_source = aws_appsync_datasource.ddb_friend.name |
69 | name = "User_friends_2" |
70 | runtime { |
71 | name = "APPSYNC_JS" |
72 | runtime_version = "1.0.0" |
73 | } |
74 | code = <<EOF |
75 | import {util, runtime} from "@aws-appsync/utils"; |
76 | export function request(ctx) { |
77 | if (ctx.stash.earlyReturnNull) { |
78 | runtime.earlyReturn("null"); |
79 | } |
80 | return { |
81 | version : "2018-05-29", |
82 | operation : "Query", |
83 | query: { |
84 | expression: "#userId1 = :userId1", |
85 | expressionNames: { |
86 | "#userId1": "userId1", |
87 | }, |
88 | expressionValues: { |
89 | ":userId1": {S: ctx.source.id}, |
90 | }, |
91 | }, |
92 | nextToken: ctx.args.nextToken, |
93 | limit: 4, |
94 | }; |
95 | } |
96 | export function response(ctx) { |
97 | if (ctx.error) { |
98 | return util.error(ctx.error.message, ctx.error.type); |
99 | } |
100 | return { |
101 | userIds: ctx.result.items.map(({userId2}) => userId2), |
102 | nextToken: ctx.result.nextToken, |
103 | }; |
104 | } |
105 | EOF |
106 | } |
107 | resource "aws_appsync_function" "User_friends_3" { |
108 | api_id = aws_appsync_graphql_api.appsync.id |
109 | data_source = aws_appsync_datasource.ddb_friend.name |
110 | name = "User_friends_3" |
111 | runtime { |
112 | name = "APPSYNC_JS" |
113 | runtime_version = "1.0.0" |
114 | } |
115 | code = <<EOF |
116 | import {util, runtime} from "@aws-appsync/utils"; |
117 | |
118 | const tableName = "${aws_dynamodb_table.user.name}"; |
119 | export function request(ctx) { |
120 | if (ctx.stash.earlyReturnNull) { |
121 | runtime.earlyReturn("null"); |
122 | } |
123 | if (ctx.prev.result.userIds.length === 0) { |
124 | runtime.earlyReturn({users: [], nextToken: null}); |
125 | } |
126 | return { |
127 | version : "2018-05-29", |
128 | operation : "BatchGetItem", |
129 | tables: { |
130 | [tableName]: { |
131 | keys: ctx.prev.result.userIds.map((userId) => ({ |
132 | id: {S: userId}, |
133 | })), |
134 | consistentRead: true, |
135 | }, |
136 | }, |
137 | }; |
138 | } |
139 | export function response(ctx) { |
140 | if (ctx.error) { |
141 | return util.error(ctx.error.message, ctx.error.type); |
142 | } |
143 | if(ctx.result.unprocessedKeys[tableName].length > 0) { |
144 | return util.error("Batch get failed for some items"); |
145 | } |
146 | return { |
147 | users: ctx.result.data[tableName], |
148 | nextToken: ctx.prev.result.nextToken, |
149 | }; |
150 | } |
151 | EOF |
152 | } |
153 | |
154 | |