Gathering detailed insights and metrics for @graphile-contrib/pg-simplify-inflector
Gathering detailed insights and metrics for @graphile-contrib/pg-simplify-inflector
Gathering detailed insights and metrics for @graphile-contrib/pg-simplify-inflector
Gathering detailed insights and metrics for @graphile-contrib/pg-simplify-inflector
@graphile-contrib/pg-many-to-many
Add connection fields for many-to-many relations
postgraphile-core
<span class="badge-patreon"><a href="https://patreon.com/benjie" title="Support Graphile development on Patreon"><img src="https://img.shields.io/badge/sponsor-via%20Patreon-orange.svg" alt="Patreon sponsor button" /></a></span> [![Discord chat room](http
@graphile-contrib/pg-order-by-related
Order by related columns on PostGraphile connections
graphile-build-pg
Build a GraphQL schema by reflection over a PostgreSQL schema. Easy to customize since it's built with plugins on graphile-build
npm install @graphile-contrib/pg-simplify-inflector
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
89 Stars
114 Commits
9 Forks
3 Watching
4 Branches
11 Contributors
Updated on 26 Nov 2024
JavaScript (49.89%)
TypeScript (43.93%)
PLpgSQL (6.18%)
Cumulative downloads
Total Downloads
Last day
25.2%
4,796
Compared to previous day
Last week
19.5%
22,155
Compared to previous week
Last month
13.1%
86,949
Compared to previous month
Last year
-19.1%
921,916
Compared to previous year
2
This plugin simplifies field names in the PostGraphile schema; e.g. allUsers
becomes simply users
, User.postsByAuthorId
becomes simply User.posts
, and
Post.userByAuthorId
becomes simply Post.author
.
Adding this plugin to your schema is almost certainly a breaking change, so do it before you ship anything! This is the primary reason this isn't enabled by default in PostGraphile.
This plugin is recommended for all PostGraphile users.
This plugin is implemented as a single JS file that does not need to be compiled at all - you can simply copy it into your project and customise it as you see fit.
Alternatively, you can write your own inflection plugin.
Given these tables:
1create table companies ( 2 id serial primary key, 3 name text not null 4); 5create table beverages ( 6 id serial primary key, 7 company_id int not null references companies, 8 distributor_id int references companies, 9 name text not null 10);
Query.allCompanies
👉 Query.companies
(disable via
pgSimplifyAllRows = false
)Query.allBeverages
👉 Query.beverages
Beverage.companyByCompanyId
👉 Beverage.company
Beverage.companyByDistributorId
👉 Beverage.distributor
Company.beveragesByCompanyId
👉 Company.beverages
(because the
company_id
column follows the [table_name]_id
naming convention)patch
instead of companyPatch
/
beveragePatch
(disable via pgSimplifyPatch = false
)pgSimpleCollections = "only"
then you can set
pgOmitListSuffix = true
to omit the List
suffixsingularize("fish") === pluralize("fish")
).Note: Company.beveragesByDistributorId
will remain, because distributor_id
does not follow the [table_name]_id
naming convention, but you could rename
this yourself with a smart comment:
1comment on constraint "beverages_distributor_id_fkey" on "beverages" is 2 E'@foreignFieldName distributedBeverages';
or with a custom inflector:
1module.exports = makeAddInflectorsPlugin( 2 { 3 getOppositeBaseName(baseName) { 4 return ( 5 { 6 // These are the default opposites 7 parent: "child", 8 child: "parent", 9 author: "authored", 10 editor: "edited", 11 reviewer: "reviewed", 12 13 // 👇 Add/customise this line: 14 distributor: "distributed", 15 }[baseName] || null 16 ); 17 }, 18 }, 19 true 20);
1yarn add @graphile-contrib/pg-simplify-inflector
or
1npm install --save @graphile-contrib/pg-simplify-inflector
CLI:
1postgraphile --append-plugins @graphile-contrib/pg-simplify-inflector
Library:
1const PgSimplifyInflectorPlugin = require("@graphile-contrib/pg-simplify-inflector"); 2 3// ... 4 5app.use( 6 postgraphile(process.env.AUTH_DATABASE_URL, "app_public", { 7 appendPlugins: [PgSimplifyInflectorPlugin], 8 9 // Optional customisation 10 graphileBuildOptions: { 11 /* 12 * Uncomment if you want simple collections to lose the 'List' suffix 13 * (and connections to gain a 'Connection' suffix). 14 */ 15 //pgOmitListSuffix: true, 16 /* 17 * Uncomment if you want 'userPatch' instead of 'patch' in update 18 * mutations. 19 */ 20 //pgSimplifyPatch: false, 21 /* 22 * Uncomment if you want 'allUsers' instead of 'users' at root level. 23 */ 24 //pgSimplifyAllRows: false, 25 /* 26 * Uncomment if you want primary key queries and mutations to have 27 * `ById` (or similar) suffix; and the `nodeId` queries/mutations 28 * to lose their `ByNodeId` suffix. 29 */ 30 // pgShortPk: true, 31 }, 32 // ... other settings ... 33 }) 34);
By naming your foreign key along the lines of author_id
or author_fk
, e.g.:
1CREATE TABLE posts ( 2 id serial primary key, 3 author_id int not null references users, 4 ... 5);
We can automatically extract the field prefix: author
and call the relation
author
rather than the default: user
. This allows for a post to have an
author
, editor
, reviewer
, etc. all which point to users
.
The reverse, however, is not so easy. On the User type, we can't call the
reverse of all these different relations posts
. The default inflector refers
to these as postsByAuthorId
, postsByEditorId
, etc. However we'd rather use
shorter names, so we introduce a new inflector: getOppositeBaseName
. This
inflector is passed a baseName (the part without the _id
/_fk
suffix, e.g.
author
, editor
, reviewer
above) and should return the opposite of that
base name which will be prepended to the target type to produce, e.g.
authoredPosts
, editedPosts
, reviewedPosts
. Failing this, we just fall back
to the default (verbose) inflector; it will be up to you to add smart comments
or a custom inflector to override these.
In most cases, the conflict errors will guide you on how to fix these issues using smart comments.
@foreignSimpleFieldName
@foreignSimpleFieldName
was added to override the naming of the foreign-side
of a one-to-many relationship's simple collections field (if you're using simple
collections). By default we'll take the @foreignFieldName
and add the "list
suffix" ("List" by default, but "" if pgOmitListSuffix
is set), but if you
prefer you can override it entirely with @foreignSimpleFieldName
. If you set
@foreignSimpleFieldName
and you're using simpleCollections 'both'
then you
should also set @foreignFieldName
explicitly or unexpected things may occur.
Applies to:
@listSuffix
@listSuffix
allows you to override the default naming on a per-entity basis,
overriding pgOmitListSuffix
. For example, with pgOmitListSuffix: true
, you
can apply @listSuffix include
to have the -List
suffix appended to the
simple collection generated for that table, and remove the -Connection
suffix
from the Relay connection. When pgOmitListSuffix
is not true
, you can use
@listSuffix omit
to selectively omit the -List
suffix on simple collections
and append -Connection
to the Relay connection instead.
If @listSuffix
is set, the only valid values are "omit"
and "include"
. Any
other value will cause an error.
@listSuffix omit | @listSuffix include | |
---|---|---|
Relay Connection | companiesConnection | companies |
Simple Collection | companies | companiesList |
NOTE:
@listSuffix
will have no effect when using@foreignSimpleFieldName
.
Applies to:
SETOF <record type>
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
3 existing vulnerabilities detected
Details
Reason
security policy file detected
Details
Reason
Found 3/24 approved changesets -- score normalized to 1
Reason
1 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More