roschaefer e24d8035b1 Fix this annoying bug with a tested helper
I don't know where the bug originates. But it can only be that either
`previousResult` or `fetchMore` result is sometimes undefined. This
should make the function bullet-proof for these situations.
2019-12-10 23:28:01 +01:00

18 lines
628 B
JavaScript

import unionBy from 'lodash/unionBy'
export default function UpdateQuery(component, { $state, pageKey }) {
if (!pageKey) throw new Error('No key given for the graphql query { data } object')
return (previousResult, { fetchMoreResult }) => {
const oldData = (previousResult && previousResult[pageKey]) || []
const newData = (fetchMoreResult && fetchMoreResult[pageKey]) || []
if (newData.length < component.pageSize) {
component.hasMore = false
$state.complete()
}
const result = {}
result[pageKey] = unionBy(oldData, newData, item => item.id)
$state.loaded()
return result
}
}