Skip to content

Commit 216f208

Browse files
refactor(server): GitHub GraphQL batches use variables and share one pager
Aliased batch reads (stats, summaries, watch fingerprints, stack memberships, stack branch access) wrote owners, names and numbers into the document and relied on a regex to keep that safe. They now go through aliasedGraphQlDocument, which passes every value as a per-alias variable. Cursor-paging loops share readGraphQlPages, which also stops on a cursor GitHub repeats instead of re-reading the same page. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 1c8a6c8 commit 216f208

7 files changed

Lines changed: 483 additions & 247 deletions

File tree

‎apps/server/src/pullRequest/GitHubPullRequestApi.test.ts‎

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -767,11 +767,15 @@ layer("GitHubPullRequestApi.layer", (it) => {
767767
);
768768
assert.strictEqual(eight?.headBranch, "feat/8");
769769
expect(mockedExecute).toHaveBeenCalledOnce();
770-
const document = queryAt(0);
771-
expect(document).toContain(
772-
's0: repository(owner: "acme", name: "web") { pullRequest(number: 7)',
770+
expect(queryAt(0)).toContain(
771+
"s0: repository(owner: $s0_owner, name: $s0_name) { pullRequest(number: $s0_number)",
773772
);
774-
expect(document).toContain("pullRequest(number: 8)");
773+
expect(varsAt(0)).toMatchObject({
774+
s0_owner: "acme",
775+
s0_name: "web",
776+
s0_number: 7,
777+
s1_number: 8,
778+
});
775779
}),
776780
);
777781

@@ -815,10 +819,10 @@ layer("GitHubPullRequestApi.layer", (it) => {
815819
// GitHub had no answer for #8, so its watch reads it in full.
816820
expect(eight).toBeNull();
817821
expect(mockedExecute).toHaveBeenCalledOnce();
818-
const call = callAt(0);
819-
expect(call.kind === "graphql" ? call.query : "").toContain(
820-
'w1: repository(owner: "acme", name: "web") { pullRequest(number: 8)',
822+
expect(queryAt(0)).toContain(
823+
"w1: repository(owner: $w1_owner, name: $w1_name) { pullRequest(number: $w1_number)",
821824
);
825+
expect(varsAt(0)).toMatchObject({ w1_owner: "acme", w1_name: "web", w1_number: 8 });
822826
}),
823827
);
824828

@@ -1408,19 +1412,20 @@ layer("GitHubPullRequestApi.layer", (it) => {
14081412
expect(batch.continues).toBe(false);
14091413
expect(mockedStackMemberships).toHaveBeenCalledTimes(1);
14101414
const membership = mockedStackMemberships.mock.calls[0]?.[0];
1411-
const query = membership?.kind === "graphql" ? membership.query : "";
1412-
expect(query).toContain("pullRequest(number: 4)");
1413-
expect(query).toContain("pullRequest(number: 5)");
1414-
expect(query).not.toContain("pullRequest(number: 1)");
1415-
expect(query).not.toContain("pullRequest(number: 6)");
1415+
const numbers = Object.entries(
1416+
membership?.kind === "graphql" ? (membership.variables ?? {}) : {},
1417+
)
1418+
.filter(([name]) => name.endsWith("_number"))
1419+
.map(([, number]) => number);
1420+
expect(numbers).toEqual([4, 5]);
14161421
}),
14171422
);
14181423

14191424
it.effect("batches membership reads and keeps successful rows when one chunk fails", () =>
14201425
Effect.gen(function* () {
14211426
mockedExecute.mockReturnValueOnce(Effect.succeed(output(pullRequests(27, 1))));
14221427
mockedStackMemberships.mockImplementation((input) =>
1423-
input.kind === "graphql" && input.query.includes("pullRequest(number: 26)")
1428+
input.kind === "graphql" && Object.values(input.variables ?? {}).includes(26)
14241429
? Effect.fail(
14251430
new GitHubApi.GitHubApiResponseError({
14261431
host: "github.com",
@@ -1513,9 +1518,8 @@ layer("GitHubPullRequestApi.layer", (it) => {
15131518
{ repository: "acme/web", number: 1, additions: 4, deletions: 1 },
15141519
{ repository: "acme/web", number: 26, additions: 4, deletions: 1 },
15151520
]);
1516-
const document = queryAt(0);
1517-
expect(document).toContain('s0: repository(owner: "acme", name: "web")');
1518-
expect(document).toContain("pullRequest(number: 25)");
1521+
expect(queryAt(0)).toContain("s0: repository(owner: $s0_owner, name: $s0_name)");
1522+
expect(varsAt(0)).toMatchObject({ s0_owner: "acme", s0_name: "web", s24_number: 25 });
15191523
}),
15201524
);
15211525

@@ -4386,9 +4390,9 @@ layer("GitHubPullRequestApi.layer", (it) => {
43864390
number: 7,
43874391
});
43884392

4389-
assert.strictEqual(mockedExecute.mock.calls.length, 5);
4393+
// The same cursor twice is a page GitHub already gave, so reading stops there.
4394+
assert.strictEqual(mockedExecute.mock.calls.length, 2);
43904395
assert.isTrue(viewed.truncated);
4391-
assert.strictEqual(viewed.files.length, 5);
43924396
}),
43934397
);
43944398

@@ -4420,8 +4424,8 @@ layer("GitHubPullRequestApi.layer", (it) => {
44204424
expect(queryAt(1)).toContain("f1: unmarkFileAsViewed");
44214425
expect(varsAt(1)).toEqual({
44224426
pullRequestId: "PR_1",
4423-
path0: "src/a.ts",
4424-
path1: "src/b.ts",
4427+
f0_path: "src/a.ts",
4428+
f1_path: "src/b.ts",
44254429
});
44264430
}),
44274431
);

0 commit comments

Comments
 (0)