1 | resource "aws_appsync_resolver" "User_posts" { |
2 | api_id = aws_appsync_graphql_api.appsync.id |
3 | type = "User" |
4 | field = "posts" |
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_posts_1.function_id, |
25 | aws_appsync_function.User_posts_2.function_id, |
26 | ] |
27 | } |
28 | } |
29 | resource "aws_appsync_function" "User_posts_1" { |
30 | api_id = aws_appsync_graphql_api.appsync.id |
31 | data_source = aws_appsync_datasource.ddb_friend.name |
32 | name = "User_posts_1" |
33 | runtime { |
34 | name = "APPSYNC_JS" |
35 | runtime_version = "1.0.0" |
36 | } |
37 | code = <<EOF |
38 | import {util, runtime} from "@aws-appsync/utils"; |
39 | export function request(ctx) { |
40 | // skip friend check for same user |
41 | if (util.authType() !== "User Pool Authorization" || ctx.identity.sub === ctx.source.id) { |
42 | return runtime.earlyReturn({}); |
43 | } |
44 | return { |
45 | version : "2018-05-29", |
46 | operation : "GetItem", |
47 | key: { |
48 | userId1: {S: ctx.identity.sub}, |
49 | userId2: {S: ctx.source.id}, |
50 | } |
51 | }; |
52 | } |
53 | export function response(ctx) { |
54 | if (ctx.error) { |
55 | return util.error(ctx.error.message, ctx.error.type); |
56 | } |
57 | if(!ctx.result) { |
58 | ctx.stash.earlyReturnNull = true; |
59 | runtime.earlyReturn("null"); |
60 | } |
61 | return ctx.result; |
62 | } |
63 | EOF |
64 | } |
65 | resource "aws_appsync_function" "User_posts_2" { |
66 | api_id = aws_appsync_graphql_api.appsync.id |
67 | data_source = aws_appsync_datasource.ddb_post.name |
68 | name = "User_posts_2" |
69 | runtime { |
70 | name = "APPSYNC_JS" |
71 | runtime_version = "1.0.0" |
72 | } |
73 | code = <<EOF |
74 | import {util, runtime} from "@aws-appsync/utils"; |
75 | export function request(ctx) { |
76 | if(ctx.stash.earlyReturnNull) { |
77 | runtime.earlyReturn("null"); |
78 | } |
79 | return { |
80 | version : "2018-05-29", |
81 | operation : "Query", |
82 | index: "userId", |
83 | query: { |
84 | expression: "#userId = :userId", |
85 | expressionNames: { |
86 | "#userId": "userId", |
87 | }, |
88 | expressionValues: { |
89 | ":userId": {S: ctx.source.id}, |
90 | }, |
91 | }, |
92 | scanIndexForward: false, |
93 | nextToken: ctx.args.nextToken, |
94 | limit: ctx.args.limit, |
95 | }; |
96 | } |
97 | export function response(ctx) { |
98 | if (ctx.error) { |
99 | return util.error(ctx.error.message, ctx.error.type); |
100 | } |
101 | return { |
102 | posts: ctx.result.items, |
103 | nextToken: ctx.result.nextToken, |
104 | }; |
105 | } |
106 | EOF |
107 | } |
108 | |
109 | |