UNPKG

@airbnb/lunar-apollo

Version:

Apollo and GraphQL utilities.

33 lines (28 loc) 1.44 kB
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } import { get, set } from 'lodash/fp'; import prepareQuery from '../utils/prepareQuery'; import getQueryName from '../utils/getQueryName'; export default function removeFromList(docOrQuery, listPath, id, idName = 'id') { const query = prepareQuery(docOrQuery); return cache => { const queryResult = cache.readQuery(query); const list = get(listPath, queryResult); if (typeof list === 'undefined' || !Array.isArray(list)) { if ("production" !== process.env.NODE_ENV) { throw new TypeError("\"" + getQueryName(query.query) + "\" list \"" + listPath + "\" is not an array."); } else { return; } } const rootItem = listPath.split('.')[0]; // @ts-ignore const resultRoot = queryResult[rootItem]; // Evict cache before writeQuery: https://github.com/apollographql/apollo-client/issues/6451 cache.evict({ id: cache.identify(resultRoot), broadcast: false }); const nextResult = set(listPath, list.filter(item => item[idName] !== id), _extends({}, queryResult)); cache.writeQuery(_extends({}, query, { data: nextResult })); }; }