social-network
social-network/Subscription_comment.tf
1
resource "aws_appsync_resolver" "Subscription_comment" {
2
  api_id = aws_appsync_graphql_api.appsync.id
3
  type   = "Subscription"
4
  field  = "comment"
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 null;
15
}
16
EOF
17
  kind = "PIPELINE"
18
  pipeline_config {
19
    functions = [
20
      aws_appsync_function.Subscription_comment_1.function_id,
21
      aws_appsync_function.Subscription_comment_2.function_id,
22
    ]
23
  }
24
}
25
26
resource "aws_appsync_function" "Subscription_comment_1" {
27
  api_id      = aws_appsync_graphql_api.appsync.id
28
  data_source = aws_appsync_datasource.ddb_friend.name
29
  name        = "Subscription_comment_1"
30
  runtime {
31
    name            = "APPSYNC_JS"
32
    runtime_version = "1.0.0"
33
  }
34
  code = <<EOF
35
import {util, runtime} from "@aws-appsync/utils";
36
export function request(ctx) {
37
	// skip friend check for same user
38
	if (ctx.identity.sub === ctx.args.postUserId) {
39
		return runtime.earlyReturn(ctx.prev.result);
40
	}
41
	return {
42
		version : "2018-05-29",
43
		operation : "GetItem",
44
		key: {
45
			userId1: {S: ctx.identity.sub},
46
			userId2: {S: ctx.args.postUserId},
47
		}
48
	};
49
}
50
export function response(ctx) {
51
	if (ctx.error) {
52
		return util.error(ctx.error.message, ctx.error.type);
53
	}
54
	if(!ctx.result) {
55
		return util.unauthorized();
56
	}
57
	return ctx.prev.result;
58
}
59
EOF
60
}
61
resource "aws_appsync_function" "Subscription_comment_2" {
62
  api_id      = aws_appsync_graphql_api.appsync.id
63
  data_source = aws_appsync_datasource.none.name
64
  name        = "Subscription_comment_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
export function request(ctx) {
72
	return {
73
		version : "2018-05-29",
74
	};
75
}
76
export function response(ctx) {
77
	const filterJson = {
78
		filterGroup: [{
79
			filters: [
80
				{
81
					fieldName: "postUserId",
82
					operator: "eq",
83
					value: ctx.args.postUserId,
84
				},
85
				{
86
					fieldName: "postDate",
87
					operator: "ge",
88
					value: util.time.epochMilliSecondsToISO8601(util.time.parseISO8601ToEpochMilliSeconds(ctx.args.postDateStarting)),
89
				}
90
			]
91
		}]
92
	};
93
94
	extensions.setSubscriptionFilter(filterJson);
95
96
	return ctx.result;
97
}
98
EOF
99
}
100
101