postgraphile-plugin-nested-mutations
postgraphile-plugin-nested-mutations copied to clipboard
Tables that relate to themselves
I have noticed tables that reference themselves will only work one way with this plugin.
In the example scenario, you can nested insert Child -> Parent OR Parent -> Child
create table app.parent (
id serial primary key,
name text not null
);
create table app.child (
id serial primary key,
parent_id integer,
name text not null,
constraint child_parent_fkey foreign key (parent_id)
references app.parent (id)
);
But if you make it a single person table it will only let you nested insert Child -> Parent
create table app.person (
id serial primary key,
parent_id integer,
name text not null,
constraint person_parent_fkey foreign key (parent_id)
references app.person(id)
);
I am facing the same problem, it must come from an inflection issue, I'll try to look into it.
I digged a little bit into this.
I modified the package with https://www.npmjs.com/package/patch-package.
Inside PostgraphileNestedTypesPlugin l.177, I modified the isForward
condition. It was masking some reverse relations when their constraints were related to themselves, eg :
const isForward = constraint.classId === table.id
I switched it to :
const isForward = constraint.classId === table.id && constraint.classId !== constraint.foreignClassId;
To allow these special kind of constraints to work.
Untested, use at your own risk.
I would be glad to make a PR but the repo owner doesn't seem to be maintaining it.
Sorry for the delay. Changing the test for isForward
just swaps the problem around -- you now get the the parent > child nested mutation, but lose the child > parent nested mutation. To fix this properly needs a bit of a refactor of the code I think. I've added a test for the currently working direction (child > parent), and I'll do some refactoring to make the other direction work right.