social-network
social-network/Post_comments.tf
1
resource "aws_appsync_resolver" "Post_comments" {
2
  api_id = aws_appsync_graphql_api.appsync.id
3
  type   = "Post"
4
  field  = "comments"
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
	return ctx.result;
15
}
16
EOF
17
  kind = "PIPELINE"
18
  pipeline_config {
19
    functions = [
20
      aws_appsync_function.Post_comments_1.function_id,
21
    ]
22
  }
23
}
24
resource "aws_appsync_function" "Post_comments_1" {
25
  api_id      = aws_appsync_graphql_api.appsync.id
26
  data_source = aws_appsync_datasource.ddb_comment.name
27
  name        = "Post_comments_1"
28
  runtime {
29
    name            = "APPSYNC_JS"
30
    runtime_version = "1.0.0"
31
  }
32
  code = <<EOF
33
import {util} from "@aws-appsync/utils";
34
export function request(ctx) {
35
	return {
36
		version : "2018-05-29",
37
		operation : "Query",
38
		index: "postId",
39
		query: {
40
			expression: "#postId = :postId",
41
			expressionNames: {
42
				"#postId": "postId",
43
			},
44
			expressionValues: {
45
				":postId": {S: ctx.source.id},
46
			},
47
		},
48
		scanIndexForward: false,
49
		nextToken: ctx.args.nextToken,
50
		limit: ctx.args.limit,
51
	};
52
}
53
export function response(ctx) {
54
	if (ctx.error) {
55
		return util.error(ctx.error.message, ctx.error.type);
56
	}
57
	return {
58
		comments: ctx.result.items,
59
		nextToken: ctx.result.nextToken,
60
	};
61
}
62
EOF
63
}
64
65