-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathLikeConnection.ts
More file actions
79 lines (73 loc) · 1.97 KB
/
Copy pathLikeConnection.ts
File metadata and controls
79 lines (73 loc) · 1.97 KB
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
import { GqlInfo, Int } from "grats";
import * as DB from "../Database.js";
import { VC } from "../ViewerContext.js";
import { Like } from "./Like.js";
import { PageInfo } from "../graphql/Connection.js";
import { PubSub } from "../PubSub.js";
import { filter, map, pipe } from "graphql-yoga";
import { getLocalTypeAssert } from "../graphql/Node.js";
import { connectionFromSelectOrCount } from "../graphql/gqlUtils.js";
/** @gqlType */
export type LikeConnection = {
/** @gqlField */
edges: LikeEdge[];
/**
* The total number of likes that post has received.
* **Note:** This is separate from the number of edges currently being read.
* @gqlField */
count: Int;
/** @gqlField */
pageInfo: PageInfo;
};
/**
* Convenience field to get the nodes from a connection.
* @gqlField */
export function nodes(likeConnection: LikeConnection): Like[] {
return likeConnection.edges.map((edge) => edge.node);
}
/** @gqlType */
type LikeEdge = {
/** @gqlField */
node: Like;
/** @gqlField */
cursor: string;
};
// --- Root Fields ---
/**
* All likes in the system. Note that there is no guarantee of order.
* @gqlQueryField
* @gqlAnnotate cost(credits: 10) */
export async function likes(
args: {
first?: Int | null;
after?: string | null;
last?: Int | null;
before?: string | null;
},
vc: VC,
info: GqlInfo,
): Promise<LikeConnection> {
return connectionFromSelectOrCount(
() => DB.selectLikes(vc),
() => DB.selectLikesCount(vc),
args,
info,
);
}
/**
* Subscribe to likes on a post.
* **Note:** Does not immediately return likes, but rather updates as likes are applied.
* @gqlSubscriptionField */
export async function postLikes(
postID: string,
vc: VC,
info: GqlInfo,
): Promise<AsyncIterable<LikeConnection>> {
const id = getLocalTypeAssert(postID, "Post");
const post = await vc.getPostById(id);
return pipe(
PubSub.subscribe("postLiked"),
filter((postId) => postId === id),
map(() => post.likes({}, info)),
);
}