Skip to content

Commit

Permalink
Add resolveNode param which allows data to be used for edges, not jus…
Browse files Browse the repository at this point in the history
…t nodes
  • Loading branch information
mzikherman committed Jan 17, 2018
1 parent a09e906 commit ded4219
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
47 changes: 47 additions & 0 deletions src/connection/__tests__/arrayconnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,53 @@ describe('connectionFromArraySlice()', () => {
}
});
});

it('can populate edges and resolve nodes from the array', () => {
const letterEdgeData = [
{
letter: 'A',
isFirst: true,
},
{
letter: 'B',
isFirst: false,
}
];

const c = connectionFromArraySlice(
letterEdgeData,
{
first: 2,
},
{
sliceStart: 0,
arrayLength: 2,
resolveNode: ({ letter }) => letter,
}
);
return expect(c).to.deep.equal({
edges: [
{
isFirst: true,
node: 'A',
letter: 'A',
cursor: 'YXJyYXljb25uZWN0aW9uOjA=',
},
{
isFirst: false,
node: 'B',
letter: 'B',
cursor: 'YXJyYXljb25uZWN0aW9uOjE=',
},
],
pageInfo: {
startCursor: 'YXJyYXljb25uZWN0aW9uOjA=',
endCursor: 'YXJyYXljb25uZWN0aW9uOjE=',
hasPreviousPage: false,
hasNextPage: false,
}
});
});
});

describe('connectionFromPromisedArraySlice()', () => {
Expand Down
17 changes: 12 additions & 5 deletions src/connection/arrayconnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export function connectionFromArraySlice<T>(
meta: ArraySliceMetaInfo
): Connection<T> {
const { after, before, first, last } = args;
const { sliceStart, arrayLength } = meta;
const { sliceStart, arrayLength, resolveNode } = meta;
const sliceEnd = sliceStart + arraySlice.length;
const beforeOffset = getOffsetWithDefault(before, arrayLength);
const afterOffset = getOffsetWithDefault(after, -1);
Expand Down Expand Up @@ -110,10 +110,17 @@ export function connectionFromArraySlice<T>(
arraySlice.length - (sliceEnd - endOffset)
);

const edges = slice.map((value, index) => ({
cursor: offsetToCursor(startOffset + index),
node: value,
}));
const edges = slice.map((value, index) => {
const newEdge = {
cursor: offsetToCursor(startOffset + index),
node: resolveNode ? resolveNode(value) : value,
};

if (resolveNode) {
return { ...newEdge, ...value };
}
return newEdge;
});

const firstEdge = edges[0];
const lastEdge = edges[edges.length - 1];
Expand Down

0 comments on commit ded4219

Please sign in to comment.