Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
@graphile-contrib/pg-order-by-related
Advanced tools
Order by related columns on PostGraphile connections
This Graphile Engine plugin adds additional enum values to the orderBy
argument on connections, allowing you to order by columns in related tables.
Requires
postgraphile@^4.3.1
orgraphile-build-pg@^4.3.1
Example:
{
# additional enum values exposed here 👇
allPosts(orderBy: PERSON_BY_AUTHOR_ID__CREATED_AT_ASC) {
nodes {
headline
personByAuthorId {
id
name
about
}
}
}
}
One-to-one and many-to-one relations are supported. For one-to-many relations, __COUNT_ASC
/__COUNT_DESC
enums allow ordering by the number of related records.
Append this plugin and the additional orderBy
options will be added to your schema.
yarn add postgraphile
yarn add @graphile-contrib/pg-order-by-related
npx postgraphile --append-plugins @graphile-contrib/pg-order-by-related
const express = require("express");
const { postgraphile } = require("postgraphile");
const PgOrderByRelatedPlugin = require("@graphile-contrib/pg-order-by-related");
const app = express();
app.use(
postgraphile(process.env.DATABASE_URL, "app_public", {
appendPlugins: [PgOrderByRelatedPlugin],
graphiql: true,
})
);
app.listen(5000);
To avoid naming conflicts, this plugin uses a <TABLE>_BY_<KEY>
naming convention (e.g. USER_BY_AUTHOR_ID__CREATED_AT_ASC
), similar to how related fields are named by default in PostGraphile v4.
You can override this by adding an inflector plugin. For example, the following plugin shortens the names by dropping the <TABLE>_BY
portion (producing e.g. AUTHOR_ID__CREATED_AT_ASC
):
const { makeAddInflectorsPlugin } = require("graphile-utils");
module.exports = makeAddInflectorsPlugin(
{
orderByRelatedColumnEnum(attr, ascending, foreignTable, keyAttributes) {
return `${this.constantCase(
keyAttributes.map((keyAttr) => this._columnName(keyAttr)).join("-and-")
)}__${this.orderByColumnEnum(attr, ascending)}`;
},
},
true // Passing true here allows the plugin to overwrite existing inflectors.
);
See the makeAddInflectorsPlugin documentation for more information.
When using PostGraphile as a library, the following options can be specified via graphileBuildOptions
.
Adds additional enum values for column aggregates (currently min
and max
) for one-to-many relationships.
Example:
postgraphile(pgConfig, schema, {
graphileBuildOptions: {
orderByRelatedColumnAggregates: true,
},
});
{
allPersons(orderBy: POSTS_BY_AUTHOR_ID__MAX_CREATED_AT_ASC, first: 10) {
nodes {
id
name
}
}
}
To establish a test environment, create an empty PostgreSQL database and set a TEST_DATABASE_URL
environment variable with your database connection string.
createdb graphile_test
export TEST_DATABASE_URL=postgres://localhost:5432/graphile_test
yarn
yarn test
FAQs
Order by related columns on PostGraphile connections
The npm package @graphile-contrib/pg-order-by-related receives a total of 2,253 weekly downloads. As such, @graphile-contrib/pg-order-by-related popularity was classified as popular.
We found that @graphile-contrib/pg-order-by-related demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers 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
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.