Skip to content

Commit 260ab0f

Browse files
committed
feat: Remove experimental streamedQuery
1 parent e90e957 commit 260ab0f

File tree

3 files changed

+86
-54
lines changed

3 files changed

+86
-54
lines changed

packages/app/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
"@mantine/spotlight": "7.9.2",
4242
"@microsoft/fetch-event-source": "^2.0.1",
4343
"@tabler/icons-react": "^3.5.0",
44-
"@tanstack/react-query": "^5.90.2",
45-
"@tanstack/react-query-devtools": "^5.90.2",
44+
"@tanstack/react-query": "^5.56.2",
45+
"@tanstack/react-query-devtools": "^5.56.2",
4646
"@tanstack/react-table": "^8.7.9",
4747
"@tanstack/react-virtual": "^3.0.1",
4848
"@uiw/codemirror-theme-atomone": "^4.23.3",

packages/app/src/hooks/useChartConfig.tsx

Lines changed: 63 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@ import {
1616
ChartConfigWithDateRange,
1717
ChartConfigWithOptDateRange,
1818
} from '@hyperdx/common-utils/dist/types';
19-
import {
20-
experimental_streamedQuery as streamedQuery,
21-
useQuery,
22-
UseQueryOptions,
23-
} from '@tanstack/react-query';
19+
import { useQuery, UseQueryOptions } from '@tanstack/react-query';
2420

2521
import {
2622
convertDateRangeToGranularityString,
@@ -52,6 +48,11 @@ type TQueryFnData = Pick<ResponseJSON<any>, 'data' | 'meta' | 'rows'> & {
5248
isComplete: boolean;
5349
};
5450

51+
type TChunk = {
52+
chunk: ResponseJSON<Record<string, string | number>>;
53+
isComplete: boolean;
54+
};
55+
5556
const shouldUseChunking = (
5657
config: ChartConfigWithOptDateRange,
5758
): config is ChartConfigWithDateRange & {
@@ -158,6 +159,19 @@ async function* fetchDataInChunks(
158159
}
159160
}
160161

162+
/** Append the given chunk to the given accumulated result */
163+
function appendChunk(
164+
accumulated: TQueryFnData,
165+
{ chunk, isComplete }: TChunk,
166+
): TQueryFnData {
167+
return {
168+
data: [...(chunk.data || []), ...(accumulated?.data || [])],
169+
meta: chunk.meta,
170+
rows: (accumulated?.rows || 0) + (chunk.rows || 0),
171+
isComplete,
172+
};
173+
}
174+
161175
/**
162176
* A hook providing data queried based on the provided chart config.
163177
*
@@ -186,36 +200,54 @@ export function useQueriedChartConfig(
186200
// Include disableQueryChunking in the query key to ensure that queries with the
187201
// same config but different disableQueryChunking values do not share a query
188202
queryKey: [config, options?.disableQueryChunking ?? false],
189-
queryFn: streamedQuery({
190-
streamFn: context =>
191-
fetchDataInChunks(
192-
config,
193-
clickhouseClient,
194-
context.signal,
195-
options?.disableQueryChunking,
196-
),
197-
/**
198-
* This mode ensures that data remains in the cache until the next full streamed result is available.
199-
* By default, the cache would be cleared before new data starts arriving, which results in the query briefly
200-
* going back into the loading/pending state when multiple observers are sharing the query result resulting
201-
* in flickering or render loops.
202-
*/
203-
refetchMode: 'replace',
204-
initialValue: {
203+
// TODO: Replace this with `streamedQuery` when it is no longer experimental. Use 'replace' refetch mode.
204+
// https://tanstack.com/query/latest/docs/reference/streamedQuery
205+
queryFn: async context => {
206+
const query = context.client
207+
.getQueryCache()
208+
.find({ queryKey: context.queryKey, exact: true });
209+
const isRefetch = !!query && query.state.data !== undefined;
210+
211+
const emptyValue: TQueryFnData = {
205212
data: [],
206213
meta: [],
207214
rows: 0,
208215
isComplete: false,
209-
} as TQueryFnData,
210-
reducer: (acc, { chunk, isComplete }) => {
211-
return {
212-
data: [...(chunk.data || []), ...(acc?.data || [])],
213-
meta: chunk.meta,
214-
rows: (acc?.rows || 0) + (chunk.rows || 0),
215-
isComplete,
216-
};
217-
},
218-
}),
216+
};
217+
218+
const chunks = fetchDataInChunks(
219+
config,
220+
clickhouseClient,
221+
context.signal,
222+
options?.disableQueryChunking,
223+
);
224+
225+
let accumulatedChunks: TQueryFnData = emptyValue;
226+
for await (const chunk of chunks) {
227+
if (context.signal.aborted) {
228+
break;
229+
}
230+
231+
accumulatedChunks = appendChunk(accumulatedChunks, chunk);
232+
233+
// When refetching, the cache is not updated until all chunks are fetched.
234+
if (!isRefetch) {
235+
context.client.setQueryData<TQueryFnData>(
236+
context.queryKey,
237+
accumulatedChunks,
238+
);
239+
}
240+
}
241+
242+
if (isRefetch && !context.signal.aborted) {
243+
context.client.setQueryData<TQueryFnData>(
244+
context.queryKey,
245+
accumulatedChunks,
246+
);
247+
}
248+
249+
return context.client.getQueryData(context.queryKey)!;
250+
},
219251
retry: 1,
220252
refetchOnWindowFocus: false,
221253
...options,

