Nested mutations plugin for PostGraphile
npm install postgraphile-plugin-nested-mutations

`` bash`
postgraphile --append-plugins postgraphile-plugin-nested-mutations
See here for
more information about loading plugins with PostGraphile.
` js
const express = require('express');
const { postgraphile } = require('postgraphile');
const PostGraphileNestedMutations = require('postgraphile-plugin-nested-mutations');
const app = express();
app.use(
postgraphile(pgConfig, schema, {
appendPlugins: [
PostGraphileNestedMutations,
],
})
);
app.listen(5000);
`
When using PostGraphile as a library, the following plugin options can be passed
via graphileBuildOptions:
Use simple field names for nested mutations. Instead of names suffixed with
tableBy and tableUsing, tables with a single foreign key relationship table
between them will have their nested relation fields named . Defaults tofalse.
`js`
postgraphile(pgConfig, schema, {
graphileBuildOptions: {
nestedMutationsSimpleFieldNames: true,
}
});
Controls whether the deleteOthers field is available on nested mutations. Defaultstrue
to .
`js`
postgraphile(pgConfig, schema, {
graphileBuildOptions: {
nestedMutationsDeleteOthers: false,
}
});
If enabled, plural names for one-to-one relations will be used. For backwards
compatibility. Defaults to false.
`js`
postgraphile(pgConfig, schema, {
graphileBuildOptions: {
nestedMutationsOldUniqueFields: false,
}
});
This plugin creates an additional field on each GraphQL Input type for every forward
and reverse foreign key relationship on a table, with the same name as the foreign table.
Each nested mutation field will have the following fields. They will accept an array if
the relationship is a one-to-many relationship, or a single input if they are one-to-one.
Connect using a nodeId from the nested table.####
connectBy
Connect using any readable primary key or unique constraint on the nested table.$3
#### create
Create a new record in the nested table.$3
#### deleteByNodeId
Delete using a nodeId from the nested table.####
deleteBy
Delete using any readable primary key or unique constraint on the nested table.$3
#### updateByNodeId
Update a record using a nodeId from the nested table.####
updatedBy
Update a record using any readable primary key or unique constraint on the nested table.Example
`sql
create table parent (
id serial primary key,
name text not null
);create table child (
id serial primary key,
parent_id integer,
name text not null,
constraint child_parent_fkey foreign key (parent_id)
references p.parent (id)
);
`A nested mutation against this schema, using
Parent as the base mutation
would look like this:` graphql
mutation {
createParent(input: {
parent: {
name: "Parent 1"
childrenUsingId: {
connectById: [{
id: 1
}]
create: [{
name: "Child 1"
}, {
name: "Child 2"
}]
}
}
}) {
parent {
id
name
childrenByParentId {
nodes {
id
name
}
}
}
}
}
`Or using
Child as the base mutation:` graphql
mutation {
createChild(input: {
child: {
name: "Child 1"
parentToParentId: {
create: {
name: "Parent of Child 1"
}
}
},
}) {
child {
id
name
parentByParentId {
id
name
}
}
}
}
`Smart Comments
Smart comments are supported for
renaming the nested mutation fields.
`sql
comment on constraint child_parent_fkey on child is
E'@fieldName parent\n@foreignFieldName children';
``