Skip to content

Commit ec89e35

Browse files
committed
Merge branch 'main' into feat/query-merging
Signed-off-by: Adam Setch <[email protected]>
1 parent 7db1845 commit ec89e35

File tree

4 files changed

+80
-41
lines changed

4 files changed

+80
-41
lines changed

src/renderer/utils/api/client.ts

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
apiRequestAuth,
2626
type ExecutionResultWithHeaders,
2727
performGraphQLRequest,
28+
performGraphQLRequestString,
2829
} from './request';
2930
import type {
3031
NotificationThreadSubscription,
@@ -194,6 +195,33 @@ export async function fetchAuthenticatedUserDetails(
194195
);
195196
}
196197

198+
/**
199+
* Fetch GitHub Discussion by Discussion Number.
200+
*/
201+
export async function fetchDiscussionByNumber(
202+
notification: GitifyNotification,
203+
): Promise<ExecutionResult<FetchDiscussionByNumberQuery>> {
204+
const url = getGitHubGraphQLUrl(notification.account.hostname);
205+
const number = getNumberFromUrl(notification.subject.url);
206+
207+
return performGraphQLRequest(
208+
url.toString() as Link,
209+
notification.account.token,
210+
FetchDiscussionByNumberDocument,
211+
{
212+
ownerINDEX: notification.repository.owner.login,
213+
nameINDEX: notification.repository.name,
214+
numberINDEX: number,
215+
firstLabels: 100,
216+
lastComments: 10,
217+
lastReplies: 10,
218+
includeIsAnswered: isAnsweredDiscussionFeatureSupported(
219+
notification.account,
220+
),
221+
},
222+
);
223+
}
224+
197225
/**
198226
* Fetch GitHub Issue by Issue Number.
199227
*/
@@ -243,28 +271,19 @@ export async function fetchPullByNumber(
243271
}
244272

245273
/**
246-
* Fetch GitHub Discussion by Discussion Number.
274+
* Fetch Batched Details for Discussions, Issues and Pull Requests.
247275
*/
248-
export async function fetchDiscussionByNumber(
276+
export async function fetchMergedQueryDetails(
249277
notification: GitifyNotification,
250-
): Promise<ExecutionResult<FetchDiscussionByNumberQuery>> {
278+
mergedQuery: string,
279+
mergedVariables: Record<string, string | number | boolean>,
280+
): Promise<ExecutionResult<Record<string, unknown>>> {
251281
const url = getGitHubGraphQLUrl(notification.account.hostname);
252-
const number = getNumberFromUrl(notification.subject.url);
253282

254-
return performGraphQLRequest(
283+
return performGraphQLRequestString(
255284
url.toString() as Link,
256285
notification.account.token,
257-
FetchDiscussionByNumberDocument,
258-
{
259-
ownerINDEX: notification.repository.owner.login,
260-
nameINDEX: notification.repository.name,
261-
numberINDEX: number,
262-
firstLabels: 100,
263-
lastComments: 10,
264-
lastReplies: 10,
265-
includeIsAnswered: isAnsweredDiscussionFeatureSupported(
266-
notification.account,
267-
),
268-
},
286+
mergedQuery,
287+
mergedVariables,
269288
);
270289
}

src/renderer/utils/api/request.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,35 @@ export async function performGraphQLRequest<TResult, TVariables>(
121121
}) as Promise<ExecutionResultWithHeaders<TResult>>;
122122
}
123123

124+
/**
125+
* Perform a GraphQL API request using a raw query string instead of a TypedDocumentString.
126+
*
127+
* Useful for dynamically composed queries (e.g., merged queries built at runtime).
128+
*/
129+
export async function performGraphQLRequestString<TResult>(
130+
url: Link,
131+
token: Token,
132+
query: string,
133+
variables?: Record<string, unknown>,
134+
): Promise<ExecutionResultWithHeaders<TResult>> {
135+
const headers = await getHeaders(url, token);
136+
137+
return axios({
138+
method: 'POST',
139+
url,
140+
data: {
141+
query,
142+
variables,
143+
},
144+
headers: headers,
145+
}).then((response) => {
146+
return {
147+
...response.data,
148+
headers: response.headers,
149+
} as ExecutionResultWithHeaders<TResult>;
150+
});
151+
}
152+
124153
/**
125154
* Return true if the request should be made with no-cache
126155
*

src/renderer/utils/notifications/handlers/issue.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import type {
1616
SettingsState,
1717
} from '../../../types';
1818
import { IconColor } from '../../../types';
19+
import { fetchIssueByNumber } from '../../api/client';
1920
import {
2021
type IssueDetailsFragment,
2122
IssueDetailsFragmentDoc,
@@ -41,11 +42,13 @@ class IssueHandler extends DefaultHandler {
4142
}
4243

4344
async enrich(
44-
_notification: GitifyNotification,
45+
notification: GitifyNotification,
4546
_settings: SettingsState,
4647
fetchedData?: IssueDetailsFragment,
4748
): Promise<Partial<GitifySubject>> {
48-
const issue = fetchedData;
49+
const issue =
50+
fetchedData ??
51+
(await fetchIssueByNumber(notification)).data.nodeINDEX.issue;
4952

5053
const issueState = issue.stateReason ?? issue.state;
5154

src/renderer/utils/notifications/notifications.ts

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
1-
import axios from 'axios';
2-
31
import type {
42
AccountNotifications,
53
GitifyNotification,
64
GitifyState,
75
GitifySubject,
8-
Link,
96
SettingsState,
107
} from '../../types';
11-
import { listNotificationsForAuthenticatedUser } from '../api/client';
8+
import {
9+
fetchMergedQueryDetails,
10+
listNotificationsForAuthenticatedUser,
11+
} from '../api/client';
1212
import { determineFailureType } from '../api/errors';
1313
import type { TypedDocumentString } from '../api/graphql/generated/graphql';
1414
import {
1515
composeMergedQuery,
1616
extractFragments,
1717
getQueryFragmentBody,
1818
} from '../api/graphql/utils';
19-
import { getHeaders } from '../api/request';
2019
import { transformNotification } from '../api/transform';
21-
import { getGitHubGraphQLUrl, getNumberFromUrl } from '../api/utils';
20+
import { getNumberFromUrl } from '../api/utils';
2221
import { rendererLogError, rendererLogWarn } from '../logger';
2322
import {
2423
filterBaseNotifications,
@@ -237,22 +236,11 @@ export async function enrichNotifications(
237236
let mergedData: Record<string, unknown> | null = null;
238237

239238
try {
240-
const url = getGitHubGraphQLUrl(
241-
notifications[0].account.hostname,
242-
).toString();
243-
const token = notifications[0].account.token;
244-
245-
const headers = await getHeaders(url as Link, token);
246-
247-
const response = await axios({
248-
method: 'POST',
249-
url,
250-
data: {
251-
query: mergedQuery,
252-
variables: queryVariables,
253-
},
254-
headers: headers,
255-
});
239+
const response = await fetchMergedQueryDetails(
240+
notifications[0],
241+
mergedQuery,
242+
queryVariables,
243+
);
256244

257245
mergedData =
258246
(response.data as { data?: Record<string, unknown> })?.data ?? null;

0 commit comments

Comments
 (0)