Skip to content

Commit

Permalink
Merge pull request #32 from sammarks/edge-customization
Browse files Browse the repository at this point in the history
feat: add modifyEdgeFn for edge customization
  • Loading branch information
dmerrill6 authored Jan 27, 2020
2 parents e150e50 + aee620d commit 85b37be
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 2 deletions.
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,24 @@ database column name is "created_at" and the column name on the model is "create
```
</details>

#### Customizing Edges

If you have additional metadata you would like to pass along with each edge, as is allowed by the Relay
specification, you may do so using the `modifyEdgeFn` option:

```javascript
const result = await paginate(
baseQuery,
{ first, last, before, after, orderBy, orderDirection },
{
modifyEdgeFn: (edge) => ({
...edge,
custom: 'foo',
})
}
);
```

### Creating your own connector

Only Knex.js is implemented for now. If you want to connect to a different ORM, you must make your own connector.
Expand Down
9 changes: 7 additions & 2 deletions src/builder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ const apolloCursorPaginationBuilder = ({
},
opts = {},
) => {
const { isAggregateFn, formatColumnFn, skipTotalCount = false } = opts;
const {
isAggregateFn, formatColumnFn, skipTotalCount = false, modifyEdgeFn,
} = opts;
let {
orderColumn, ascOrDesc,
} = opts;
Expand Down Expand Up @@ -146,11 +148,14 @@ const apolloCursorPaginationBuilder = ({
getNodesLength,
});

const edges = convertNodesToEdges(nodes, {
let edges = convertNodesToEdges(nodes, {
before, after, first, last,
}, {
orderColumn, ascOrDesc, isAggregateFn, formatColumnFn,
});
if (modifyEdgeFn) {
edges = edges.map(edge => modifyEdgeFn(edge));
}

const startCursor = edges[0] && edges[0].cursor;
const endCursor = edges[edges.length - 1] && edges[edges.length - 1].cursor;
Expand Down
4 changes: 4 additions & 0 deletions tests/test-app/src/queries/cat/root/cats-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export default async (_, args) => {
{
isAggregateFn: column => column === 'idsum',
formatColumnFn: column => (column === 'idsum' ? Cat.knex().raw('sum(id)') : column),
modifyEdgeFn: edge => ({
...edge,
custom: 'foo',
}),
},
);
return result;
Expand Down
1 change: 1 addition & 0 deletions tests/test-app/src/type-defs/cat.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const Cat = `
type CatEdge {
cursor: String!
node: Cat!
custom: String
}
enum OrderDirection {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -678,4 +678,30 @@ describe('getCatsByOwner root query', () => {
expect(response2.body.data.catsConnection.edges[1].node.lastName).not.toEqual(null);
});
});

describe('modifyEdgeFn', () => {
it('modifies edges per the callback', async () => {
const query = `
{
catsConnection(first: 2) {
edges {
cursor
node {
id
name
}
custom
}
}
}
`;
const response = await graphqlQuery(app, query);
expect(response.body.errors).not.toBeDefined();
expect(response.body.data.catsConnection.edges).toHaveLength(2);
expect(response.body.data.catsConnection.edges.map(edge => edge.node.name))
.toEqual([cat1.name, cat2.name]);
expect(response.body.data.catsConnection.edges.map(edge => edge.custom))
.toEqual(['foo', 'foo']);
});
});
});

0 comments on commit 85b37be

Please sign in to comment.