-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Refetch aggregate queries on record creation/update/deletion of record #8885
Changes from 2 commits
692005c
0340a91
13061b0
b775f1b
2ce5466
86dec8b
4bc1d42
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ import { getObjectTypename } from '@/object-record/cache/utils/getObjectTypename | |
import { RecordGqlOperationGqlRecordFields } from '@/object-record/graphql/types/RecordGqlOperationGqlRecordFields'; | ||
import { generateDepthOneRecordGqlFields } from '@/object-record/graphql/utils/generateDepthOneRecordGqlFields'; | ||
import { useCreateManyRecordsMutation } from '@/object-record/hooks/useCreateManyRecordsMutation'; | ||
import { useRefetchAggregateQueries } from '@/object-record/hooks/useRefetchAggregateQueries'; | ||
import { ObjectRecord } from '@/object-record/types/ObjectRecord'; | ||
import { getCreateManyRecordsMutationResponseField } from '@/object-record/utils/getCreateManyRecordsMutationResponseField'; | ||
import { sanitizeRecordInput } from '@/object-record/utils/sanitizeRecordInput'; | ||
|
@@ -51,6 +52,10 @@ export const useCreateManyRecords = < | |
|
||
const { objectMetadataItems } = useObjectMetadataItems(); | ||
|
||
const { refetchAggregateQueries } = useRefetchAggregateQueries({ | ||
objectMetadataNamePlural: objectMetadataItem.namePlural, | ||
}); | ||
|
||
const createManyRecords = async ( | ||
recordsToCreate: Partial<CreatedObjectRecord>[], | ||
upsert?: boolean, | ||
|
@@ -141,6 +146,7 @@ export const useCreateManyRecords = < | |
throw error; | ||
}); | ||
|
||
await refetchAggregateQueries(); | ||
return createdObjects.data?.[mutationResponseField] ?? []; | ||
Comment on lines
+149
to
150
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: consider wrapping refetch in try/catch to handle potential refetch failures gracefully |
||
}; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ import { useGetRecordFromCache } from '@/object-record/cache/hooks/useGetRecordF | |
import { getRecordNodeFromRecord } from '@/object-record/cache/utils/getRecordNodeFromRecord'; | ||
import { updateRecordFromCache } from '@/object-record/cache/utils/updateRecordFromCache'; | ||
import { useDeleteOneRecordMutation } from '@/object-record/hooks/useDeleteOneRecordMutation'; | ||
import { useRefetchAggregateQueries } from '@/object-record/hooks/useRefetchAggregateQueries'; | ||
import { ObjectRecord } from '@/object-record/types/ObjectRecord'; | ||
import { getDeleteOneRecordMutationResponseField } from '@/object-record/utils/getDeleteOneRecordMutationResponseField'; | ||
import { capitalize } from '~/utils/string/capitalize'; | ||
|
@@ -35,6 +36,10 @@ export const useDeleteOneRecord = ({ | |
|
||
const { objectMetadataItems } = useObjectMetadataItems(); | ||
|
||
const { refetchAggregateQueries } = useRefetchAggregateQueries({ | ||
objectMetadataNamePlural: objectMetadataItem.namePlural, | ||
}); | ||
|
||
const mutationResponseField = | ||
getDeleteOneRecordMutationResponseField(objectNameSingular); | ||
|
||
|
@@ -126,6 +131,7 @@ export const useDeleteOneRecord = ({ | |
throw error; | ||
}); | ||
|
||
await refetchAggregateQueries(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: refetchAggregateQueries() should be wrapped in a try/catch to handle potential refetch failures gracefully |
||
return deletedRecord.data?.[mutationResponseField] ?? null; | ||
}, | ||
[ | ||
|
@@ -135,6 +141,7 @@ export const useDeleteOneRecord = ({ | |
mutationResponseField, | ||
objectMetadataItem, | ||
objectMetadataItems, | ||
refetchAggregateQueries, | ||
], | ||
); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ import { useObjectMetadataItems } from '@/object-metadata/hooks/useObjectMetadat | |
import { useGetRecordFromCache } from '@/object-record/cache/hooks/useGetRecordFromCache'; | ||
import { DEFAULT_MUTATION_BATCH_SIZE } from '@/object-record/constants/DefaultMutationBatchSize'; | ||
import { useDestroyManyRecordsMutation } from '@/object-record/hooks/useDestroyManyRecordsMutation'; | ||
import { useRefetchAggregateQueries } from '@/object-record/hooks/useRefetchAggregateQueries'; | ||
import { getDestroyManyRecordsMutationResponseField } from '@/object-record/utils/getDestroyManyRecordsMutationResponseField'; | ||
import { useRecoilValue } from 'recoil'; | ||
import { isDefined } from '~/utils/isDefined'; | ||
|
@@ -48,6 +49,10 @@ export const useDestroyManyRecords = ({ | |
|
||
const { objectMetadataItems } = useObjectMetadataItems(); | ||
|
||
const { refetchAggregateQueries } = useRefetchAggregateQueries({ | ||
objectMetadataNamePlural: objectMetadataItem.namePlural, | ||
}); | ||
|
||
const mutationResponseField = getDestroyManyRecordsMutationResponseField( | ||
objectMetadataItem.namePlural, | ||
); | ||
|
@@ -127,6 +132,7 @@ export const useDestroyManyRecords = ({ | |
} | ||
} | ||
|
||
await refetchAggregateQueries(); | ||
Comment on lines
134
to
+135
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: refetchAggregateQueries could fail silently here - consider handling errors or at least logging them |
||
return destroyedRecords; | ||
}; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { getAggregateQueryName } from '@/object-record/utils/getAggregateQueryName'; | ||
import { useIsFeatureEnabled } from '@/workspace/hooks/useIsFeatureEnabled'; | ||
import { useApolloClient } from '@apollo/client'; | ||
|
||
export const useRefetchAggregateQueries = ({ | ||
objectMetadataNamePlural, | ||
}: { | ||
objectMetadataNamePlural: string; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: objectMetadataNamePlural should be validated - getAggregateQueryName will throw if this is empty |
||
}) => { | ||
const apolloClient = useApolloClient(); | ||
const isAggregateQueryEnabled = useIsFeatureEnabled( | ||
'IS_AGGREGATE_QUERY_ENABLED', | ||
); | ||
const refetchAggregateQueries = async () => { | ||
const queryName = getAggregateQueryName(objectMetadataNamePlural); | ||
|
||
if (isAggregateQueryEnabled) { | ||
await apolloClient.refetchQueries({ | ||
include: [queryName], | ||
}); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: refetchQueries could fail - needs try/catch with error handling |
||
}; | ||
|
||
return { | ||
refetchAggregateQueries, | ||
}; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { capitalize } from '~/utils/string/capitalize'; | ||
|
||
export const getAggregateQueryName = ( | ||
objectMetadataNamePlural: string, | ||
): string => { | ||
if (!objectMetadataNamePlural) { | ||
throw new Error('objectMetadataNamePlural is required'); | ||
ijreilly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
return `AggregateMany${capitalize(objectMetadataNamePlural)}`; | ||
ijreilly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; |
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 will seek to refetch aggregate queries even if there were no AggregateQueries performed before (like on table view for instance). I think it's ok but it does trigger a warning (yellow) in the console.
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.
@charlesBochet fyi