Commit c3a0ffa4 authored by Jose Vargas's avatar Jose Vargas Committed by Jose Ivan Vargas

Config changes to make it work with filtered search

parent 07a8b271
import { isEqual, differenceWith } from 'lodash'; import { isEqual } from 'lodash';
export default { export default {
typePolicies: { typePolicies: {
...@@ -13,15 +13,28 @@ export default { ...@@ -13,15 +13,28 @@ export default {
merge(existing = {}, incoming, { args = {} }) { merge(existing = {}, incoming, { args = {} }) {
let nodes; let nodes;
const areNodesEqual = isEqual(existing.nodes, incoming.nodes);
const statuses = Array.isArray(args.statuses) ? [...args.statuses] : args.statuses;
const { pageInfo } = incoming;
if (Object.keys(existing).length !== 0 && isEqual(existing?.statuses, args?.statuses)) { if (Object.keys(existing).length !== 0 && isEqual(existing?.statuses, args?.statuses)) {
const diff = differenceWith(existing.nodes, incoming.nodes, isEqual); if (areNodesEqual) {
if (existing.nodes.length === incoming.nodes.length) { if (incoming.pageInfo.hasNextPage) {
if (diff.length !== 0) { nodes = [...existing.nodes, ...incoming.nodes];
nodes = [...existing.nodes, ...diff];
} else { } else {
nodes = [...existing.nodes]; nodes = [...incoming.nodes];
} }
} else { } else {
if (!existing.pageInfo?.hasNextPage) {
nodes = [...incoming.nodes];
return {
nodes,
statuses,
pageInfo,
};
}
nodes = [...existing.nodes, ...incoming.nodes]; nodes = [...existing.nodes, ...incoming.nodes];
} }
} else { } else {
...@@ -30,8 +43,8 @@ export default { ...@@ -30,8 +43,8 @@ export default {
return { return {
nodes, nodes,
statuses: Array.isArray(args.statuses) ? [...args.statuses] : args.statuses, statuses,
pageInfo: incoming.pageInfo, pageInfo,
}; };
}, },
}, },
......
...@@ -34,9 +34,17 @@ describe('jobs/components/table/graphql/cache_config', () => { ...@@ -34,9 +34,17 @@ describe('jobs/components/table/graphql/cache_config', () => {
}); });
it('should not add to existing cache if the incoming elements are the same', () => { it('should not add to existing cache if the incoming elements are the same', () => {
// simulate that this is the last page
const finalExistingCache = {
...CIJobConnectionExistingCache,
pageInfo: {
hasNextPage: false,
},
};
const res = cacheConfig.typePolicies.CiJobConnection.merge( const res = cacheConfig.typePolicies.CiJobConnection.merge(
CIJobConnectionExistingCache, CIJobConnectionExistingCache,
CIJobConnectionExistingCache, finalExistingCache,
{ {
args: firstLoadArgs, args: firstLoadArgs,
}, },
......
...@@ -1911,6 +1911,13 @@ export const CIJobConnectionIncomingCacheRunningStatus = { ...@@ -1911,6 +1911,13 @@ export const CIJobConnectionIncomingCacheRunningStatus = {
}; };
export const CIJobConnectionExistingCache = { export const CIJobConnectionExistingCache = {
pageInfo: {
__typename: 'PageInfo',
endCursor: 'eyJpZCI6IjIwNTEifQ',
hasNextPage: true,
hasPreviousPage: false,
startCursor: 'eyJpZCI6IjIxNzMifQ',
},
nodes: [ nodes: [
{ __ref: 'CiJob:gid://gitlab/Ci::Build/2100' }, { __ref: 'CiJob:gid://gitlab/Ci::Build/2100' },
{ __ref: 'CiJob:gid://gitlab/Ci::Build/2101' }, { __ref: 'CiJob:gid://gitlab/Ci::Build/2101' },
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment