Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a resolveNode option to allow input data to be used for the edges in a connection rather than the node #197

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Copy link

@jaydenseric jaydenseric May 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is bad practice; non yarn users/environments (the majority, and industry standard) will not be able to run the script. Yarn knows to substitute npm run for yarn automatically when running npm scripts.

"testonly": "babel-node ./node_modules/.bin/_mocha $npm_package_options_mocha",
"lint": "eslint src",
"check": "flow check",
Expand Down
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
18 changes: 13 additions & 5 deletions src/connection/arrayconnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
type ArraySliceMetaInfo = {
sliceStart: number;
arrayLength: number;
resolveNode?: (edge: any) => any;
};

/**
Expand Down Expand Up @@ -68,7 +69,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 +111,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