Skip to content

Commit 2220abc

Browse files
committed
Populate query cache in query hooks
1 parent 3495c54 commit 2220abc

File tree

4 files changed

+55
-17
lines changed

4 files changed

+55
-17
lines changed

packages/ra-core/src/dataProvider/useDataProvider.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import validateResponseFormat from './validateResponseFormat';
77
import { DataProvider } from '../types';
88
import useLogoutIfAccessDenied from '../auth/useLogoutIfAccessDenied';
99
import { reactAdminFetchActions } from './dataFetchActions';
10-
import { populateQueryCache } from './populateQueryCache';
1110

1211
/**
1312
* Hook for getting a dataProvider
@@ -114,12 +113,7 @@ export const useDataProvider = <
114113
) {
115114
validateResponseFormat(response, type);
116115
}
117-
if (response?.meta?.prefetched) {
118-
populateQueryCache({
119-
data: response?.meta.prefetched,
120-
queryClient,
121-
});
122-
}
116+
123117
return response;
124118
})
125119
.catch(error => {

packages/ra-core/src/dataProvider/useGetList.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
import { RaRecord, GetListParams, GetListResult } from '../types';
1010
import { useDataProvider } from './useDataProvider';
1111
import { useEvent } from '../util';
12+
import { populateQueryCache } from './populateQueryCache';
1213

1314
const MAX_DATA_LENGTH_TO_CACHE = 100;
1415

@@ -167,6 +168,15 @@ export const useGetList = <
167168
result.isFetching,
168169
]);
169170

171+
useEffect(() => {
172+
if (result.data?.meta?.prefetched) {
173+
populateQueryCache({
174+
data: result.data?.meta.prefetched,
175+
queryClient,
176+
});
177+
}
178+
}, [result.data?.meta, queryClient]);
179+
170180
return useMemo(
171181
() =>
172182
result.data

packages/ra-core/src/dataProvider/useGetManyReference.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
GetManyReferenceResult,
1313
} from '../types';
1414
import { useDataProvider } from './useDataProvider';
15+
import { populateQueryCache } from './populateQueryCache';
1516
import { useEvent } from '../util';
1617

1718
/**
@@ -146,6 +147,15 @@ export const useGetManyReference = <
146147
onSettledEvent(result.data, result.error);
147148
}, [onSettledEvent, result.data, result.error, result.status]);
148149

150+
useEffect(() => {
151+
if (result.data?.meta?.prefetched) {
152+
populateQueryCache({
153+
data: result.data?.meta.prefetched,
154+
queryClient,
155+
});
156+
}
157+
}, [result.data?.meta, queryClient]);
158+
149159
return useMemo(
150160
() =>
151161
result.data

packages/ra-core/src/dataProvider/useGetOne.ts

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import { RaRecord, GetOneParams, GetOneResult } from '../types';
1+
import { useEffect, useMemo } from 'react';
22
import {
33
useQuery,
4+
useQueryClient,
45
UseQueryOptions,
56
UseQueryResult,
67
} from '@tanstack/react-query';
8+
9+
import { RaRecord, GetOneParams, GetOneResult } from '../types';
710
import { useDataProvider } from './useDataProvider';
8-
import { useEffect } from 'react';
11+
import { populateQueryCache } from './populateQueryCache';
912
import { useEvent } from '../util';
1013

1114
/**
@@ -53,6 +56,7 @@ export const useGetOne = <RecordType extends RaRecord = any, ErrorType = Error>(
5356
options: UseGetOneOptions<RecordType, ErrorType> = {}
5457
): UseGetOneHookValue<RecordType, ErrorType> => {
5558
const dataProvider = useDataProvider();
59+
const queryClient = useQueryClient();
5660
const {
5761
onError = noop,
5862
onSuccess = noop,
@@ -64,7 +68,7 @@ export const useGetOne = <RecordType extends RaRecord = any, ErrorType = Error>(
6468
const onErrorEvent = useEvent(onError);
6569
const onSettledEvent = useEvent(onSettled);
6670

67-
const result = useQuery<RecordType, ErrorType>({
71+
const result = useQuery<GetOneResult<RecordType>, ErrorType>({
6872
// Sometimes the id comes as a string (e.g. when read from the URL in a Show view).
6973
// Sometimes the id comes as a number (e.g. when read from a Record in useGetList response).
7074
// As the react-query cache is type-sensitive, we always stringify the identifier to get a match
@@ -81,7 +85,7 @@ export const useGetOne = <RecordType extends RaRecord = any, ErrorType = Error>(
8185
? queryParams.signal
8286
: undefined,
8387
})
84-
.then(({ data }) => data),
88+
.then(({ data, meta }) => ({ data, meta })),
8589
enabled: enabled ?? id != null,
8690
...queryOptions,
8791
});
@@ -93,8 +97,8 @@ export const useGetOne = <RecordType extends RaRecord = any, ErrorType = Error>(
9397
result.isFetching
9498
)
9599
return;
96-
onSuccessEvent(result.data);
97-
}, [onSuccessEvent, result.data, result.error, result.isFetching]);
100+
onSuccessEvent(result.data.data);
101+
}, [onSuccessEvent, result.data?.data, result.error, result.isFetching]);
98102

99103
useEffect(() => {
100104
if (result.error == null || result.isFetching) return;
@@ -103,16 +107,36 @@ export const useGetOne = <RecordType extends RaRecord = any, ErrorType = Error>(
103107

104108
useEffect(() => {
105109
if (result.status === 'pending' || result.isFetching) return;
106-
onSettledEvent(result.data, result.error);
110+
onSettledEvent(result.data?.data, result.error);
107111
}, [
108112
onSettledEvent,
109-
result.data,
113+
result.data?.data,
110114
result.error,
111115
result.status,
112116
result.isFetching,
113117
]);
114118

115-
return result;
119+
useEffect(() => {
120+
if (result.data?.meta?.prefetched) {
121+
populateQueryCache({
122+
data: result.data?.meta.prefetched,
123+
queryClient,
124+
});
125+
}
126+
}, [result.data?.meta, queryClient]);
127+
128+
return useMemo(
129+
() =>
130+
result.data
131+
? {
132+
...result,
133+
...result.data,
134+
}
135+
: result,
136+
[result]
137+
) as UseQueryResult<RecordType, ErrorType> & {
138+
meta?: any;
139+
};
116140
};
117141

118142
const noop = () => undefined;
@@ -121,7 +145,7 @@ export type UseGetOneOptions<
121145
RecordType extends RaRecord = any,
122146
ErrorType = Error,
123147
> = Omit<
124-
UseQueryOptions<GetOneResult<RecordType>['data'], ErrorType>,
148+
UseQueryOptions<GetOneResult<RecordType>, ErrorType>,
125149
'queryKey' | 'queryFn'
126150
> & {
127151
onSuccess?: (data: GetOneResult<RecordType>['data']) => void;

0 commit comments

Comments
 (0)