Skip to content

Commit cbeea64

Browse files
committed
Revert "Populate query cache in query hooks"
This reverts commit 2220abc.
1 parent 2220abc commit cbeea64

File tree

4 files changed

+17
-55
lines changed

4 files changed

+17
-55
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ 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';
1011

1112
/**
1213
* Hook for getting a dataProvider
@@ -113,7 +114,12 @@ export const useDataProvider = <
113114
) {
114115
validateResponseFormat(response, type);
115116
}
116-
117+
if (response?.meta?.prefetched) {
118+
populateQueryCache({
119+
data: response?.meta.prefetched,
120+
queryClient,
121+
});
122+
}
117123
return response;
118124
})
119125
.catch(error => {

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

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

1413
const MAX_DATA_LENGTH_TO_CACHE = 100;
1514

@@ -168,15 +167,6 @@ export const useGetList = <
168167
result.isFetching,
169168
]);
170169

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-
180170
return useMemo(
181171
() =>
182172
result.data

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

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

1817
/**
@@ -147,15 +146,6 @@ export const useGetManyReference = <
147146
onSettledEvent(result.data, result.error);
148147
}, [onSettledEvent, result.data, result.error, result.status]);
149148

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-
159149
return useMemo(
160150
() =>
161151
result.data

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

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

1411
/**
@@ -56,7 +53,6 @@ export const useGetOne = <RecordType extends RaRecord = any, ErrorType = Error>(
5653
options: UseGetOneOptions<RecordType, ErrorType> = {}
5754
): UseGetOneHookValue<RecordType, ErrorType> => {
5855
const dataProvider = useDataProvider();
59-
const queryClient = useQueryClient();
6056
const {
6157
onError = noop,
6258
onSuccess = noop,
@@ -68,7 +64,7 @@ export const useGetOne = <RecordType extends RaRecord = any, ErrorType = Error>(
6864
const onErrorEvent = useEvent(onError);
6965
const onSettledEvent = useEvent(onSettled);
7066

71-
const result = useQuery<GetOneResult<RecordType>, ErrorType>({
67+
const result = useQuery<RecordType, ErrorType>({
7268
// Sometimes the id comes as a string (e.g. when read from the URL in a Show view).
7369
// Sometimes the id comes as a number (e.g. when read from a Record in useGetList response).
7470
// As the react-query cache is type-sensitive, we always stringify the identifier to get a match
@@ -85,7 +81,7 @@ export const useGetOne = <RecordType extends RaRecord = any, ErrorType = Error>(
8581
? queryParams.signal
8682
: undefined,
8783
})
88-
.then(({ data, meta }) => ({ data, meta })),
84+
.then(({ data }) => data),
8985
enabled: enabled ?? id != null,
9086
...queryOptions,
9187
});
@@ -97,8 +93,8 @@ export const useGetOne = <RecordType extends RaRecord = any, ErrorType = Error>(
9793
result.isFetching
9894
)
9995
return;
100-
onSuccessEvent(result.data.data);
101-
}, [onSuccessEvent, result.data?.data, result.error, result.isFetching]);
96+
onSuccessEvent(result.data);
97+
}, [onSuccessEvent, result.data, result.error, result.isFetching]);
10298

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

108104
useEffect(() => {
109105
if (result.status === 'pending' || result.isFetching) return;
110-
onSettledEvent(result.data?.data, result.error);
106+
onSettledEvent(result.data, result.error);
111107
}, [
112108
onSettledEvent,
113-
result.data?.data,
109+
result.data,
114110
result.error,
115111
result.status,
116112
result.isFetching,
117113
]);
118114

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-
};
115+
return result;
140116
};
141117

142118
const noop = () => undefined;
@@ -145,7 +121,7 @@ export type UseGetOneOptions<
145121
RecordType extends RaRecord = any,
146122
ErrorType = Error,
147123
> = Omit<
148-
UseQueryOptions<GetOneResult<RecordType>, ErrorType>,
124+
UseQueryOptions<GetOneResult<RecordType>['data'], ErrorType>,
149125
'queryKey' | 'queryFn'
150126
> & {
151127
onSuccess?: (data: GetOneResult<RecordType>['data']) => void;

0 commit comments

Comments
 (0)