import { DocumentNode } from 'graphql' ; import { get , set } from 'lodash/fp' ; import { DataProxy , ApolloCache , StoreObject } from '@apollo/client' ; import prepareQuery from '../utils/prepareQuery' ; import getQueryName from '../utils/getQueryName' ; export default function removeFromList < Result , Vars = { } > ( docOrQuery , listPath , id , idName = 'id' ) { const query = prepareQuery < Result , Vars > ( docOrQuery ) ; return ( cache ) => { const queryResult = cache . readQuery < Result > ( query ) ; const list = get ( listPath , queryResult ) ; if ( typeof list === 'undefined' || ! Array . isArray ( list ) ) { if ( __DEV__ ) { throw new TypeError ( `"${ getQueryName ( query . query ) }" list "${ listPath }" is not an array.` ) ; } else { return ; } } const rootItem = listPath . split ( '.' ) [ 0 ] ; const resultRoot = queryResult [ rootItem ] as StoreObject ; cache . evict ( { id : cache . identify ( resultRoot ) , broadcast : false , } ) ; const nextResult = set ( listPath , list . filter ( ( item ) => ( item as { [ key ] : unknown ; } ) [ idName ] !== id ) , { ... queryResult } ) ; cache . writeQuery ( { ... query , data : nextResult , } ) ; } ; }