Gathering detailed insights and metrics for @graphile-contrib/pg-many-to-many
Gathering detailed insights and metrics for @graphile-contrib/pg-many-to-many
Gathering detailed insights and metrics for @graphile-contrib/pg-many-to-many
Gathering detailed insights and metrics for @graphile-contrib/pg-many-to-many
@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
@graphile-contrib/pg-omit-archived
Makes collections omit archived items by default, but gives the ability to view archived items if explicitly requested.
@graphile-contrib/pg-order-by-multi-column-index
Order by multi-column indexes on PostGraphile connections
npm install @graphile-contrib/pg-many-to-many
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
81 Stars
113 Commits
10 Forks
4 Watching
8 Branches
9 Contributors
Updated on 17 Sept 2024
Minified
Minified + Gzipped
JavaScript (98.98%)
Shell (1.02%)
Cumulative downloads
Total Downloads
Last day
55%
1,789
Compared to previous day
Last week
30.3%
7,247
Compared to previous week
Last month
5.3%
27,124
Compared to previous month
Last year
-50.3%
299,682
Compared to previous year
This Graphile Engine plugin adds connection fields for many-to-many relations.
Requires
postgraphile@^4.5.0
orgraphile-build-pg@^4.5.0
Example:
1{ 2 allPeople { 3 nodes { 4 personName 5 # 👇 many-to-many relation 6 teamsByTeamMemberPersonIdAndTeamId { 7 nodes { 8 teamName 9 } 10 } 11 } 12 } 13}
Append this plugin and the additional fields will be added to your schema.
1yarn add postgraphile 2yarn add @graphile-contrib/pg-many-to-many 3npx postgraphile --append-plugins @graphile-contrib/pg-many-to-many
1const express = require("express"); 2const { postgraphile } = require("postgraphile"); 3const PgManyToManyPlugin = require("@graphile-contrib/pg-many-to-many"); 4 5const app = express(); 6 7app.use( 8 postgraphile(process.env.DATABASE_URL, "app_public", { 9 appendPlugins: [PgManyToManyPlugin], 10 graphiql: true, 11 }) 12); 13 14app.listen(5000);
To exclude certain many-to-many fields from appearing in your GraphQL schema, you can use @omit manyToMany
smart comments on constraints and tables.
Here is an example of using a smart comment on a constraint:
create table p.foo (
id serial primary key,
name text not null
);
create table p.bar (
id serial primary key,
name text not null
);
create table p.qux (
foo_id int constraint qux_foo_id_fkey references p.foo (id),
bar_id int constraint qux_bar_id_fkey references p.bar (id),
primary key (foo_id, bar_id)
);
-- `Foo` and `Bar` would normally have `barsBy...` and `foosBy...` fields,
-- but this smart comment causes the constraint between `qux` and `bar`
-- to be ignored, preventing the fields from being generated.
comment on constraint qux_bar_id_fkey on p.qux is E'@omit manyToMany';
Here is an example of using a smart comment on a table:
create table p.foo (
id serial primary key,
name text not null
);
create table p.bar (
id serial primary key,
name text not null
);
create table p.corge (
foo_id int constraint corge_foo_id_fkey references p.foo (id),
bar_id int constraint corge_bar_id_fkey references p.bar (id),
primary key (foo_id, bar_id)
);
-- `Foo` and `Bar` would normally have `barsBy...` and `foosBy...` fields,
-- but this smart comment causes `corge` to be excluded from consideration
-- as a junction table, preventing the fields from being generated.
comment on table p.corge is E'@omit manyToMany';
To avoid naming conflicts, this plugin uses a verbose naming convention (e.g. teamsByTeamMemberTeamId
), similar to how related fields are named by default in PostGraphile v4. You can override this by writing a custom inflector plugin or by using smart comments in your SQL schema.
Writing a custom inflector plugin gives you full control over the GraphQL field names. Here is an example plugin that shortens the field names to just the table name (producing e.g. teams
):
:warning: Warning: Simplifying the field names as shown below will lead to field name conflicts if your junction table has multiple foreign keys referencing the same table. You will need to customize the inflector function to resolve the conflicts.
1const { makeAddInflectorsPlugin } = require("graphile-utils");
2
3module.exports = makeAddInflectorsPlugin(
4 {
5 manyToManyRelationByKeys(
6 _leftKeyAttributes,
7 _junctionLeftKeyAttributes,
8 _junctionRightKeyAttributes,
9 _rightKeyAttributes,
10 _junctionTable,
11 rightTable,
12 _junctionLeftConstraint,
13 junctionRightConstraint
14 ) {
15 if (junctionRightConstraint.tags.manyToManyFieldName) {
16 return junctionRightConstraint.tags.manyToManyFieldName;
17 }
18 return this.camelCase(
19 `${this.pluralize(this._singularizedTableName(rightTable))}`
20 );
21 },
22 manyToManyRelationByKeysSimple(
23 _leftKeyAttributes,
24 _junctionLeftKeyAttributes,
25 _junctionRightKeyAttributes,
26 _rightKeyAttributes,
27 _junctionTable,
28 rightTable,
29 _junctionLeftConstraint,
30 junctionRightConstraint
31 ) {
32 if (junctionRightConstraint.tags.manyToManySimpleFieldName) {
33 return junctionRightConstraint.tags.manyToManySimpleFieldName;
34 }
35 return this.camelCase(
36 `${this.pluralize(this._singularizedTableName(rightTable))}-list`
37 );
38 },
39 },
40 true // Passing true here allows the plugin to overwrite existing inflectors.
41);
For more information on custom inflector plugins, see the makeAddInflectorsPlugin documentation.
The @manyToManyFieldName
and @manyToManySimpleFieldName
smart comments allow you to override the field names generated by this plugin.
For example, to rename the Connection field from teamsByTeamMemberTeamId
to teams
:
1comment on constraint membership_team_id_fkey on p.membership is E'@manyToManyFieldName teams';
To rename both the Connection and simple collection fields (assuming simple collections are enabled):
1comment on constraint membership_team_id_fkey on p.membership is E'@manyToManyFieldName teams\n@manyToManySimpleFieldName teamsList';
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
4 existing vulnerabilities detected
Details
Reason
Found 2/10 approved changesets -- score normalized to 2
Reason
2 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 1
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
security policy file not detected
Details
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