Skip to content

Commit 85b37be

Browse files
authored
Merge pull request #32 from sammarks/edge-customization
feat: add modifyEdgeFn for edge customization
2 parents e150e50 + aee620d commit 85b37be

File tree

6 files changed

+57
-2
lines changed

6 files changed

+57
-2
lines changed

.nvmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
10

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,24 @@ database column name is "created_at" and the column name on the model is "create
9292
```
9393
</details>
9494

95+
#### Customizing Edges
96+
97+
If you have additional metadata you would like to pass along with each edge, as is allowed by the Relay
98+
specification, you may do so using the `modifyEdgeFn` option:
99+
100+
```javascript
101+
const result = await paginate(
102+
baseQuery,
103+
{ first, last, before, after, orderBy, orderDirection },
104+
{
105+
modifyEdgeFn: (edge) => ({
106+
...edge,
107+
custom: 'foo',
108+
})
109+
}
110+
);
111+
```
112+
95113
### Creating your own connector
96114

97115
Only Knex.js is implemented for now. If you want to connect to a different ORM, you must make your own connector.

src/builder/index.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ const apolloCursorPaginationBuilder = ({
109109
},
110110
opts = {},
111111
) => {
112-
const { isAggregateFn, formatColumnFn, skipTotalCount = false } = opts;
112+
const {
113+
isAggregateFn, formatColumnFn, skipTotalCount = false, modifyEdgeFn,
114+
} = opts;
113115
let {
114116
orderColumn, ascOrDesc,
115117
} = opts;
@@ -146,11 +148,14 @@ const apolloCursorPaginationBuilder = ({
146148
getNodesLength,
147149
});
148150

149-
const edges = convertNodesToEdges(nodes, {
151+
let edges = convertNodesToEdges(nodes, {
150152
before, after, first, last,
151153
}, {
152154
orderColumn, ascOrDesc, isAggregateFn, formatColumnFn,
153155
});
156+
if (modifyEdgeFn) {
157+
edges = edges.map(edge => modifyEdgeFn(edge));
158+
}
154159

155160
const startCursor = edges[0] && edges[0].cursor;
156161
const endCursor = edges[edges.length - 1] && edges[edges.length - 1].cursor;

tests/test-app/src/queries/cat/root/cats-connection.js

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ export default async (_, args) => {
2222
{
2323
isAggregateFn: column => column === 'idsum',
2424
formatColumnFn: column => (column === 'idsum' ? Cat.knex().raw('sum(id)') : column),
25+
modifyEdgeFn: edge => ({
26+
...edge,
27+
custom: 'foo',
28+
}),
2529
},
2630
);
2731
return result;

tests/test-app/src/type-defs/cat.js

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const Cat = `
1414
type CatEdge {
1515
cursor: String!
1616
node: Cat!
17+
custom: String
1718
}
1819
1920
enum OrderDirection {

tests/test-app/tests/apollo-cursor-pagination/knex-implementation.test.js

+26
Original file line numberDiff line numberDiff line change
@@ -678,4 +678,30 @@ describe('getCatsByOwner root query', () => {
678678
expect(response2.body.data.catsConnection.edges[1].node.lastName).not.toEqual(null);
679679
});
680680
});
681+
682+
describe('modifyEdgeFn', () => {
683+
it('modifies edges per the callback', async () => {
684+
const query = `
685+
{
686+
catsConnection(first: 2) {
687+
edges {
688+
cursor
689+
node {
690+
id
691+
name
692+
}
693+
custom
694+
}
695+
}
696+
}
697+
`;
698+
const response = await graphqlQuery(app, query);
699+
expect(response.body.errors).not.toBeDefined();
700+
expect(response.body.data.catsConnection.edges).toHaveLength(2);
701+
expect(response.body.data.catsConnection.edges.map(edge => edge.node.name))
702+
.toEqual([cat1.name, cat2.name]);
703+
expect(response.body.data.catsConnection.edges.map(edge => edge.custom))
704+
.toEqual(['foo', 'foo']);
705+
});
706+
});
681707
});

0 commit comments

Comments
 (0)