Skip to content

Commit d367be4

Browse files
committed
fix: migrate /search/issues to GraphQL + improve error handling
1 parent f594f4e commit d367be4

File tree

2 files changed

+63
-16
lines changed

2 files changed

+63
-16
lines changed

src/hooks/useGitHubData.ts

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,41 @@ export const useGitHubData = (getOctokit: () => any) => {
1010
const [rateLimited, setRateLimited] = useState(false);
1111

1212
const fetchPaginated = async (octokit: any, username: string, type: string, page = 1, per_page = 10) => {
13-
const q = `author:${username} is:${type}`;
14-
const response = await octokit.request('GET /search/issues', {
15-
q,
16-
sort: 'created',
17-
order: 'desc',
18-
per_page,
19-
page,
13+
const query = `
14+
query ($queryString: String!, $first: Int!, $after: String) {
15+
search(query: $queryString, type: ISSUE, first: $first, after: $after) {
16+
issueCount
17+
edges {
18+
cursor
19+
node {
20+
... on Issue {
21+
id
22+
title
23+
url
24+
createdAt
25+
}
26+
... on PullRequest {
27+
id
28+
title
29+
url
30+
createdAt
31+
}
32+
}
33+
}
34+
}
35+
}
36+
`;
37+
38+
const queryString = `author:${username} is:${type}`;
39+
const response = await octokit.graphql(query, {
40+
queryString,
41+
first: per_page,
42+
after: page > 1 ? btoa(`cursor:${(page - 1) * per_page}`) : null,
2043
});
2144

2245
return {
23-
items: response.data.items,
24-
total: response.data.total_count,
46+
items: response.search.edges.map((edge: any) => edge.node),
47+
total: response.search.issueCount,
2548
};
2649
};
2750

src/pages/ContributorProfile/ContributorProfile.tsx

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,34 @@ export default function ContributorProfile() {
5353
const userData = (await userRes.json()) as Profile;
5454
setProfile(userData);
5555

56-
// fetch PRs authored by the user (latest first)
57-
const q = encodeURIComponent(`author:${username} type:pr`);
58-
const prsRes = await gh(
59-
`/search/issues?q=${q}&per_page=100&sort=updated&order=desc`,
60-
token
61-
);
56+
// fetch PRs authored by the user (latest first) using GraphQL
57+
const gqlQuery = {
58+
query: `
59+
query($query: String!) {
60+
search(query: $query, type: ISSUE, first: 100) {
61+
nodes {
62+
... on PullRequest {
63+
id
64+
title
65+
url
66+
repository {
67+
nameWithOwner
68+
}
69+
}
70+
}
71+
}
72+
}
73+
`,
74+
variables: {
75+
query: `author:${username} type:pr sort:updated-desc`,
76+
},
77+
};
78+
79+
const prsRes = await gh("/graphql", token, {
80+
method: "POST",
81+
body: JSON.stringify(gqlQuery),
82+
});
83+
6284
if (!prsRes.ok) {
6385
const msg = await prsRes.text().catch(() => "");
6486
throw new Error(
@@ -67,8 +89,10 @@ export default function ContributorProfile() {
6789
}`
6890
);
6991
}
92+
7093
const prsData = await prsRes.json();
71-
setPRs(Array.isArray(prsData.items) ? (prsData.items as PR[]) : []);
94+
const prs = prsData.data?.search?.nodes || [];
95+
setPRs(prs as PR[]);
7296
} catch (error: any) {
7397
toast.error(`Failed to fetch user data. ${error?.message ?? ""}`);
7498
} finally {

0 commit comments

Comments
 (0)