/
getComments.ts
76 lines (66 loc) · 2.06 KB
/
getComments.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
import { gql } from "graphql-request";
import type { Unwrap } from "../types";
import type { GithubBlog } from "../github-blog";
import { GithubQueryParams } from "../utils/github-query";
import { PagerParams } from "../utils/pager";
import { isNonNull } from "../utils/func";
import { Comment } from "../datatypes/Comment";
import { PageInfo } from "../datatypes/PageInfo";
gql`
query GetComments($query: String!, $first: Int, $last: Int, $before: String, $after: String) {
search(first: 1, type: ISSUE, query: $query) {
nodes {
... on Issue {
comments(first: $first, last: $last, before: $before, after: $after) {
totalCount
pageInfo {
...PageInfo_PageInfo
}
edges {
cursor
node {
...Comment_IssueComment
}
}
}
}
}
}
}
`;
export type GetCommentsParams = {
query?: GithubQueryParams;
/**
* Pagination with limit and offset don't work in comments. Use cursor pagination
*/
pager?: Omit<PagerParams, "limit" | "offset">;
};
export const getComments = (blog: GithubBlog) => async (params: GetCommentsParams) => {
const query = blog.buildQuery(params.query);
const pager = blog.buildPager(params.pager);
const result = await blog.sdk.GetComments({ query, ...pager });
const issue = result.search.nodes?.[0];
if (!issue) {
return {
pageInfo: {},
totalCount: 0,
edges: [],
};
}
const connection = (issue as Extract<typeof issue, { __typename?: "Issue" }>).comments;
const edges = connection.edges ?? [];
const pageInfo = connection.pageInfo ?? {};
const totalCount = connection.totalCount ?? 0;
return {
totalCount,
pageInfo: PageInfo.translate(pageInfo),
edges: edges.filter(isNonNull).map((edge) => {
return {
cursor: edge.cursor,
comment: Comment.translate(edge.node),
};
}),
};
};
export type GetComments = ReturnType<typeof getComments>;
export type GetCommentsResult = Unwrap<ReturnType<GetComments>>;