Repository navigation
Expand file tree
/
Copy pathgraphql.ts
More file actions
969 lines (918 loc) · 36.7 KB
/
Copy pathgraphql.ts
File metadata and controls
969 lines (918 loc) · 36.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
// graphql.ts — a compact GraphQL engine for the subset of GitHub's v4 API
// the forge serves: a tokenizer, a parser for operations/fragments/values,
// and an executor whose resolvers read and write the same store as REST.
//
// It implements real GraphQL mechanics — aliases, variables, input objects,
// enum and list literals, inline fragments, cursor connections, Relay global
// ids, batched operation arrays and field errors as HTTP 200 + `errors` —
// rather than pattern-matching individual documents, so any document built
// from the served schema resolves the same way.
import { Forge, Repo, VIEWER, diffFiles, mergeTrees, type IssueRec, type PullRec } from './store.ts';
import { createPullIn, mergePullIn } from './rest.ts';
// ---------------------------------------------------------------------------
// tokenizer
type Token =
| { kind: 'name'; value: string }
| { kind: 'int'; value: string }
| { kind: 'float'; value: string }
| { kind: 'string'; value: string }
| { kind: 'punct'; value: string }
| { kind: 'eof'; value: '' };
function tokenize(source: string): Token[] {
const tokens: Token[] = [];
let i = 0;
const isNameStart = (c: string): boolean => /[A-Za-z_]/.test(c);
const isNameChar = (c: string): boolean => /[A-Za-z0-9_]/.test(c);
while (i < source.length) {
const c = source[i] as string;
if (c === ' ' || c === '\t' || c === '\n' || c === '\r' || c === ',' || c === '') {
i += 1;
continue;
}
if (c === '#') {
while (i < source.length && source[i] !== '\n') i += 1;
continue;
}
if (source.startsWith('...', i)) {
tokens.push({ kind: 'punct', value: '...' });
i += 3;
continue;
}
if ('!$():=@[]{}|&'.includes(c)) {
tokens.push({ kind: 'punct', value: c });
i += 1;
continue;
}
if (c === '"') {
if (source.startsWith('"""', i)) {
const end = source.indexOf('"""', i + 3);
if (end === -1) throw new Error('unterminated block string');
tokens.push({ kind: 'string', value: source.slice(i + 3, end) });
i = end + 3;
continue;
}
let j = i + 1;
let value = '';
while (j < source.length && source[j] !== '"') {
if (source[j] === '\\') {
const esc = source[j + 1] as string;
const map: Record<string, string> = { '"': '"', '\\': '\\', '/': '/', b: '\b', f: '\f', n: '\n', r: '\r', t: '\t' };
if (esc === 'u') {
value += String.fromCharCode(parseInt(source.slice(j + 2, j + 6), 16));
j += 6;
} else {
value += map[esc] ?? esc;
j += 2;
}
} else {
value += source[j];
j += 1;
}
}
if (j >= source.length) throw new Error('unterminated string');
tokens.push({ kind: 'string', value });
i = j + 1;
continue;
}
if (/[0-9-]/.test(c)) {
let j = i;
let isFloat = false;
while (j < source.length && /[0-9]/.test(source[j] as string)) j += 1;
if (source[j] === '.') {
isFloat = true;
j += 1;
while (j < source.length && /[0-9]/.test(source[j] as string)) j += 1;
}
if (source[j] === 'e' || source[j] === 'E') {
isFloat = true;
j += 1;
if (source[j] === '+' || source[j] === '-') j += 1;
while (j < source.length && /[0-9]/.test(source[j] as string)) j += 1;
}
tokens.push({ kind: isFloat ? 'float' : 'int', value: source.slice(i, j) });
i = j;
continue;
}
if (isNameStart(c)) {
let j = i;
while (j < source.length && isNameChar(source[j] as string)) j += 1;
tokens.push({ kind: 'name', value: source.slice(i, j) });
i = j;
continue;
}
throw new Error(`unexpected character ${JSON.stringify(c)}`);
}
tokens.push({ kind: 'eof', value: '' });
return tokens;
}
// ---------------------------------------------------------------------------
// parser
type ValueNode =
| { kind: 'variable'; name: string }
| { kind: 'int'; value: number }
| { kind: 'float'; value: number }
| { kind: 'string'; value: string }
| { kind: 'boolean'; value: boolean }
| { kind: 'null' }
| { kind: 'enum'; value: string }
| { kind: 'list'; items: ValueNode[] }
| { kind: 'object'; fields: Array<{ name: string; value: ValueNode }> };
interface FieldNode {
kind: 'field';
alias: string | null;
name: string;
args: Array<{ name: string; value: ValueNode }>;
selection: SelectionNode[] | null;
}
type SelectionNode =
| FieldNode
| { kind: 'spread'; name: string }
| { kind: 'inline'; typeCondition: string | null; selection: SelectionNode[] };
interface OperationNode {
type: 'query' | 'mutation';
name: string | null;
selection: SelectionNode[];
}
interface DocumentNode {
operations: OperationNode[];
fragments: Map<string, SelectionNode[]>;
}
class Parser {
private pos = 0;
private readonly tokens: Token[];
constructor(tokens: Token[]) {
this.tokens = tokens;
}
private peek(): Token {
return this.tokens[this.pos] as Token;
}
private next(): Token {
const token = this.tokens[this.pos] as Token;
this.pos += 1;
return token;
}
private expectPunct(value: string): void {
const token = this.next();
if (token.kind !== 'punct' || token.value !== value) throw new Error(`expected ${value}, got ${token.value}`);
}
private skipPunct(value: string): boolean {
const token = this.peek();
if (token.kind === 'punct' && token.value === value) {
this.pos += 1;
return true;
}
return false;
}
private expectName(): string {
const token = this.next();
if (token.kind !== 'name') throw new Error(`expected a name, got ${token.value}`);
return token.value;
}
parseDocument(): DocumentNode {
const operations: OperationNode[] = [];
const fragments = new Map<string, SelectionNode[]>();
while (this.peek().kind !== 'eof') {
const token = this.peek();
if (token.kind === 'punct' && token.value === '{') {
operations.push({ type: 'query', name: null, selection: this.parseSelectionSet() });
continue;
}
const keyword = this.expectName();
if (keyword === 'query' || keyword === 'mutation' || keyword === 'subscription') {
let name: string | null = null;
if (this.peek().kind === 'name') name = this.expectName();
if (this.peek().kind === 'punct' && this.peek().value === '(') this.skipBalancedParens();
this.skipDirectives();
operations.push({ type: keyword === 'mutation' ? 'mutation' : 'query', name, selection: this.parseSelectionSet() });
} else if (keyword === 'fragment') {
const fragName = this.expectName();
const on = this.expectName();
if (on !== 'on') throw new Error('fragment without on');
this.expectName(); // type condition
this.skipDirectives();
fragments.set(fragName, this.parseSelectionSet());
} else {
throw new Error(`unexpected definition ${keyword}`);
}
}
return { operations, fragments };
}
private skipBalancedParens(): void {
this.expectPunct('(');
let depth = 1;
while (depth > 0) {
const token = this.next();
if (token.kind === 'eof') throw new Error('unbalanced parens');
if (token.kind === 'punct' && token.value === '(') depth += 1;
if (token.kind === 'punct' && token.value === ')') depth -= 1;
}
}
private skipDirectives(): void {
while (this.peek().kind === 'punct' && this.peek().value === '@') {
this.next();
this.expectName();
if (this.peek().kind === 'punct' && this.peek().value === '(') this.skipBalancedParens();
}
}
private parseSelectionSet(): SelectionNode[] {
this.expectPunct('{');
const selections: SelectionNode[] = [];
while (!(this.peek().kind === 'punct' && this.peek().value === '}')) {
if (this.peek().kind === 'eof') throw new Error('unterminated selection set');
selections.push(this.parseSelection());
}
this.expectPunct('}');
return selections;
}
private parseSelection(): SelectionNode {
const token = this.peek();
if (token.kind === 'punct' && token.value === '...') {
this.next();
if (this.peek().kind === 'name' && this.peek().value !== 'on') {
return { kind: 'spread', name: this.expectName() };
}
let typeCondition: string | null = null;
if (this.peek().kind === 'name' && this.peek().value === 'on') {
this.next();
typeCondition = this.expectName();
}
this.skipDirectives();
return { kind: 'inline', typeCondition, selection: this.parseSelectionSet() };
}
let name = this.expectName();
let alias: string | null = null;
if (this.skipPunct(':')) {
alias = name;
name = this.expectName();
}
const args: FieldNode['args'] = [];
if (this.peek().kind === 'punct' && this.peek().value === '(') {
this.next();
while (!(this.peek().kind === 'punct' && this.peek().value === ')')) {
const argName = this.expectName();
this.expectPunct(':');
args.push({ name: argName, value: this.parseValue() });
}
this.expectPunct(')');
}
this.skipDirectives();
let selection: SelectionNode[] | null = null;
if (this.peek().kind === 'punct' && this.peek().value === '{') selection = this.parseSelectionSet();
return { kind: 'field', alias, name, args, selection };
}
private parseValue(): ValueNode {
const token = this.next();
if (token.kind === 'punct' && token.value === '$') return { kind: 'variable', name: this.expectName() };
if (token.kind === 'int') return { kind: 'int', value: Number(token.value) };
if (token.kind === 'float') return { kind: 'float', value: Number(token.value) };
if (token.kind === 'string') return { kind: 'string', value: token.value };
if (token.kind === 'punct' && token.value === '[') {
const items: ValueNode[] = [];
while (!(this.peek().kind === 'punct' && this.peek().value === ']')) items.push(this.parseValue());
this.expectPunct(']');
return { kind: 'list', items };
}
if (token.kind === 'punct' && token.value === '{') {
const fields: Array<{ name: string; value: ValueNode }> = [];
while (!(this.peek().kind === 'punct' && this.peek().value === '}')) {
const name = this.expectName();
this.expectPunct(':');
fields.push({ name, value: this.parseValue() });
}
this.expectPunct('}');
return { kind: 'object', fields };
}
if (token.kind === 'name') {
if (token.value === 'true') return { kind: 'boolean', value: true };
if (token.value === 'false') return { kind: 'boolean', value: false };
if (token.value === 'null') return { kind: 'null' };
return { kind: 'enum', value: token.value };
}
throw new Error(`unexpected value token ${token.value}`);
}
}
function valueOf(node: ValueNode, variables: Record<string, unknown>): unknown {
switch (node.kind) {
case 'variable':
return variables[node.name];
case 'int':
case 'float':
case 'string':
case 'boolean':
return node.value;
case 'null':
return null;
case 'enum':
return node.value;
case 'list':
return node.items.map((item) => valueOf(item, variables));
case 'object': {
const out: Record<string, unknown> = {};
for (const field of node.fields) out[field.name] = valueOf(field.value, variables);
return out;
}
}
}
// ---------------------------------------------------------------------------
// global ids
function globalId(kind: string, ...parts: Array<string | number>): string {
return Buffer.from(`${kind}:${parts.join(':')}`, 'utf8').toString('base64');
}
function decodeGlobalId(id: string): { kind: string; parts: string[] } | null {
try {
const text = Buffer.from(id, 'base64').toString('utf8');
const parts = text.split(':');
const kind = parts.shift();
if (!kind) return null;
return { kind, parts };
} catch {
return null;
}
}
// ---------------------------------------------------------------------------
// schema surface for introspection
const TYPE_FIELDS: Record<string, string[]> = {
Query: ['viewer', 'repository', 'node', 'rateLimit'],
Mutation: ['createIssue', 'closeIssue', 'createPullRequest', 'mergePullRequest', 'addComment'],
User: ['id', 'login', 'name', 'email', 'url'],
Repository: [
'id', 'databaseId', 'name', 'nameWithOwner', 'owner', 'defaultBranchRef', 'viewerPermission',
'hasIssuesEnabled', 'hasProjectsEnabled', 'hasWikiEnabled', 'isPrivate', 'description', 'url',
'createdAt', 'updatedAt', 'issue', 'issues', 'pullRequest', 'pullRequests', 'labels', 'refs',
],
Issue: [
'id', 'number', 'title', 'body', 'state', 'author', 'labels', 'comments', 'repository',
'createdAt', 'updatedAt', 'closedAt', 'url',
],
PullRequest: [
'id', 'number', 'title', 'body', 'state', 'merged', 'mergeable', 'mergeCommit', 'author',
'headRefName', 'baseRefName', 'headRef', 'baseRef', 'files', 'commits', 'repository',
'createdAt', 'updatedAt', 'closedAt', 'url',
],
Ref: ['id', 'name', 'prefix', 'target'],
IssueComment: ['id', 'body', 'author', 'createdAt', 'updatedAt', 'url'],
PageInfo: ['hasNextPage', 'hasPreviousPage', 'startCursor', 'endCursor'],
IssueConnection: ['nodes', 'edges', 'pageInfo', 'totalCount'],
PullRequestConnection: ['nodes', 'edges', 'pageInfo', 'totalCount'],
PullRequestChangedFileConnection: ['nodes', 'edges', 'pageInfo', 'totalCount'],
CreateIssuePayload: ['issue', 'clientMutationId'],
CloseIssuePayload: ['issue', 'clientMutationId'],
CreatePullRequestPayload: ['pullRequest', 'clientMutationId'],
MergePullRequestPayload: ['pullRequest', 'clientMutationId'],
AddCommentPayload: ['commentEdge', 'subject', 'clientMutationId'],
IssueCommentEdge: ['node', 'cursor'],
Label: ['id', 'name', 'color'],
LabelConnection: ['nodes', 'pageInfo', 'totalCount'],
};
const ENUM_VALUES: Record<string, string[]> = {
SearchType: ['ISSUE', 'REPOSITORY', 'USER', 'DISCUSSION'],
IssueState: ['OPEN', 'CLOSED'],
PullRequestState: ['OPEN', 'CLOSED', 'MERGED'],
MergeableState: ['MERGEABLE', 'CONFLICTING', 'UNKNOWN'],
RepositoryPermission: ['ADMIN', 'MAINTAIN', 'WRITE', 'TRIAGE', 'READ'],
OrderDirection: ['ASC', 'DESC'],
};
// ---------------------------------------------------------------------------
// execution
interface Ctx {
forge: Forge;
variables: Record<string, unknown>;
fragments: Map<string, SelectionNode[]>;
errors: Array<{ message: string }>;
}
type Source =
| { type: 'User' }
| { type: 'Repository'; repo: Repo }
| { type: 'Issue'; repo: Repo; issue: IssueRec }
| { type: 'PullRequest'; repo: Repo; pull: PullRec }
| { type: 'Ref'; name: string }
| { type: 'IssueComment'; repo: Repo; id: number; body: string }
| { type: 'Connection'; connType: string; items: Source[]; hasNextPage: boolean; endCursor: string | null; totalCount: number }
| { type: 'PageInfo'; hasNextPage: boolean; endCursor: string | null }
| { type: 'Payload'; payloadType: string; value: Source | null }
| { type: 'CommentEdge'; node: Source }
| { type: 'GqlType'; name: string }
| { type: 'GqlField'; name: string }
| { type: 'GqlEnumValue'; name: string }
| { type: 'Label'; repo: Repo; name: string };
const SCALAR = Symbol('scalar');
function cursorOf(number: number): string {
return Buffer.from(`cursor:${number}`, 'utf8').toString('base64');
}
function numberFromCursor(cursor: unknown): number | null {
if (typeof cursor !== 'string' || cursor === '') return null;
try {
const text = Buffer.from(cursor, 'base64').toString('utf8');
const match = /^cursor:(\d+)$/.exec(text);
return match ? Number(match[1]) : null;
} catch {
return null;
}
}
function issuesConnection(repo: Repo, args: Record<string, unknown>): Source {
let issues = [...repo.issues.values()];
const states = args.states;
if (Array.isArray(states) && states.length > 0) {
const wanted = new Set(states.map((state) => String(state).toLowerCase()));
issues = issues.filter((issue) => wanted.has(issue.state));
}
let descending = false;
const orderBy = args.orderBy;
if (orderBy !== null && typeof orderBy === 'object') {
const direction = (orderBy as Record<string, unknown>).direction;
if (direction === 'DESC') descending = true;
if (direction === 'ASC') descending = false;
}
issues.sort((a, b) => (descending ? b.seq - a.seq : a.seq - b.seq));
const total = issues.length;
const afterNumber = numberFromCursor(args.after);
let start = 0;
if (afterNumber !== null) {
const index = issues.findIndex((issue) => issue.number === afterNumber);
if (index !== -1) start = index + 1;
}
const first = typeof args.first === 'number' ? Math.max(0, Math.trunc(args.first)) : total;
const page = issues.slice(start, start + first);
const hasNextPage = start + page.length < total;
const last = page[page.length - 1];
return {
type: 'Connection',
connType: 'IssueConnection',
items: page.map((issue) => ({ type: 'Issue', repo, issue }) as Source),
hasNextPage,
endCursor: last ? cursorOf(last.number) : null,
totalCount: total,
};
}
function pullsConnection(repo: Repo, args: Record<string, unknown>): Source {
let pulls = [...repo.pulls.values()];
const states = args.states;
if (Array.isArray(states) && states.length > 0) {
const wanted = new Set(states.map((state) => String(state).toUpperCase()));
pulls = pulls.filter((pull) => wanted.has(pull.merged ? 'MERGED' : pull.state.toUpperCase()));
}
pulls.sort((a, b) => b.seq - a.seq);
const total = pulls.length;
const first = typeof args.first === 'number' ? Math.max(0, Math.trunc(args.first)) : total;
const page = pulls.slice(0, first);
return {
type: 'Connection',
connType: 'PullRequestConnection',
items: page.map((pull) => ({ type: 'PullRequest', repo, pull }) as Source),
hasNextPage: page.length < total,
endCursor: page.length > 0 ? cursorOf((page[page.length - 1] as PullRec).number) : null,
totalCount: total,
};
}
function pullState(pull: PullRec): string {
return pull.merged ? 'MERGED' : pull.state.toUpperCase();
}
function pullMergeable(repo: Repo, pull: PullRec): string {
if (pull.merged) return 'UNKNOWN';
if (pull.state !== 'open') return 'UNKNOWN';
const baseTip = repo.refCommit(`refs/heads/${pull.baseRef}`);
const headTip = repo.refCommit(`refs/heads/${pull.headRef}`);
if (baseTip === null || headTip === null) return 'UNKNOWN';
const baseCommit = repo.commitOf(baseTip);
const headCommit = repo.commitOf(headTip);
if (!baseCommit || !headCommit) return 'UNKNOWN';
const base = repo.mergeBase(baseTip, headTip);
const ancestorTree = base === null ? null : repo.commitOf(base)?.tree ?? null;
const result = mergeTrees(repo, ancestorTree, baseCommit.tree, headCommit.tree);
return 'conflict' in result ? 'CONFLICTING' : 'MERGEABLE';
}
function filesConnection(repo: Repo, pull: PullRec, args: Record<string, unknown>): Source {
const baseCommit = repo.peelToCommit(pull.baseSha) ?? pull.baseSha;
const headOid = repo.refs.get(`refs/heads/${pull.headRef}`) ?? pull.headSha;
const headCommit = repo.peelToCommit(headOid) ?? headOid;
const changes = diffFiles(repo, repo.filesAt(baseCommit), repo.filesAt(headCommit));
const first = typeof args.first === 'number' ? Math.max(0, Math.trunc(args.first)) : changes.length;
const page = changes.slice(0, first);
return {
type: 'Connection',
connType: 'PullRequestChangedFileConnection',
items: page.map((change) => ({ type: 'ChangedFile', path: change.filename }) as unknown as Source),
hasNextPage: page.length < changes.length,
endCursor: null,
totalCount: changes.length,
};
}
type ChangedFileSource = { type: 'ChangedFile'; path: string };
/** Resolve one field on one source; SCALAR marks a leaf value. */
function resolveField(ctx: Ctx, sourceType: string, source: Source | null, field: string, args: Record<string, unknown>): { value: unknown; type: string } | typeof SCALAR {
const forge = ctx.forge;
// Dispatch on the source's own kind: a connection answers as 'Connection'
// whatever its GraphQL type name (IssueConnection, …), a mutation payload
// as 'Payload', a node(id:) hit as the concrete type it resolved to.
const kind = source === null ? sourceType : source.type;
switch (kind) {
case 'Query': {
switch (field) {
case 'viewer':
return { value: { type: 'User' } as Source, type: 'User' };
case 'repository': {
const repo = forge.repo(String(args.owner ?? ''), String(args.name ?? ''));
return { value: repo === null ? null : ({ type: 'Repository', repo } as Source), type: 'Repository' };
}
case 'node': {
const node = nodeById(ctx, String(args.id ?? ''));
return { value: node, type: node === null ? 'Node' : concreteType(node) };
}
case '__type': {
const name = String(args.name ?? '');
const known = TYPE_FIELDS[name] !== undefined || ENUM_VALUES[name] !== undefined;
return { value: known ? ({ type: 'GqlType', name } as Source) : null, type: '__Type' };
}
case '__schema':
return { value: null, type: '__Schema' };
default:
break;
}
break;
}
case 'Mutation': {
return resolveMutation(ctx, field, args);
}
case 'User': {
switch (field) {
case 'login':
return leaf(VIEWER.login);
case 'id':
return leaf(globalId('User', VIEWER.id));
case 'name':
return leaf(VIEWER.name);
case 'email':
return leaf(VIEWER.email);
default:
break;
}
break;
}
case 'Repository': {
const repo = (source as { repo: Repo }).repo;
switch (field) {
case 'id':
return leaf(globalId('Repository', repo.dbId));
case 'databaseId':
return leaf(repo.dbId);
case 'name':
return leaf(repo.name);
case 'nameWithOwner':
return leaf(repo.fullName);
case 'description':
return leaf(repo.description);
case 'isPrivate':
return leaf(repo.isPrivate);
case 'owner':
return { value: { type: 'User' } as Source, type: 'User' };
case 'defaultBranchRef': {
const tip = repo.refs.get(repo.headRef);
return { value: tip === undefined ? null : ({ type: 'Ref', name: repo.defaultBranch } as Source), type: 'Ref' };
}
case 'viewerPermission':
return leaf('ADMIN');
case 'hasIssuesEnabled':
return leaf(true);
case 'hasProjectsEnabled':
return leaf(true);
case 'hasWikiEnabled':
return leaf(true);
case 'issue': {
const issue = repo.issues.get(Number(args.number));
return { value: issue === undefined ? null : ({ type: 'Issue', repo, issue } as Source), type: 'Issue' };
}
case 'issues':
return { value: issuesConnection(repo, args), type: 'IssueConnection' };
case 'pullRequest': {
const pull = repo.pulls.get(Number(args.number));
return { value: pull === undefined ? null : ({ type: 'PullRequest', repo, pull } as Source), type: 'PullRequest' };
}
case 'pullRequests':
return { value: pullsConnection(repo, args), type: 'PullRequestConnection' };
default:
break;
}
break;
}
case 'Issue': {
const { repo, issue } = source as { repo: Repo; issue: IssueRec };
switch (field) {
case 'id':
return leaf(globalId('Issue', repo.dbId, issue.number));
case 'number':
return leaf(issue.number);
case 'title':
return leaf(issue.title);
case 'body':
return leaf(issue.body);
case 'state':
return leaf(issue.state.toUpperCase());
case 'repository':
return { value: { type: 'Repository', repo } as Source, type: 'Repository' };
case 'author':
return { value: { type: 'User' } as Source, type: 'User' };
default:
break;
}
break;
}
case 'PullRequest': {
const { repo, pull } = source as { repo: Repo; pull: PullRec };
switch (field) {
case 'id':
return leaf(globalId('PullRequest', repo.dbId, pull.number));
case 'number':
return leaf(pull.number);
case 'title':
return leaf(pull.title);
case 'body':
return leaf(pull.body);
case 'state':
return leaf(pullState(pull));
case 'merged':
return leaf(pull.merged);
case 'mergeable':
return leaf(pullMergeable(repo, pull));
case 'headRefName':
return leaf(pull.headRef);
case 'baseRefName':
return leaf(pull.baseRef);
case 'repository':
return { value: { type: 'Repository', repo } as Source, type: 'Repository' };
case 'author':
return { value: { type: 'User' } as Source, type: 'User' };
case 'files':
return { value: filesConnection(repo, pull, args), type: 'PullRequestChangedFileConnection' };
default:
break;
}
break;
}
case 'Ref': {
if (field === 'name') return leaf((source as { name: string }).name);
if (field === 'prefix') return leaf('refs/heads/');
break;
}
case 'IssueComment': {
const comment = source as { repo: Repo; id: number; body: string };
if (field === 'id') return leaf(globalId('IssueComment', comment.repo.dbId, comment.id));
if (field === 'body') return leaf(comment.body);
if (field === 'author') return { value: { type: 'User' } as Source, type: 'User' };
break;
}
case 'ChangedFile': {
if (field === 'path') return leaf((source as unknown as ChangedFileSource).path);
break;
}
case 'Connection': {
const conn = source as Extract<Source, { type: 'Connection' }>;
if (field === 'nodes') return { value: conn.items, type: conn.connType.replace('Connection', '') };
if (field === 'pageInfo') {
return {
value: { type: 'PageInfo', hasNextPage: conn.hasNextPage, endCursor: conn.endCursor } as Source,
type: 'PageInfo',
};
}
if (field === 'totalCount') return leaf(conn.totalCount);
if (field === 'edges') return { value: [], type: 'Edge' };
break;
}
case 'PageInfo': {
const info = source as Extract<Source, { type: 'PageInfo' }>;
if (field === 'hasNextPage') return leaf(info.hasNextPage);
if (field === 'hasPreviousPage') return leaf(false);
if (field === 'endCursor') return leaf(info.endCursor);
if (field === 'startCursor') return leaf(info.endCursor);
break;
}
case 'Payload': {
const payload = source as Extract<Source, { type: 'Payload' }>;
if (field === 'clientMutationId') return leaf(null);
if (payload.payloadType === 'AddCommentPayload' && field === 'commentEdge') {
return { value: payload.value === null ? null : ({ type: 'CommentEdge', node: payload.value } as Source), type: 'IssueCommentEdge' };
}
if (field === 'issue') return { value: payload.value, type: 'Issue' };
if (field === 'pullRequest') return { value: payload.value, type: 'PullRequest' };
if (field === 'subject') return { value: payload.value, type: 'Issue' };
break;
}
case 'CommentEdge': {
if (field === 'node') return { value: (source as Extract<Source, { type: 'CommentEdge' }>).node, type: 'IssueComment' };
if (field === 'cursor') return leaf(cursorOf(0));
break;
}
case 'GqlType': {
const typeName = (source as { name: string }).name;
if (field === 'name') return leaf(typeName);
if (field === 'kind') return leaf(ENUM_VALUES[typeName] !== undefined ? 'ENUM' : 'OBJECT');
if (field === 'fields') {
const names = TYPE_FIELDS[typeName] ?? [];
return { value: names.map((name) => ({ type: 'GqlField', name }) as Source), type: '__Field' };
}
if (field === 'enumValues') {
const names = ENUM_VALUES[typeName] ?? [];
return { value: names.map((name) => ({ type: 'GqlEnumValue', name }) as Source), type: '__EnumValue' };
}
break;
}
case 'GqlField':
case 'GqlEnumValue': {
if (field === 'name') return leaf((source as { name: string }).name);
if (field === 'description') return leaf(null);
if (field === 'isDeprecated') return leaf(false);
if (field === 'deprecationReason') return leaf(null);
break;
}
case 'Label': {
const label = source as Extract<Source, { type: 'Label' }>;
if (field === 'name') return leaf(label.name);
if (field === 'color') return leaf(label.repo.labels.get(label.name) ?? 'ededed');
if (field === 'id') return leaf(globalId('Label', label.repo.dbId, label.name));
break;
}
default:
break;
}
ctx.errors.push({ message: `Cannot query field '${field}' on type '${sourceType}'.` });
return leaf(null);
}
function leaf(value: unknown): { value: unknown; type: string } {
return { value, type: 'Scalar' };
}
function nodeById(ctx: Ctx, id: string): Source | null {
const decoded = decodeGlobalId(id);
if (decoded === null) return null;
if (decoded.kind === 'Repository') {
const dbId = Number(decoded.parts[0]);
for (const repo of ctx.forge.repos.values()) {
if (repo.dbId === dbId) return { type: 'Repository', repo };
}
return null;
}
if (decoded.kind === 'Issue' || decoded.kind === 'PullRequest') {
const dbId = Number(decoded.parts[0]);
const number = Number(decoded.parts[1]);
for (const repo of ctx.forge.repos.values()) {
if (repo.dbId !== dbId) continue;
if (decoded.kind === 'Issue') {
const issue = repo.issues.get(number);
return issue === undefined ? null : { type: 'Issue', repo, issue };
}
const pull = repo.pulls.get(number);
return pull === undefined ? null : { type: 'PullRequest', repo, pull };
}
}
return null;
}
function repoByGlobalId(ctx: Ctx, id: unknown): Repo | null {
if (typeof id !== 'string') return null;
const node = nodeById(ctx, id);
return node !== null && node.type === 'Repository' ? node.repo : null;
}
function resolveMutation(ctx: Ctx, field: string, args: Record<string, unknown>): { value: unknown; type: string } {
const input = (args.input ?? {}) as Record<string, unknown>;
switch (field) {
case 'createIssue': {
const repo = repoByGlobalId(ctx, input.repositoryId);
if (repo === null) {
ctx.errors.push({ message: 'Could not resolve the repository.' });
return leaf(null);
}
const issue = repo.createIssue(String(input.title ?? ''), String(input.body ?? ''), []);
return { value: { type: 'Payload', payloadType: 'CreateIssuePayload', value: { type: 'Issue', repo, issue } as Source } as Source, type: 'CreateIssuePayload' };
}
case 'closeIssue': {
const node = typeof input.issueId === 'string' ? nodeById(ctx, input.issueId) : null;
if (node === null || node.type !== 'Issue') {
ctx.errors.push({ message: 'Could not resolve the issue.' });
return leaf(null);
}
node.issue.state = 'closed';
return { value: { type: 'Payload', payloadType: 'CloseIssuePayload', value: node } as Source, type: 'CloseIssuePayload' };
}
case 'createPullRequest': {
const repo = repoByGlobalId(ctx, input.repositoryId);
if (repo === null) {
ctx.errors.push({ message: 'Could not resolve the repository.' });
return leaf(null);
}
const created = createPullIn(repo, {
title: String(input.title ?? ''),
body: String(input.body ?? ''),
head: String(input.headRefName ?? ''),
base: String(input.baseRefName ?? ''),
});
if ('error' in created) {
ctx.errors.push({ message: created.error });
return leaf(null);
}
return {
value: { type: 'Payload', payloadType: 'CreatePullRequestPayload', value: { type: 'PullRequest', repo, pull: created } as Source } as Source,
type: 'CreatePullRequestPayload',
};
}
case 'mergePullRequest': {
const node = typeof input.pullRequestId === 'string' ? nodeById(ctx, input.pullRequestId) : null;
if (node === null || node.type !== 'PullRequest') {
ctx.errors.push({ message: 'Could not resolve the pull request.' });
return leaf(null);
}
const merged = mergePullIn(node.repo, node.pull);
if ('error' in merged) {
ctx.errors.push({ message: merged.error });
return { value: { type: 'Payload', payloadType: 'MergePullRequestPayload', value: node } as Source, type: 'MergePullRequestPayload' };
}
return { value: { type: 'Payload', payloadType: 'MergePullRequestPayload', value: node } as Source, type: 'MergePullRequestPayload' };
}
case 'addComment': {
const node = typeof input.subjectId === 'string' ? nodeById(ctx, input.subjectId) : null;
if (node === null || node.type !== 'Issue') {
ctx.errors.push({ message: 'Could not resolve the comment subject.' });
return leaf(null);
}
const comment = node.repo.addComment(node.issue.number, String(input.body ?? ''));
const commentSource = { type: 'IssueComment', repo: node.repo, id: comment.id, body: comment.body } as Source;
return { value: { type: 'Payload', payloadType: 'AddCommentPayload', value: commentSource } as Source, type: 'AddCommentPayload' };
}
default:
ctx.errors.push({ message: `Cannot query field '${field}' on type 'Mutation'.` });
return leaf(null);
}
}
/** The concrete type a source answers to (for inline fragments). */
function concreteType(source: Source | null): string {
if (source === null) return '';
switch (source.type) {
case 'Connection':
return source.connType;
case 'Payload':
return source.payloadType;
case 'GqlType':
return '__Type';
case 'GqlField':
return '__Field';
case 'GqlEnumValue':
return '__EnumValue';
case 'CommentEdge':
return 'IssueCommentEdge';
default:
return source.type;
}
}
function executeSelection(ctx: Ctx, sourceType: string, source: Source | null, selections: readonly SelectionNode[]): Record<string, unknown> {
const out: Record<string, unknown> = {};
for (const selection of selections) {
if (selection.kind === 'spread') {
const fragment = ctx.fragments.get(selection.name);
if (fragment) Object.assign(out, executeSelection(ctx, sourceType, source, fragment));
continue;
}
if (selection.kind === 'inline') {
if (selection.typeCondition === null || selection.typeCondition === concreteType(source) || selection.typeCondition === sourceType) {
Object.assign(out, executeSelection(ctx, selection.typeCondition ?? sourceType, source, selection.selection));
}
continue;
}
const key = selection.alias ?? selection.name;
const args: Record<string, unknown> = {};
for (const arg of selection.args) args[arg.name] = valueOf(arg.value, ctx.variables);
const resolved = resolveField(ctx, sourceType, source, selection.name, args);
if (resolved === SCALAR || resolved.type === 'Scalar') {
out[key] = resolved === SCALAR ? null : resolved.value;
continue;
}
const value = resolved.value as Source | Source[] | null;
if (value === null) {
out[key] = null;
} else if (Array.isArray(value)) {
out[key] = value.map((item) =>
selection.selection ? executeSelection(ctx, resolved.type, item, selection.selection) : item,
);
} else {
out[key] = selection.selection ? executeSelection(ctx, resolved.type, value, selection.selection) : value;
}
}
return out;
}
export interface GraphQLResult {
data: Record<string, unknown> | null;
errors?: Array<{ message: string }>;
}
/** Execute one operation document (or, given an array body upstream, one entry of it). */
export function executeGraphQL(forge: Forge, query: string, variables: Record<string, unknown>, operationName?: string): GraphQLResult {
let document: DocumentNode;
try {
document = new Parser(tokenize(query)).parseDocument();
} catch (err) {
return { data: null, errors: [{ message: err instanceof Error ? err.message : 'parse error' }] };
}
const operation =
document.operations.find((op) => operationName === undefined || op.name === operationName) ?? document.operations[0];
if (!operation) return { data: null, errors: [{ message: 'No operation in the document.' }] };
const ctx: Ctx = { forge, variables, fragments: document.fragments, errors: [] };
const rootType = operation.type === 'mutation' ? 'Mutation' : 'Query';
const data = executeSelection(ctx, rootType, null, operation.selection);
const result: GraphQLResult = { data };
if (ctx.errors.length > 0) result.errors = ctx.errors;
return result;
}