social-network
social-network/Subscription_post.tf
1
resource "aws_appsync_resolver" "Subscription_post" {
2
  api_id = aws_appsync_graphql_api.appsync.id
3
  type   = "Subscription"
4
  field  = "post"
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_post_1.function_id,
21
    ]
22
  }
23
}
24
25
resource "aws_appsync_function" "Subscription_post_1" {
26
  api_id      = aws_appsync_graphql_api.appsync.id
27
  data_source = aws_appsync_datasource.ddb_friend.name
28
  name        = "Subscription_post_1"
29
  runtime {
30
    name            = "APPSYNC_JS"
31
    runtime_version = "1.0.0"
32
  }
33
  code = <<EOF
34
import {util, runtime} from "@aws-appsync/utils";
35
export function request(ctx) {
36
	// skip friend check for same user
37
	if (ctx.identity.sub === ctx.args.userId) {
38
		return runtime.earlyReturn(ctx.prev.result);
39
	}
40
	return {
41
		version : "2018-05-29",
42
		operation : "GetItem",
43
		key: {
44
			userId1: {S: ctx.identity.sub},
45
			userId2: {S: ctx.args.userId},
46
		}
47
	};
48
}
49
export function response(ctx) {
50
	if (ctx.error) {
51
		return util.error(ctx.error.message, ctx.error.type);
52
	}
53
	if(!ctx.result) {
54
		return util.unauthorized();
55
	}
56
	return ctx.prev.result;
57
}
58
EOF
59
}
60
61