diff --git a/README.md b/README.md index f489383..4216406 100644 --- a/README.md +++ b/README.md @@ -162,32 +162,32 @@ Our mutation type then creates the `introduceShip` field using the return value After cloning this repo, ensure dependencies are installed by running: ```sh -npm install +yarn install ``` This library is written in ES6 and uses [Babel](http://babeljs.io/) for ES5 transpilation and [Flow](http://flowtype.org/) for type safety. Widely consumable JavaScript can be produced by running: ```sh -npm run build +yarn build ``` -Once `npm run build` has run, you may `import` or `require()` directly from node. +Once `yarn build` has run, you may `import` or `require()` directly from node. After developing, the full test suite can be evaluated by running: ```sh -npm test +yarn test ``` While actively developing, we recommend running ```sh -npm run watch +yarn watch ``` in a terminal. This will watch the file system run lint, tests, and type checking automatically whenever you save a js file. -To lint the JS files and run type interface checks run `npm run lint`. +To lint the JS files and run type interface checks run `yarn lint`. ## License diff --git a/package.json b/package.json index e59f64f..beede93 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ }, "scripts": { "prepublish": "./resources/prepublish.sh", - "test": "npm run lint && npm run check && npm run testonly", + "test": "yarn lint && yarn check && yarn testonly", "testonly": "babel-node ./node_modules/.bin/_mocha $npm_package_options_mocha", "lint": "eslint src", "check": "flow check", diff --git a/src/connection/__tests__/arrayconnection.js b/src/connection/__tests__/arrayconnection.js index edd108f..dc6c67d 100644 --- a/src/connection/__tests__/arrayconnection.js +++ b/src/connection/__tests__/arrayconnection.js @@ -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()', () => { diff --git a/src/connection/arrayconnection.js b/src/connection/arrayconnection.js index 2ec1056..6a12c82 100644 --- a/src/connection/arrayconnection.js +++ b/src/connection/arrayconnection.js @@ -21,6 +21,7 @@ import { type ArraySliceMetaInfo = { sliceStart: number; arrayLength: number; + resolveNode?: (edge: any) => any; }; /** @@ -68,7 +69,7 @@ export function connectionFromArraySlice( meta: ArraySliceMetaInfo ): Connection { 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); @@ -110,10 +111,17 @@ export function connectionFromArraySlice( 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];