-
Notifications
You must be signed in to change notification settings - Fork 2.7k
SSR functions take batchOptions to modulate query batching
#12767
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@apollo/client": minor | ||
| --- | ||
|
|
||
| SSR functions take batchOptions, to modulate query batching |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
167 changes: 167 additions & 0 deletions
167
src/react/ssr/__tests__/RenderPromises.debounce.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| import { RenderPromises } from "../RenderPromises.js"; | ||
| import type { QueryDataOptions } from "../../types/types.js"; | ||
| import { Kind } from "graphql"; | ||
|
|
||
| describe("RenderPromises with debounced batching", () => { | ||
| let renderPromises: RenderPromises; | ||
|
|
||
| beforeEach(() => { | ||
| renderPromises = new RenderPromises(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| renderPromises.stop(); | ||
| }); | ||
|
|
||
| it("should resolve on first promise in debounced mode", async () => { | ||
| const fastPromise = new Promise<void>((resolve) => { | ||
| setTimeout(() => resolve(), 10); | ||
| }); | ||
|
|
||
| const slowPromise = new Promise<void>((resolve) => { | ||
| setTimeout(() => resolve(), 100); | ||
| }); | ||
|
|
||
| const mockOptions1: QueryDataOptions<any, any> = { | ||
| query: { kind: Kind.DOCUMENT, definitions: [] }, | ||
| variables: { id: 1 } | ||
| }; | ||
|
|
||
| const mockOptions2: QueryDataOptions<any, any> = { | ||
| query: { kind: Kind.DOCUMENT, definitions: [] }, | ||
| variables: { id: 2 } | ||
| }; | ||
|
|
||
| // Add promises to the map directly for testing | ||
| (renderPromises as any).queryPromises.set(mockOptions1, fastPromise); | ||
| (renderPromises as any).queryPromises.set(mockOptions2, slowPromise); | ||
|
|
||
| const startTime = Date.now(); | ||
|
|
||
| await renderPromises.consumeAndAwaitPromises({ | ||
| batchOptions: { debounce: 50 } | ||
| }); | ||
|
|
||
| const endTime = Date.now(); | ||
| const duration = endTime - startTime; | ||
|
|
||
| // Should resolve quickly (around 10ms) not wait for the slow promise | ||
| expect(duration).toBeLessThan(50); | ||
| }); | ||
|
|
||
| it("should handle promise rejections correctly in debounced mode", async () => { | ||
| const fastPromise = Promise.resolve(); | ||
| const failingPromise = Promise.reject(new Error("Test error")); | ||
|
|
||
| const mockOptions1: QueryDataOptions<any, any> = { | ||
| query: { kind: Kind.DOCUMENT, definitions: [] }, | ||
| variables: { id: 1 } | ||
| }; | ||
|
|
||
| const mockOptions2: QueryDataOptions<any, any> = { | ||
| query: { kind: Kind.DOCUMENT, definitions: [] }, | ||
| variables: { id: 2 } | ||
| }; | ||
|
|
||
| (renderPromises as any).queryPromises.set(mockOptions1, fastPromise); | ||
| (renderPromises as any).queryPromises.set(mockOptions2, failingPromise); | ||
|
|
||
| // Should resolve successfully because fastPromise resolves first | ||
| await expect( | ||
| renderPromises.consumeAndAwaitPromises({ | ||
| batchOptions: { debounce: 10 } | ||
| }) | ||
| ).resolves.toBeUndefined(); | ||
| }); | ||
|
|
||
| it("should reject when all promises fail in debounced mode", async () => { | ||
| const failingPromise1 = Promise.reject(new Error("Error 1")); | ||
| const failingPromise2 = Promise.reject(new Error("Error 2")); | ||
|
|
||
| const mockOptions1: QueryDataOptions<any, any> = { | ||
| query: { kind: Kind.DOCUMENT, definitions: [] }, | ||
| variables: { id: 1 } | ||
| }; | ||
|
|
||
| const mockOptions2: QueryDataOptions<any, any> = { | ||
| query: { kind: Kind.DOCUMENT, definitions: [] }, | ||
| variables: { id: 2 } | ||
| }; | ||
|
|
||
| (renderPromises as any).queryPromises.set(mockOptions1, failingPromise1); | ||
| (renderPromises as any).queryPromises.set(mockOptions2, failingPromise2); | ||
|
|
||
| // Should reject when all promises fail | ||
| await expect( | ||
| renderPromises.consumeAndAwaitPromises({ | ||
| batchOptions: { debounce: 10 } | ||
| }) | ||
| ).rejects.toThrow("All 2 queries failed during SSR"); | ||
| }); | ||
|
|
||
| it("should timeout when debounce expires", async () => { | ||
| const slowPromise = new Promise<void>((resolve) => { | ||
| setTimeout(() => resolve(), 100); | ||
| }); | ||
|
|
||
| const mockOptions: QueryDataOptions<any, any> = { | ||
| query: { kind: Kind.DOCUMENT, definitions: [] }, | ||
| variables: { id: 1 } | ||
| }; | ||
|
|
||
| (renderPromises as any).queryPromises.set(mockOptions, slowPromise); | ||
|
|
||
| const startTime = Date.now(); | ||
|
|
||
| await renderPromises.consumeAndAwaitPromises({ | ||
| batchOptions: { debounce: 20 } | ||
| }); | ||
|
|
||
| const endTime = Date.now(); | ||
| const duration = endTime - startTime; | ||
|
|
||
| // Should timeout around 20ms, not wait for the 100ms promise | ||
| expect(duration).toBeGreaterThanOrEqual(15); | ||
| expect(duration).toBeLessThan(50); | ||
| }); | ||
|
|
||
| it("should track resolved promises to prevent infinite loops", async () => { | ||
| const mockOptions: QueryDataOptions<any, any> = { | ||
| query: { kind: Kind.DOCUMENT, definitions: [] }, | ||
| variables: { id: 1 } | ||
| }; | ||
|
|
||
| const promise = Promise.resolve(); | ||
|
|
||
| (renderPromises as any).queryPromises.set(mockOptions, promise); | ||
|
|
||
| await renderPromises.consumeAndAwaitPromises({ | ||
| batchOptions: { debounce: 10 } | ||
| }); | ||
|
|
||
| // The promise should be marked as resolved | ||
| expect((renderPromises as any).resolvedPromises.has(mockOptions)).toBe(true); | ||
|
|
||
| // Adding the same query again should not create a new promise | ||
| const result = renderPromises.addQueryPromise({ | ||
| getOptions: () => mockOptions, | ||
| fetchData: () => Promise.resolve() | ||
| }); | ||
|
|
||
| // Should return finish() instead of null (indicating no new promise was created) | ||
| expect(result).toBe(null); | ||
| }); | ||
|
|
||
| it("should clear resolved promises on stop", () => { | ||
| const mockOptions: QueryDataOptions<any, any> = { | ||
| query: { kind: Kind.DOCUMENT, definitions: [] }, | ||
| variables: { id: 1 } | ||
| }; | ||
|
|
||
| (renderPromises as any).resolvedPromises.add(mockOptions); | ||
| expect((renderPromises as any).resolvedPromises.has(mockOptions)).toBe(true); | ||
|
|
||
| renderPromises.stop(); | ||
| expect((renderPromises as any).resolvedPromises.has(mockOptions)).toBe(false); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is something we might pass in to
batchOptions