Skip to content

Commit 3828b0e

Browse files
committed
Apollo: Drop manual cache item eviction code
There were several problems with the code sections removed: * Some trying to remove wrong type of an cache object: Made changes to DB entity type A, but dropped item of type B from cache. * Some were giving in undefined ids, thus causing errors to be printed into the dev console. * Some were just outright illogical, patching an object, returning and caching the updated data, and then removing the freshly updated item. * There were couple of proper use cases (DeleteStop, DeleteRoute) where the eviction call was valid, but those have also been removed to let automatic garbage collection then handle that at a later point. BUT!: Then there was a bug in the eviction code it self. `cache.evict` takes in an options parameter in the form of `{ id: thing to remove }`, but the `removeFromApolloCache` function incorrectly passed in the id string straight to the evict call. Type script did warn the original developer about this, but said developer (T. K.) ignored the warning and thought he knew better and suppressed said error with a handy dandy `@ts-expect-error` comment. What that did do, was that it instead removing a single item from the cache, it reset the whole Apollo cache to an empty state, which in turn resulted in active queries being refetched from the database. Line details page was relying on this unconventional data refresh strategy to function correctly, so a proper data refetch has been added for that as a fix.
1 parent f988b49 commit 3828b0e

11 files changed

Lines changed: 12 additions & 100 deletions

File tree

