Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
apollo-cursor-pagination
Advanced tools
Readme
Implementation of Relay's Connection specs for Apollo Server. Allows your Apollo Server to do cursor-based pagination. It can connect to any ORM, but only the connection with Knex.js is implemented currently.
yarn add apollo-cursor-pagination
Use the paginate
function of the connector in your GraphQL resolver.
paginate
receives the following arguments:
1- nodesAccessor
: An object that can be queried or accessed to obtain a reduced result set.
2- args
: GraphQL args for your connection. Can have the following fields: first
, last
, before
, after
.
3- orderArgs
: If using a connector with stable cursor, you must indicate to paginate
how are you sorting your query. Must contain orderColumn
(which attribute you are ordering by) and ascOrDesc
(which can be asc
or desc
). Note: apollo-cursor-pagination does not sort your query, you must do it yourself before calling paginate
.
For example with the knex connector:
// cats-connection.js
import { knexPaginator as paginate } from 'apollo-cursor-pagination';
import knex from '../../../db'; // Or instantiate a connection here
export default async (_, args) => {
// orderBy must be the column to sort with or an array of columns for ordering by multiple fields
// orderDirection must be 'asc' or 'desc', or an array of those values if ordering by multiples
const {
first, last, before, after, orderBy, orderDirection,
} = args;
const baseQuery = knex('cats');
const result = await paginate(baseQuery, {first, last, before, after, orderBy, orderDirection});
/* result will contain:
* edges
* totalCount
* pageInfo { hasPreviousPage, hasNextPage, }
*/
return result;
};
If you are using something like Objection and have
mapped the column names to something like snakeCase instead of camel_case, you'll want to use
the formatColumnFn
option to make sure you're ordering by and building cursors from the correct
column name:
const result = await paginate(
baseQuery,
{ first, last, before, after, orderBy, orderDirection },
{
formatColumnFn: (column) => {
// Logic to transform your column name goes here...
return column
}
}
);
Important note: Make sure you pass the un-formatted version as your orderBy
argument. It helps
to create a wrapper around the paginate()
function that enforces this. For example, if the formatted
database column name is "created_at" and the column name on the model is "createdAt," you would pass
"createdAt" as the orderBy
argument.
const result = await paginate(
baseQuery,
{ first, last, before, after, orderBy, orderDirection },
{
formatColumnFn: (column) => {
if (Model.columnNameMappers && Model.columnNameMappers.format) {
const result = Model.columnNameMappers.format({ [column]: true })
return Object.keys(result)[0]
} else {
return column
}
}
}
);
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:
const result = await paginate(
baseQuery,
{ first, last, before, after, orderBy, orderDirection },
{
modifyEdgeFn: (edge) => ({
...edge,
custom: 'foo',
})
}
);
Only Knex.js is implemented for now. If you want to connect to a different ORM, you must make your own connector.
To create your own connector:
1- Import apolloCursorPaginationBuilder
from src/builder/index.js
2- Call apolloCursorPaginationBuilder
with the specified params. It will generate a paginate
function that you can export to use in your resolvers.
You can base off from src/orm-connectors/knex/custom-pagination.js
.
Pull requests are welcome, specially to implement new connectors for different ORMs / Query builders.
When submitting a pull request, please include tests for the code you are submitting, and check that you did not break any working test.
1- Code your changes
2- Run yarn build
. This will generate a dist
folder with the distributable files
3- cd
into the test app and run yarn install
and then yarn test
. Check that all tests pass.
4- Send the PR. When accepted, the maintainer will publish a new version to npm using the new dist
folder.
Do not publish using yarn, as it doesn't publishes all dependencies. Use npm publish
.
1- yarn link
2- cd tests/test-app
3- yarn link apollo-cursor-pagination
2- yarn install
3- yarn test
FAQs
Relay's Connection implementation for Apollo Server GraphQL library
The npm package apollo-cursor-pagination receives a total of 293 weekly downloads. As such, apollo-cursor-pagination popularity was classified as not popular.
We found that apollo-cursor-pagination demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.