forked from functionless/functionless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
people-db.ts
116 lines (104 loc) · 3.16 KB
/
people-db.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { Construct } from "constructs";
import { aws_dynamodb, aws_lambda } from "aws-cdk-lib";
import { Table, Function, $util, AppsyncResolver } from "functionless";
import { QueryResolvers, MutationResolvers, Person } from "./generated-types";
export class PeopleDatabase extends Construct {
readonly personTable;
readonly computeScore;
readonly getPerson;
readonly addPerson;
readonly updateName;
readonly deletePerson;
constructor(scope: Construct, id: string) {
super(scope, id);
this.personTable = new Table<Person, "id", undefined>(
new aws_dynamodb.Table(this, "table", {
partitionKey: {
name: "id",
type: aws_dynamodb.AttributeType.STRING,
},
})
);
this.computeScore = new Function<(person: Person) => number>(
new aws_lambda.Function(this, "ComputeScore", {
code: aws_lambda.Code.fromInline(
"exports.handle = async function() {return 1;}"
),
handler: "index.handle",
runtime: aws_lambda.Runtime.NODEJS_14_X,
})
);
this.getPerson = new AppsyncResolver<
QueryResolvers["getPerson"]["args"],
QueryResolvers["getPerson"]["result"],
QueryResolvers["getPerson"]["parent"]
>(($context) => {
const person = this.personTable.getItem({
key: {
id: $util.dynamodb.toDynamoDB($context.arguments.id),
},
consistentRead: true,
});
if (person === undefined) {
return undefined;
}
const score = this.computeScore(person);
return {
...person,
score,
};
});
this.addPerson = new AppsyncResolver<
MutationResolvers["addPerson"]["args"],
MutationResolvers["addPerson"]["result"],
MutationResolvers["addPerson"]["parent"]
>(($context) => {
const person = this.personTable.putItem({
key: {
id: {
S: $util.autoId(),
},
},
attributeValues: {
name: {
S: $context.arguments.input.name,
},
},
});
return person;
});
// example of inferring the TArguments and TResult from the function signature
this.updateName = new AppsyncResolver<
MutationResolvers["updateName"]["args"],
MutationResolvers["updateName"]["result"],
MutationResolvers["updateName"]["parent"]
>(($context) =>
this.personTable.updateItem({
key: {
id: $util.dynamodb.toDynamoDB($context.arguments.id),
},
update: {
expression: "SET #name = :name",
expressionNames: {
"#name": "name",
},
expressionValues: {
":name": $util.dynamodb.toDynamoDB($context.arguments.name),
},
},
})
);
// example of explicitly specifying TArguments and TResult
this.deletePerson = new AppsyncResolver<
MutationResolvers["deletePerson"]["args"],
MutationResolvers["deletePerson"]["result"],
MutationResolvers["deletePerson"]["parent"]
>(($context) =>
this.personTable.deleteItem({
key: {
id: $util.dynamodb.toDynamoDB($context.arguments.id),
},
})
);
}
}