Installations
npm install @graphile-contrib/pg-simplify-inflector
Developer
graphile-contrib
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
No
Node Version
14.11.0
NPM Version
6.14.8
Statistics
89 Stars
114 Commits
9 Forks
3 Watching
4 Branches
11 Contributors
Updated on 26 Nov 2024
Languages
JavaScript (49.89%)
TypeScript (43.93%)
PLpgSQL (6.18%)
Total Downloads
Cumulative downloads
Total Downloads
4,176,295
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
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
2
@graphile-contrib/pg-simplify-inflector
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.
Customising
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.
Changes:
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 viapgSimplifyAllRows = false
)Query.allBeverages
👉Query.beverages
Beverage.companyByCompanyId
👉Beverage.company
Beverage.companyByDistributorId
👉Beverage.distributor
Company.beveragesByCompanyId
👉Company.beverages
(because thecompany_id
column follows the[table_name]_id
naming convention)- All update mutations now accept
patch
instead ofcompanyPatch
/beveragePatch
(disable viapgSimplifyPatch = false
) - If you are using
pgSimpleCollections = "only"
then you can setpgOmitListSuffix = true
to omit theList
suffix - Fields where the singular and plural are the same and a distinct plural is
required are force-pluralised ("fishes") to avoid conflicts (e.g.
singularize("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);
Installation:
1yarn add @graphile-contrib/pg-simplify-inflector
or
1npm install --save @graphile-contrib/pg-simplify-inflector
Usage:
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);
Naming your foreign key fields
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.
Handling field conflicts:
In most cases, the conflict errors will guide you on how to fix these issues using smart comments.
Smart Tags
@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:
- foreign key constraints
@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:
- tables
- foreign key constraints
- computed column functions returning
SETOF <record type>
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE.md:0
- Info: FSF or OSI recognized license: MIT License: LICENSE.md:0
Reason
3 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-8cf7-32gw-wr33
- Warn: Project is vulnerable to: GHSA-hjrf-2m68-5959
- Warn: Project is vulnerable to: GHSA-qwph-4952-7xr6
Reason
security policy file detected
Details
- Info: security policy file detected: github.com/graphile/.github/SECURITY.md:1
- Warn: no linked content found
- Info: Found disclosure, vulnerability, and/or timelines in security policy: github.com/graphile/.github/SECURITY.md:1
- Info: Found text in security policy: github.com/graphile/.github/SECURITY.md:1
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
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'main'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 9 are checked with a SAST tool
Score
3.1
/10
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 MoreOther packages similar to @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