Fixed refetch query for GetCommentThreadsByTargets (#336)

* Fixed refetch query for GetCommentThreadsByTargets

* Improvement : use apollo util getOperationName to de-hard code refetch queries arrays
This commit is contained in:
Lucas Bordeau
2023-06-19 17:44:05 +02:00
committed by GitHub
parent 96a53ad765
commit 950a0b77fe
7 changed files with 47 additions and 14 deletions

View File

@ -1,15 +1,20 @@
import { getOperationName } from '@apollo/client/utilities';
import styled from '@emotion/styled';
import { useRecoilValue } from 'recoil';
import { v4 } from 'uuid';
import { currentUserState } from '@/auth/states/currentUserState';
import { CommentThreadForDrawer } from '@/comments/types/CommentThreadForDrawer';
import { GET_COMPANIES } from '@/companies/services';
import { GET_PEOPLE } from '@/people/services';
import { AutosizeTextInput } from '@/ui/components/inputs/AutosizeTextInput';
import { logError } from '@/utils/logs/logError';
import { isDefined } from '@/utils/type-guards/isDefined';
import { isNonEmptyString } from '@/utils/type-guards/isNonEmptyString';
import { useCreateCommentMutation } from '~/generated/graphql';
import { GET_COMMENT_THREADS_BY_TARGETS } from '../services';
import { CommentThreadItem } from './CommentThreadItem';
import { CommentThreadRelationPicker } from './CommentThreadRelationPicker';
@ -67,12 +72,10 @@ export function CommentThread({ commentThread }: OwnProps) {
commentText,
createdAt: new Date().toISOString(),
},
// TODO: find a way to have this configuration dynamic and typed
// Also it cannot refetch queries than are not in the cache
refetchQueries: [
'GetCommentThreadsByTargets',
'GetCompanies',
'GetPeople',
getOperationName(GET_COMPANIES) ?? '',
getOperationName(GET_PEOPLE) ?? '',
getOperationName(GET_COMMENT_THREADS_BY_TARGETS) ?? '',
],
onError: (error) => {
logError(

View File

@ -1,3 +1,4 @@
import { getOperationName } from '@apollo/client/utilities';
import styled from '@emotion/styled';
import { useRecoilState, useRecoilValue } from 'recoil';
import { v4 } from 'uuid';
@ -5,6 +6,8 @@ import { v4 } from 'uuid';
import { currentUserState } from '@/auth/states/currentUserState';
import { commentableEntityArrayState } from '@/comments/states/commentableEntityArrayState';
import { createdCommentThreadIdState } from '@/comments/states/createdCommentThreadIdState';
import { GET_COMPANIES } from '@/companies/services';
import { GET_PEOPLE } from '@/people/services';
import { AutosizeTextInput } from '@/ui/components/inputs/AutosizeTextInput';
import { useOpenRightDrawer } from '@/ui/layout/right-drawer/hooks/useOpenRightDrawer';
import { logError } from '@/utils/logs/logError';
@ -16,6 +19,8 @@ import {
useGetCommentThreadQuery,
} from '~/generated/graphql';
import { GET_COMMENT_THREAD } from '../services';
import { CommentThreadItem } from './CommentThreadItem';
const StyledContainer = styled.div`
@ -99,7 +104,11 @@ export function CommentThreadCreateMode() {
}),
),
},
refetchQueries: ['GetCommentThread', 'GetCompanies', 'GetPeople'],
refetchQueries: [
getOperationName(GET_COMPANIES) ?? '',
getOperationName(GET_PEOPLE) ?? '',
getOperationName(GET_COMMENT_THREAD) ?? '',
],
onCompleted(data) {
setCreatedCommentThreadId(data.createOneCommentThread.id);
openRightDrawer('comments');
@ -114,8 +123,7 @@ export function CommentThreadCreateMode() {
commentText,
createdAt: new Date().toISOString(),
},
// TODO: find a way to have this configuration dynamic and typed
refetchQueries: ['GetCommentThread'],
refetchQueries: [getOperationName(GET_COMMENT_THREAD) ?? ''],
onError: (error) => {
logError(
`In handleCreateCommentThread, createCommentMutation onError, error: ${error}`,