Skip to content

Commit 6340374

Browse files
committed
feat(api): codegen for discussions
Signed-off-by: Adam Setch <[email protected]>
1 parent 2174ebd commit 6340374

File tree

4 files changed

+103
-65
lines changed

4 files changed

+103
-65
lines changed

src/renderer/utils/api/__mocks__/response-mocks.ts

Lines changed: 88 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import {
33
mockGitHubEnterpriseServerAccount,
44
} from '../../../__mocks__/account-mocks';
55
import type { Link } from '../../../types';
6-
import type {
7-
DiscussionAuthor,
8-
Notification,
9-
Repository,
10-
User,
11-
} from '../../../typesGitHub';
6+
import type { Notification, Repository, User } from '../../../typesGitHub';
7+
import { makeFragmentData } from '../graphql/generated/fragment-masking';
128
import type { FetchDiscussionsQuery } from '../graphql/generated/graphql';
13-
import type { GitHubGraphQLResponse } from '../types';
9+
import {
10+
CommentFieldsFragmentDoc,
11+
DiscussionCommentFieldsFragmentDoc,
12+
DiscussionFieldsFragmentDoc,
13+
} from '../graphql/generated/graphql';
1414

1515
export const mockNotificationUser: User = {
1616
login: 'octocat',
@@ -374,72 +374,105 @@ export const mockEnterpriseNotifications: Notification[] = [
374374
},
375375
];
376376

