social-network
social-network/Query_post.tf
1
resource "aws_appsync_resolver" "Query_post" {
2
  api_id = aws_appsync_graphql_api.appsync.id
3
  type   = "Query"
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 ctx.result;
15
}
16
EOF
17
  kind = "PIPELINE"
18
  pipeline_config {
19
    functions = [
20
      aws_appsync_function.Query_post_1.function_id,
21
      aws_appsync_function.Query_post_2.function_id,
22
    ]
23
  }
24
}
25
resource "aws_appsync_function" "Query_post_1" {
26
  api_id      = aws_appsync_graphql_api.appsync.id
27
  data_source = aws_appsync_datasource.ddb_post.name
28
  name        = "Query_post_1"
29
  runtime {
30
    name            = "APPSYNC_JS"
31
    runtime_version = "1.0.0"
32
  }
33
  code = <<EOF
34
import {util} from "@aws-appsync/utils";
35
export function request(ctx) {
36
	return {
37
		version : "2018-05-29",
38
		operation : "GetItem",
39
		key: {
40
			id: {S: ctx.args.id}
41
		}
42
	};
43
}
44
export function response(ctx) {
45
	if (ctx.error) {
46
		return util.error(ctx.error.message, ctx.error.type);
47
	}
48
	return ctx.result;
49
}
50
EOF
51
}
52
53
resource "aws_appsync_function" "Query_post_2" {
54
  api_id      = aws_appsync_graphql_api.appsync.id
55
  data_source = aws_appsync_datasource.ddb_friend.name
56
  name        = "Query_post_2"
57
  runtime {
58
    name            = "APPSYNC_JS"
59
    runtime_version = "1.0.0"
60
  }
61
  code = <<EOF
62
import {util, runtime} from "@aws-appsync/utils";
63
export function request(ctx) {
64
	// skip friend check for same user
65
	if (ctx.identity.sub === ctx.prev.result.userId) {
66
		return runtime.earlyReturn(ctx.prev.result);
67
	}
68
	return {
69
		version : "2018-05-29",
70
		operation : "GetItem",
71
		key: {
72
			userId1: {S: ctx.identity.sub},
73
			userId2: {S: ctx.prev.result.userId},
74
		}
75
	};
76
}
77
export function response(ctx) {
78
	if (ctx.error) {
79
		return util.error(ctx.error.message, ctx.error.type);
80
	}
81
	if(!ctx.result) {
82
		return util.unauthorized();
83
	}
84
	return ctx.prev.result;
85
}
86
EOF
87
}
88
89