Skip to content

Commit caf1476

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

File tree

1 file changed

+36
-36
lines changed

1 file changed

+36
-36
lines changed

src/renderer/utils/notifications/notifications.ts

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type {
1010
import type { Notification } from '../../typesGitHub';
1111
import { listNotificationsForAuthenticatedUser } from '../api/client';
1212
import { determineFailureType } from '../api/errors';
13+
import { PullRequestDetailsFragmentDoc } from '../api/graphql/generated/graphql';
1314
import { getHeaders } from '../api/request';
1415
import { getGitHubGraphQLUrl, getNumberFromUrl } from '../api/utils';
1516
import { rendererLogError, rendererLogWarn } from '../logger';
@@ -134,10 +135,10 @@ export async function enrichNotifications(
134135
return notifications;
135136
}
136137

137-
// Build combined query for pull requests (builder is immutable)
138+
// Build combined query for pull requests
138139
let mergedQuery = '';
139140
let i = 0;
140-
const args = [];
141+
const args: Array<{ org: string; repo: string; number: number }> = [];
141142

142143
for (const notification of notifications) {
143144
if (notification.subject.type === 'PullRequest') {
@@ -146,48 +147,58 @@ export async function enrichNotifications(
146147
const number = getNumberFromUrl(notification.subject.url);
147148

148149
args.push({
149-
org: org,
150-
repo: repo,
151-
number: number,
150+
org,
151+
repo,
152+
number,
152153
});
153154

154-
mergedQuery += `repo${i}: repository(owner: $owner${i}, name: $name${i}) {
155+
mergedQuery += `pr${i}: repository(owner: $owner${i}, name: $name${i}) {
155156
pullRequest(number: $number${i}) {
156-
title
157+
...PullRequestDetails
157158
}
158159
}\n`;
159160

160161
i += 1;
161-
162-
// const handler = createNotificationHandler(notification);
163-
// const queryData = handler.query(notification);
164162
}
165163
}
166164

165+
// If no pull requests, return early
166+
if (args.length === 0) {
167+
const enrichedNotifications = await Promise.all(
168+
notifications.map(async (notification: Notification) => {
169+
return enrichNotification(notification, settings);
170+
}),
171+
);
172+
return enrichedNotifications;
173+
}
174+
167175
let queryArgs = '';
168-
let queryArgsVariables = {};
176+
const queryArgsVariables: Record<string, string | number> = {};
169177

170178
for (let idx = 0; idx < args.length; idx++) {
171179
const arg = args[idx];
172180
if (idx > 0) {
173181
queryArgs += ', ';
174182
}
175183
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-
};
184+
queryArgsVariables[`owner${idx}`] = arg.org;
185+
queryArgsVariables[`name${idx}`] = arg.repo;
186+
queryArgsVariables[`number${idx}`] = arg.number;
182187
}
183188

184-
mergedQuery = `query JumboQuery(${queryArgs}) {\n${mergedQuery}}\n`;
189+
// Add variables from PullRequestDetailsFragment
190+
queryArgs +=
191+
', $firstLabels: Int, $lastComments: Int, $lastReviews: Int, $firstClosingIssues: Int';
192+
queryArgsVariables['firstLabels'] = 100;
193+
queryArgsVariables['lastComments'] = 100;
194+
queryArgsVariables['lastReviews'] = 100;
195+
queryArgsVariables['firstClosingIssues'] = 100;
185196

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-
);
197+
const fragmentQuery = PullRequestDetailsFragmentDoc.toString();
198+
mergedQuery = `query FetchMergedPullRequests(${queryArgs}) {\n${mergedQuery}}\n\n${fragmentQuery}`;
199+
200+
console.log('MERGED QUERY ', JSON.stringify(mergedQuery, null, 2));
201+
console.log('MERGED ARGS ', JSON.stringify(queryArgsVariables, null, 2));
191202

192203
try {
193204
const url = getGitHubGraphQLUrl(
@@ -206,23 +217,12 @@ export async function enrichNotifications(
206217
},
207218
headers: headers,
208219
}).then((response) => {
209-
console.log('ADAM RESPONSE ', JSON.stringify(response, null, 2));
220+
console.log('MERGED RESPONSE ', JSON.stringify(response, null, 2));
210221
});
211222
} catch (err) {
212-
console.error('oops');
223+
console.error('Failed to fetch merged pull request details', err);
213224
}
214225

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-
// }
225-
226226
const enrichedNotifications = await Promise.all(
227227
notifications.map(async (notification: Notification) => {
228228
return enrichNotification(notification, settings);

0 commit comments

Comments
 (0)