377-
const mockDiscussionAuthor: DiscussionAuthor = {
378-
login: 'comment-user',
379-
url: 'https://github.com/comment-user' as Link,
380-
avatar_url: 'https://avatars.githubusercontent.com/u/123456789?v=4' as Link,
381-
type: 'User',
377+
const mockDiscussionAuthor = {
378+
__typename: 'User' as const,
379+
' $fragmentRefs': {
380+
AuthorFields_User_Fragment: {
381+
__typename: 'User' as const,
382+
login: 'comment-user',
383+
url: 'https://github.com/comment-user' as Link,
384+
avatar_url:
385+
'https://avatars.githubusercontent.com/u/123456789?v=4' as Link,
386+
type: 'User' as const,
387+
},
388+
},
382389
};
383390

384-
const mockDiscussionReplier: DiscussionAuthor = {
385-
login: 'reply-user',
386-
url: 'https://github.com/reply-user' as Link,
387-
avatar_url: 'https://avatars.githubusercontent.com/u/123456789?v=4' as Link,
388-
type: 'User',
391+
const mockDiscussionReplier = {
392+
__typename: 'User' as const,
393+
' $fragmentRefs': {
394+
AuthorFields_User_Fragment: {
395+
__typename: 'User' as const,
396+
login: 'reply-user',
397+
url: 'https://github.com/reply-user' as Link,
398+
avatar_url:
399+
'https://avatars.githubusercontent.com/u/123456789?v=4' as Link,
400+
type: 'User' as const,
401+
},
402+
},
389403
};
390404

391-
export const mockDiscussionComments: DiscussionComments = {
405+
const mockDiscussionComments = {
392406
nodes: [
393-
{
394-
databaseId: 2258799,
395-
createdAt: '2017-02-20T17:51:57Z',
396-
author: mockDiscussionAuthor,
397-
replies: {
398-
nodes: [
399-
{
400-
databaseId: 2300902,
401-
createdAt: '2017-05-20T17:51:57Z',
402-
author: mockDiscussionReplier,
403-
},
404-
],
407+
makeFragmentData(
408+
{
409+
__typename: 'DiscussionComment' as const,
410+
databaseId: 2258799,
411+
createdAt: '2017-02-20T17:51:57Z',
412+
author: mockDiscussionAuthor,
413+
replies: {
414+
__typename: 'DiscussionCommentConnection' as const,
415+
nodes: [
416+
makeFragmentData(
417+
{
418+
__typename: 'DiscussionComment' as const,
419+
databaseId: 2300902,
420+
createdAt: '2017-05-20T17:51:57Z',
421+
author: mockDiscussionReplier,
422+
},
423+
CommentFieldsFragmentDoc,
424+
),
425+
],
426+
},
405427
},
406-
},
428+
DiscussionCommentFieldsFragmentDoc,
429+
),
407430
],
408431
totalCount: 2,
409432
};
410433

411-
export const mockDiscussionLabels: DiscussionLabels = {
434+
const mockDiscussionLabels = {
435+
__typename: 'LabelConnection' as const,
412436
nodes: [
413437
{
438+
__typename: 'Label' as const,
414439
name: 'enhancement',
415440
},
416441
],
417442
};
418443

419-
export const mockGraphQLResponse: GitHubGraphQLResponse<FetchDiscussionsQuery> =
420-
{
421-
data: {
422-
search: {
423-
nodes: [
424-
{
425-
number: 123,
426-
title: '1.16.0',
427-
isAnswered: false,
428-
stateReason: 'OPEN',
429-
url: 'https://github.com/gitify-app/notifications-test/discussions/612' as Link,
430-
author: {
431-
login: 'discussion-creator',
432-
url: 'https://github.com/discussion-creator' as Link,
433-
avatar_url:
434-
'https://avatars.githubusercontent.com/u/123456789?v=4' as Link,
435-
type: 'User',
444+
export const mockGraphQLResponse: FetchDiscussionsQuery = {
445+
search: {
446+
__typename: 'SearchResultItemConnection',
447+
nodes: [
448+
makeFragmentData(
449+
{
450+
__typename: 'Discussion' as const,
451+
number: 123,
452+
title: '1.16.0',
453+
isAnswered: false,
454+
stateReason: null,
455+
url: 'https://github.com/gitify-app/notifications-test/discussions/612' as Link,
456+
author: {
457+
__typename: 'User' as const,
458+
' $fragmentRefs': {
459+
AuthorFields_User_Fragment: {
460+
__typename: 'User' as const,
461+
login: 'discussion-creator',
462+
url: 'https://github.com/discussion-creator' as Link,
463+
avatar_url:
464+
'https://avatars.githubusercontent.com/u/123456789?v=4' as Link,
465+
type: 'User' as const,
466+
},
436467
},
437-
comments: mockDiscussionComments,
438-
labels: mockDiscussionLabels,
439468
},
440-
],
441-
},
442-
},
443-
};
469+
comments: mockDiscussionComments,
470+
labels: mockDiscussionLabels,
471+
},
472+
DiscussionFieldsFragmentDoc,
473+
),
474+
],
475+
},
476+
};
444477

445478
export const mockSingleNotification: Notification = mockGitHubNotifications[0];

src/renderer/utils/api/client.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {
2929
type MeQuery,
3030
} from './graphql/generated/graphql';
3131
import { formatAsGitHubSearchSyntax } from './graphql/utils';
32-
import { apiRequestAuth, performRequestForAccount } from './request';
32+
import { apiRequestAuth, performGraphQLRequest } from './request';
3333
import type { GitHubGraphQLResponse } from './types';
3434
import { getGitHubAPIBaseUrl, getGitHubGraphQLUrl } from './utils';
3535

@@ -244,7 +244,7 @@ export async function searchDiscussions(
244244
): Promise<GitHubGraphQLResponse<FetchDiscussionsQuery>> {
245245
const url = getGitHubGraphQLUrl(notification.account.hostname);
246246

247-
return performRequestForAccount(
247+
return performGraphQLRequest(
248248
url.toString() as Link,
249249
notification.account.token,
250250
FetchDiscussionsDocument,
@@ -283,7 +283,7 @@ export async function searchDiscussionsv2(
283283
}
284284
`);
285285

286-
return performRequestForAccount(
286+
return performGraphQLRequest(
287287
url.toString() as Link,
288288
notification.account.token,
289289
Me,

src/renderer/utils/api/request.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export async function apiRequestAuth(
9090
* @param variables
9191
* @returns
9292
*/
93-
export async function performRequestForAccount<TResult, TVariables>(
93+
export async function performGraphQLRequest<TResult, TVariables>(
9494
url: Link,
9595
token: Token,
9696
query: TypedDocumentString<TResult, TVariables>,

src/renderer/utils/helpers.test.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,11 @@ describe('renderer/utils/helpers.ts', () => {
273273
});
274274

275275
describe('Discussions URLs', () => {
276+
const performGraphQLRequestSpy = jest.spyOn(
277+
apiRequests,
278+
'performGraphQLRequest',
279+
);
280+
276281
it('when no subject urls and no discussions found via query, default to linking to repository discussions', async () => {
277282
const subject = {
278283
title: 'generate github web url unit tests',
@@ -281,7 +286,7 @@ describe('renderer/utils/helpers.ts', () => {
281286
type: 'Discussion' as SubjectType,
282287
};
283288

284-
apiRequestAuthSpy.mockResolvedValue({
289+
performGraphQLRequestSpy.mockResolvedValue({
285290
data: { data: { search: { nodes: [] } } },
286291
} as AxiosResponse);
287292

@@ -290,7 +295,7 @@ describe('renderer/utils/helpers.ts', () => {
290295
subject: subject,
291296
});
292297

293-
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(1);
298+
expect(performGraphQLRequestSpy).toHaveBeenCalledTimes(1);
294299
expect(result).toBe(
295300
`${mockSingleNotification.repository.html_url}/discussions?${mockNotificationReferrer}`,
296301
);
@@ -304,7 +309,7 @@ describe('renderer/utils/helpers.ts', () => {
304309
type: 'Discussion' as SubjectType,
305310
};
306311

307-
apiRequestAuthSpy.mockResolvedValue({
312+
performGraphQLRequestSpy.mockResolvedValue({
308313
data: {
309314
...mockGraphQLResponse,
310315
},
@@ -315,7 +320,7 @@ describe('renderer/utils/helpers.ts', () => {
315320
subject: subject,
316321
});
317322

318-
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(1);
323+
expect(performGraphQLRequestSpy).toHaveBeenCalledTimes(1);
319324
expect(result).toBe(
320325
`https://github.com/gitify-app/notifications-test/discussions/612?${mockNotificationReferrer}#discussioncomment-2300902`,
321326
);
@@ -333,14 +338,14 @@ describe('renderer/utils/helpers.ts', () => {
333338
type: 'Discussion' as SubjectType,
334339
};
335340

336-
apiRequestAuthSpy.mockResolvedValue(null as AxiosResponse);
341+
performGraphQLRequestSpy.mockResolvedValue(null as AxiosResponse);
337342

338343
const result = await generateGitHubWebUrl({
339344
...mockSingleNotification,
340345
subject: subject,
341346
});
342347

343-
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(1);
348+
expect(performGraphQLRequestSpy).toHaveBeenCalledTimes(1);
344349
expect(result).toBe(
345350
`https://github.com/gitify-app/notifications-test/discussions?${mockNotificationReferrer}`,
346351
);

0 commit comments

Comments
 (0)