ui/src/components/map/routes/hooks/useCreateRoute.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import { MIN_DATE } from '../../../../time';
1515
import {
1616
buildJourneyPatternStopSequence,
1717
mapToObject,
18-
removeFromApolloCache,
1918
showDangerToastWithError,
2019
} from '../../../../utils';
2120
import { useCheckValidityAndPriorityConflicts } from '../../../common/hooks';
@@ -47,15 +46,7 @@ export const useCreateRoute = () => {
4746
const insertRouteMutation = async (
4847
variables: InsertRouteOneMutationVariables,
4948
) => {
50-
return mutateFunction({
51-
variables,
52-
update(cache) {
53-
removeFromApolloCache(cache, {
54-
line_id: variables.object.on_line_id,
55-
__typename: 'route_line',
56-
});
57-
},
58-
});
49+
return mutateFunction({ variables });
5950
};
6051

6152
const mapRouteDetailsToInsertMutationVariables = (

ui/src/components/map/routes/hooks/useDeleteRoute.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import { useTranslation } from 'react-i18next';
22
import { useDeleteRouteMutation } from '../../../../generated/graphql';
3-
import {
4-
mapToVariables,
5-
removeFromApolloCache,
6-
showDangerToastWithError,
7-
} from '../../../../utils';
3+
import { mapToVariables, showDangerToastWithError } from '../../../../utils';
84

95
export const useDeleteRoute = () => {
106
const { t } = useTranslation();
@@ -17,12 +13,6 @@ export const useDeleteRoute = () => {
1713

1814
const result = await mutateFunction({
1915
...mapToVariables({ route_id: routeId }),
20-
update(cache) {
21-
removeFromApolloCache(cache, {
22-
route_id: routeId,
23-
__typename: 'route_route',
24-
});
25-
},
2616
});
2717
return result;
2818
};

ui/src/components/map/routes/hooks/useEditRouteGeometry.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
} from '../../../../graphql';
1717
import {
1818
buildJourneyPatternStopSequence,
19-
removeFromApolloCache,
2019
showDangerToastWithError,
2120
} from '../../../../utils';
2221
import { useValidateRoute } from './useValidateRoute';
@@ -153,12 +152,6 @@ export const useEditRouteGeometry = () => {
153152
) => {
154153
await mutateFunction({
155154
variables,
156-
update(cache) {
157-
removeFromApolloCache(cache, {
158-
route_id: variables.route_id,
159-
__typename: 'route_route',
160-
});
161-
},
162155
refetchQueries: [
163156
{
164157
query: GetRouteDetailsByIdDocument,

ui/src/components/map/routes/hooks/useEditRouteMetadata.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
defaultLocalizedString,
1616
mapDateInputToValidityEnd,
1717
mapDateInputToValidityStart,
18-
removeFromApolloCache,
1918
showDangerToastWithError,
2019
} from '../../../../utils';
2120
import { useCheckValidityAndPriorityConflicts } from '../../../common/hooks';
@@ -161,15 +160,7 @@ export const useEditRouteMetadata = () => {
161160
});
162161

163162
const editRouteMetadata = (variables: PatchRouteMutationVariables) => {
164-
return mutateFunction({
165-
variables,
166-
update(cache) {
167-
removeFromApolloCache(cache, {
168-
route_id: variables.route_id,
169-
__typename: 'route_route',
170-
});
171-
},
172-
});
163+
return mutateFunction({ variables });
173164
};
174165

175166
// default handler that can be used to show error messages as toast

ui/src/components/map/stops/hooks/useCreateStop.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {
1717
LinkNotResolvedError,
1818
findKeyValue,
1919
getRouteLabelVariantText,
20-
removeFromApolloCache,
2120
} from '../../../../utils';
2221
import { useCheckValidityAndPriorityConflicts } from '../../../common/hooks';
2322
import {
@@ -208,12 +207,6 @@ export function useCreateStop() {
208207
stop_place_ref: quayId,
209208
},
210209
},
211-
update(cache) {
212-
removeFromApolloCache(cache, {
213-
infrastructure_link_id: stopPoint.located_on_infrastructure_link_id,
214-
__typename: 'infrastructure_network_infrastructure_link',
215-
});
216-
},
217210
});
218211

219212
const stopPointId =

ui/src/components/map/stops/hooks/useDeleteStop.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { mapStopResultToStop } from '../../../../graphql';
99
import {
1010
EditRouteTerminalStopsError,
1111
InternalError,
12-
removeFromApolloCache,
1312
showDangerToast,
1413
showDangerToastWithError,
1514
} from '../../../../utils';
@@ -93,13 +92,6 @@ export const useDeleteStop = () => {
9392
}: DeleteChanges) => {
9493
const removedStopPointResult = await removeStopMutation({
9594
variables: { stop_id: stopPointId },
96-
// remove stop from cache after mutation
97-
update(cache) {
98-
removeFromApolloCache(cache, {
99-
scheduled_stop_point_id: stopPointId,
100-
__typename: 'service_pattern_scheduled_stop_point',
101-
});
102-
},
10395
});
10496

10597
const removedQuayResult = await deleteQuay(stopPlaceId, quayId);

ui/src/components/map/stops/hooks/useEditStop.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import {
3636
TiamatUpdateFailedError,
3737
TimingPlaceRequiredError,
3838
defaultTo,
39-
removeFromApolloCache,
4039
showDangerToast,
4140
} from '../../../../utils';
4241
import { useCheckValidityAndPriorityConflicts } from '../../../common/hooks';
@@ -578,13 +577,6 @@ export const useEditStop = () => {
578577
updateStopPointResult = await wrapErrors(
579578
editStopMutation({
580579
variables,
581-
update(cache) {
582-
removeFromApolloCache(cache, {
583-
infrastructure_link_id:
584-
variables.stop_patch.located_on_infrastructure_link_id,
585-
__typename: 'infrastructure_network_infrastructure_link',
586-
});
587-
},
588580

589581
// Always refetch map queries after StopPoint update.
590582
...refetchQueries,

ui/src/components/routes-and-lines/edit-route/EditRoutePage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ export const EditRoutePage: FC = () => {
109109
stopPointLabels: draftStops?.map((stop) => stop.label),
110110
});
111111
const variables = mapEditJourneyPatternChangesToVariables(changes);
112-
await updateRouteGeometryMutation(variables);
112+
await updateRouteGeometryMutation(variables, route.route_id);
113113
}
114114
onSubmit(form);
115115
} catch (err) {

ui/src/components/routes-and-lines/edit-route/useEditRouteJourneyPattern.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { gql } from '@apollo/client';
22
import {
3+
GetRouteDetailsByIdDocument,
34
JourneyPatternStopFragment,
45
RouteStopFieldsFragment,
56
RouteWithInfrastructureLinksWithStopsAndJpsFragment,
@@ -16,7 +17,6 @@ import {
1617
buildJourneyPatternStopSequence,
1718
filterDistinctConsecutiveStops,
1819
mapRouteStopsToJourneyPatternStops,
19-
removeFromApolloCache,
2020
} from '../../../utils';
2121
import { extractJourneyPatternCandidateStops } from '../../map/routes/hooks/useExtractRouteFromFeature';
2222
import { useValidateRoute } from '../../map/routes/hooks/useValidateRoute';
@@ -141,16 +141,14 @@ export const useEditRouteJourneyPattern = () => {
141141

142142
const updateRouteGeometryMutation = async (
143143
variables: UpdateRouteJourneyPatternMutationVariables,
144+
routeId: UUID,
144145
) => {
145146
await updateRouteJourneyPatternMutation({
146147
variables,
147-
// remove scheduled stop point from cache after mutation
148-
update(cache) {
149-
removeFromApolloCache(cache, {
150-
journey_pattern_id: variables.journey_pattern_id,
151-
__typename: 'journey_pattern_journey_pattern',
152-
});
153-
},
148+
awaitRefetchQueries: true,
149+
refetchQueries: [
150+
{ query: GetRouteDetailsByIdDocument, variables: { routeId } },
151+
],
154152
});
155153
};
156154

ui/src/components/routes-and-lines/line-details/StopActionsDropdown.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export const StopActionsDropdown: FC<StopActionsDropdownProps> = ({
8989

9090
const variables = mapEditJourneyPatternChangesToVariables(changes);
9191

92-
await updateRouteGeometryMutation(variables);
92+
await updateRouteGeometryMutation(variables, route.route_id);
9393
showSuccessToast(t(($) => $.routes.saveSuccess));
9494
} catch (err) {
9595
showDangerToast(`${t(($) => $.errors.saveFailed)}, '${err}'`);
@@ -107,7 +107,7 @@ export const StopActionsDropdown: FC<StopActionsDropdownProps> = ({
107107

108108
const variables = mapEditJourneyPatternChangesToVariables(changes);
109109

110-
await updateRouteGeometryMutation(variables);
110+
await updateRouteGeometryMutation(variables, route.route_id);
111111
showSuccessToast(t(($) => $.routes.saveSuccess));
112112
} catch (err) {
113113
showDangerToast(`${t(($) => $.errors.saveFailed)}, '${err}'`);

0 commit comments

Comments
 (0)