yarn.lock

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4595,8 +4595,8 @@ __metadata:
45954595
"@storybook/react": "npm:^8.1.5"
45964596
"@storybook/test": "npm:^8.1.5"
45974597
"@tabler/icons-react": "npm:^3.5.0"
4598-
"@tanstack/react-query": "npm:^5.90.2"
4599-
"@tanstack/react-query-devtools": "npm:^5.90.2"
4598+
"@tanstack/react-query": "npm:^5.56.2"
4599+
"@tanstack/react-query-devtools": "npm:^5.56.2"
46004600
"@tanstack/react-table": "npm:^8.7.9"
46014601
"@tanstack/react-virtual": "npm:^3.0.1"
46024602
"@testing-library/jest-dom": "npm:^6.4.2"
@@ -9399,40 +9399,40 @@ __metadata:
93999399
languageName: node
94009400
linkType: hard
94019401

9402-
"@tanstack/query-core@npm:5.90.2":
9403-
version: 5.90.2
9404-
resolution: "@tanstack/query-core@npm:5.90.2"
9405-
checksum: 10c0/695a7450b0bb9f6dd21bebeacfc962dfc886631a3b3a13c33a842ef719b4c3dd30c15febe8c1ade6902a85e0f387c51a97570f430cc8f5c7032ff737d6410597
9402+
"@tanstack/query-core@npm:5.56.2":
9403+
version: 5.56.2
9404+
resolution: "@tanstack/query-core@npm:5.56.2"
9405+
checksum: 10c0/54ff55f02b01f6ba089f4965bfd46f430c18ce7e11d874de04c4d58cc8f698598b41e1c017ba029d08ae75e321e546b26f1ea7f788474db265eeba46e780f2f6
94069406
languageName: node
94079407
linkType: hard
94089408

9409-
"@tanstack/query-devtools@npm:5.90.1":
9410-
version: 5.90.1
9411-
resolution: "@tanstack/query-devtools@npm:5.90.1"
9412-
checksum: 10c0/3b69e5441438acf0e753adbf187abf54b5b2e19d7c6d1e465d97278cb8c248bb86d3be193092d50414e4093cbf014093103517cb523daae003e53c867f3c11c2
9409+
"@tanstack/query-devtools@npm:5.56.1":
9410+
version: 5.56.1
9411+
resolution: "@tanstack/query-devtools@npm:5.56.1"
9412+
checksum: 10c0/4f50fccf9e731e0fee4b7d2344cd8bf73a9c45816d7361f54ca4ec2df0a0455bf400fa3b578279edc8a4d1013c52c0d90cd50dbffb133edcf52e55868468d6c3
94139413
languageName: node
94149414
linkType: hard
94159415

9416-
"@tanstack/react-query-devtools@npm:^5.90.2":
9417-
version: 5.90.2
9418-
resolution: "@tanstack/react-query-devtools@npm:5.90.2"
9416+
"@tanstack/react-query-devtools@npm:^5.56.2":
9417+
version: 5.56.2
9418+
resolution: "@tanstack/react-query-devtools@npm:5.56.2"
94199419
dependencies:
9420-
"@tanstack/query-devtools": "npm:5.90.1"
9420+
"@tanstack/query-devtools": "npm:5.56.1"
94219421
peerDependencies:
9422-
"@tanstack/react-query": ^5.90.2
9422+
"@tanstack/react-query": ^5.56.2
94239423
react: ^18 || ^19
9424-
checksum: 10c0/526d529bf995426ace7511f51a425ce92dfc1b6dd74c9956a3cd7d68950119e97291bced2ff17173bcdb329eae36c68abc211a4dec32d6e92ab537b41c0533c2
9424+
checksum: 10c0/2edebb04800585da1ca479f9289a9265cfcc26c5d8fb1db08e6fbe9fdf370a55a2007416b52f819c2faeab7b919cd51a4afca7731b50b6f3dd164057e4966741
94259425
languageName: node
94269426
linkType: hard
94279427

9428-
"@tanstack/react-query@npm:^5.90.2":
9429-
version: 5.90.2
9430-
resolution: "@tanstack/react-query@npm:5.90.2"
9428+
"@tanstack/react-query@npm:^5.56.2":
9429+
version: 5.56.2
9430+
resolution: "@tanstack/react-query@npm:5.56.2"
94319431
dependencies:
9432-
"@tanstack/query-core": "npm:5.90.2"
9432+
"@tanstack/query-core": "npm:5.56.2"
94339433
peerDependencies:
94349434
react: ^18 || ^19
9435-
checksum: 10c0/22e76626a59890409858521b0e42b49219126a4ea5ed79eaa48a267959175dfdd28b30b9b03a415dccf703d95c18100a9d8917679818f6d2adc26d6c5f96a4d6
9435+
checksum: 10c0/6e883b4ca1948f990215b7bce194251faf13a79c6ecf3f3c660af6c6788ed113ab629cefdafb496dfb04866f12dd48d7314e936b75c881b6749127b6496ac8fd
94369436
languageName: node
94379437
linkType: hard
94389438

0 commit comments

Comments
 (0)