Skip to content

Commit b3e76c9

Browse files
committed
feat(api): merge query
Signed-off-by: Adam Setch <[email protected]>
1 parent 256328c commit b3e76c9

File tree

2 files changed

+85
-31
lines changed

2 files changed

+85
-31
lines changed

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

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,13 @@ import {
1919
} from '../../../types';
2020
import type { Notification, Subject } from '../../../typesGitHub';
2121
import { fetchPullByNumber } from '../../api/client';
22-
import {
23-
FetchPullRequestByNumberDocument,
24-
type PullRequestReviewFieldsFragment,
25-
} from '../../api/graphql/generated/graphql';
26-
import { getNumberFromUrl } from '../../api/utils';
22+
import type { PullRequestReviewFieldsFragment } from '../../api/graphql/generated/graphql';
2723
import { DefaultHandler, defaultHandler } from './default';
2824
import { getNotificationAuthor } from './utils';
2925

3026
class PullRequestHandler extends DefaultHandler {
3127
readonly type = 'PullRequest' as const;
3228

33-
query(notification: Notification) {
34-
const number = getNumberFromUrl(notification.subject.url);
35-
36-
return {
37-
query: FetchPullRequestByNumberDocument,
38-
variables: {
39-
owner: notification.repository.owner.login,
40-
name: notification.repository.name,
41-
number: number,
42-
firstLabels: 100,
43-
firstClosingIssues: 100,
44-
lastComments: 1,
45-
lastReviews: 100,
46-
},
47-
};
48-
}
49-
5029
async enrich(
5130
notification: Notification,
5231
_settings: SettingsState,

src/renderer/utils/notifications/notifications.ts

Lines changed: 84 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
import { parse } from 'graphql';
2-
import combineQuery from 'graphql-combine-query';
1+
import axios from 'axios';
32

43
import type {
54
AccountNotifications,
65
GitifyState,
76
GitifySubject,
7+
Link,
88
SettingsState,
99
} from '../../types';
1010
import type { Notification } from '../../typesGitHub';
1111
import { listNotificationsForAuthenticatedUser } from '../api/client';
1212
import { determineFailureType } from '../api/errors';
13+
import { getHeaders } from '../api/request';
14+
import { getGitHubGraphQLUrl, getNumberFromUrl } from '../api/utils';
1315
import { rendererLogError, rendererLogWarn } from '../logger';
1416
import {
1517
filterBaseNotifications,
@@ -133,20 +135,93 @@ export async function enrichNotifications(
133135
}
134136

135137
// Build combined query for pull requests (builder is immutable)
136-
const buildQuery = combineQuery('PullRequestBatch');
138+
let mergedQuery = '';
139+
let i = 0;
140+
const args = [];
137141

138142
for (const notification of notifications) {
139143
if (notification.subject.type === 'PullRequest') {
140-
const handler = createNotificationHandler(notification);
141-
const queryData = handler.query(notification);
144+
const org = notification.repository.owner.login;
145+
const repo = notification.repository.name;
146+
const number = getNumberFromUrl(notification.subject.url);
142147

143-
if (queryData?.query && queryData?.variables) {
144-
buildQuery.addN(parse(queryData.query), queryData.variables);
145-
}
148+
args.push({
149+
org: org,
150+
repo: repo,
151+
number: number,
152+
});
153+
154+
mergedQuery += `repo${i}: repository(owner: $owner${i}, name: $name${i}) {
155+
pullRequest(number: $number${i}) {
156+
title
157+
}
158+
}\n`;
159+
160+
i += 1;
161+
162+
// const handler = createNotificationHandler(notification);
163+
// const queryData = handler.query(notification);
146164
}
147165
}
148166

149-
// console.log('ADAM COMBINED QUERY: ', JSON.stringify(buildQuery, null, 2));
167+
let queryArgs = '';
168+
let queryArgsVariables = {};
169+
170+
for (let idx = 0; idx < args.length; idx++) {
171+
const arg = args[idx];
172+
if (idx > 0) {
173+
queryArgs += ', ';
174+
}
175+
queryArgs += `$owner${idx}: String!, $name${idx}: String!, $number${idx}: Int!`;
176+
queryArgsVariables = {
177+
...queryArgsVariables,
178+
[`owner${idx}`]: arg.org,
179+
[`name${idx}`]: arg.repo,
180+
[`number${idx}`]: arg.number,
181+
};
182+
}
183+
184+
mergedQuery = `query JumboQuery(${queryArgs}) {\n${mergedQuery}}\n`;
185+
186+
console.log('ADAM COMBINED QUERY ', JSON.stringify(mergedQuery, null, 2));
187+
console.log(
188+
'ADAM COMBINED ARGS ',
189+
JSON.stringify(queryArgsVariables, null, 2),
190+
);
191+
192+
try {
193+
const url = getGitHubGraphQLUrl(
194+
notifications[0].account.hostname,
195+
).toString();
196+
const token = notifications[0].account.token;
197+
198+
const headers = await getHeaders(url as Link, token);
199+
200+
axios({
201+
method: 'POST',
202+
url,
203+
data: {
204+
query: mergedQuery,
205+
variables: queryArgsVariables,
206+
},
207+
headers: headers,
208+
}).then((response) => {
209+
console.log('ADAM RESPONSE ', JSON.stringify(response, null, 2));
210+
});
211+
} catch (err) {
212+
console.error('oops');
213+
}
214+
215+
// const headers = await getHeaders(url.toString() as Link, token);
216+
217+
// await axios.post(url.toString(), combined, { headers });
218+
// } catch (err) {
219+
// rendererLogError(
220+
// 'enrichNotifications',
221+
// 'failed to fetch batch pull request details',
222+
// err,
223+
// );
224+
// }
150225

151226
const enrichedNotifications = await Promise.all(
152227
notifications.map(async (notification: Notification) => {

0 commit comments

Comments
 (0)