Skip to content

Commit 4abffcf

Browse files
authored
fix(relations): respect relations statuses via different relations (#22)
1 parent a324c74 commit 4abffcf

File tree

2 files changed

+76
-19
lines changed

2 files changed

+76
-19
lines changed

__tests__/main.js

+75-19
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ const {
2020
const SQL = `
2121
drop schema if exists omit_archived cascade;
2222
create schema omit_archived;
23+
create table omit_archived.organizations (
24+
id int primary key,
25+
name text,
26+
is_archived boolean not null default false,
27+
archived_at timestamptz default null,
28+
is_published boolean not null default true,
29+
published_at timestamptz default now()
30+
);
2331
create table omit_archived.parents (
2432
id int primary key,
2533
name text,
@@ -30,26 +38,30 @@ create table omit_archived.parents (
3038
);
3139
create table omit_archived.children (
3240
id int primary key,
33-
parent_id int not null references omit_archived.parents,
41+
organization_id int not null references omit_archived.organizations,
42+
parent_id int not null,
3443
name text,
3544
is_archived boolean not null default false,
3645
archived_at timestamptz default null,
3746
is_published boolean not null default true,
38-
published_at timestamptz default now()
47+
published_at timestamptz default now(),
48+
constraint fk_children_parents foreign key (parent_id) references omit_archived.parents
3949
);
4050
create index on omit_archived.children(parent_id);
4151
create table omit_archived.other_children (
4252
id int primary key,
4353
parent_id int not null references omit_archived.parents,
4454
title text
4555
);
56+
insert into omit_archived.organizations (id, name, is_archived, archived_at, is_published, published_at)
57+
values (3, 'GoodOrganization', false, null, true, now()), (4, 'BadOrganization', true, now(), false, null);
4658
insert into omit_archived.parents (id, name, is_archived, archived_at, is_published, published_at)
4759
values (1, 'First', false, null, true, now()), (2, 'Second', true, now(), false, null);
48-
insert into omit_archived.children (id, parent_id, name, is_archived, archived_at, is_published, published_at) values
49-
(1001, 1, 'First child 1', false, null, true, now()),
50-
(1002, 1, 'First child 2', true, now(), false, null),
51-
(2001, 2, 'Second child 1', false, null, true, now()),
52-
(2002, 2, 'Second child 2', true, now(), false, null);
60+
insert into omit_archived.children (id, organization_id, parent_id, name, is_archived, archived_at, is_published, published_at) values
61+
(1001, 3, 1, 'First child 1', false, null, true, now()),
62+
(1002, 3, 1, 'First child 2', true, now(), false, null),
63+
(2001, 3, 2, 'Second child 1', false, null, true, now()),
64+
(2002, 3, 2, 'Second child 2', true, now(), false, null);
5365
insert into omit_archived.other_children (id, parent_id, title) values
5466
(101, 1, 'First other child 1'),
5567
(102, 1, 'First other child 2'),
@@ -67,11 +79,18 @@ beforeAll(() => {
6779
connectionString: process.env.TEST_DATABASE_URL || "pggql_test",
6880
});
6981
});
70-
beforeAll(() => pgPool.query(SQL));
7182
afterAll(() => pgPool.end());
7283

7384
describe.each([
7485
["default"],
86+
[
87+
"default_w_comment",
88+
undefined,
89+
undefined,
90+
{
91+
fk_children_parents: true,
92+
},
93+
],
7594
[
7695
"is_archived",
7796
"archived",
@@ -100,7 +119,8 @@ describe.each([
100119
pgDraftRelations: true,
101120
},
102121
],
103-
])("%s", (_columnName, keyword, graphileBuildOptions) => {
122+
])("%s", (_columnName, keyword, graphileBuildOptions, config = {}) => {
123+
const { fk_children_parents = null } = config;
104124
const Keyword = keyword
105125
? keyword[0].toUpperCase() + keyword.slice(1)
106126
: `Archived`;
@@ -117,6 +137,16 @@ describe.each([
117137
? graphileBuildOptions[pgRelationsAttr] || false
118138
: false;
119139
beforeAll(async () => {
140+
// Reset database between each tes set
141+
await pgPool.query(SQL);
142+
143+
// Load comments if needed
144+
if (fk_children_parents) {
145+
await pgPool.query(`\
146+
comment on constraint fk_children_parents on omit_archived.children is E'@archivedRelation';`);
147+
}
148+
149+
// Build schema
120150
schema = await createPostGraphileSchema(pgPool, ["omit_archived"], options);
121151
});
122152

@@ -209,7 +239,7 @@ describe.each([
209239
});
210240

211241
describe("children", () => {
212-
if (pgArchivedRelations) {
242+
if (pgArchivedRelations || fk_children_parents) {
213243
test(
214244
"Omits archived children (and those with archived parents) by default",
215245
check(
@@ -239,7 +269,7 @@ describe.each([
239269
);
240270
}
241271

242-
if (pgArchivedRelations) {
272+
if (pgArchivedRelations || fk_children_parents) {
243273
test(
244274
"Omits archived children (and those with archived parents) when NO",
245275
check(
@@ -269,7 +299,7 @@ describe.each([
269299
);
270300
}
271301

272-
if (pgArchivedRelations) {
302+
if (pgArchivedRelations || fk_children_parents) {
273303
test(
274304
"Includes everything (except those with archived parents) when YES",
275305
check(
@@ -324,7 +354,7 @@ describe.each([
324354
);
325355
}
326356

327-
if (pgArchivedRelations) {
357+
if (pgArchivedRelations || fk_children_parents) {
328358
test(
329359
"Includes only archived (with non-archived parents) when EXCLUSIVELY",
330360
check(
@@ -552,7 +582,7 @@ describe.each([
552582
});
553583

554584
describe("children", () => {
555-
if (pgArchivedRelations) {
585+
if (pgArchivedRelations || fk_children_parents) {
556586
test(
557587
"Omits archived children (and those with archived parents) by default",
558588
check(
@@ -578,7 +608,7 @@ describe.each([
578608
);
579609
}
580610

581-
if (pgArchivedRelations) {
611+
if (pgArchivedRelations || fk_children_parents) {
582612
test(
583613
"Omits archived children (and those with archived parents) when NO",
584614
check(
@@ -604,7 +634,7 @@ describe.each([
604634
);
605635
}
606636

607-
if (pgArchivedRelations) {
637+
if (pgArchivedRelations || fk_children_parents) {
608638
test(
609639
"Includes everything (except those with archived parents) when YES",
610640
check(
@@ -647,7 +677,7 @@ describe.each([
647677
);
648678
}
649679

650-
if (pgArchivedRelations) {
680+
if (pgArchivedRelations || fk_children_parents) {
651681
test(
652682
"Includes only archived (with non-archived parents) when EXCLUSIVELY",
653683
check(
@@ -791,7 +821,7 @@ describe.each([
791821
});
792822
});
793823

794-
if (graphileBuildOptions && graphileBuildOptions[pgRelationsAttr]) {
824+
if (pgArchivedRelations) {
795825
describe(pgRelationsAttr, () => {
796826
it(
797827
"Defaults to omitting other_children where parent is archived",
@@ -939,7 +969,7 @@ describe.each([
939969
),
940970
);
941971
it(
942-
"Only ncludes archived other children within relation when explicitly EXCLUSIVELY",
972+
"Only includes archived other children within relation when explicitly EXCLUSIVELY",
943973
check(
944974
/* GraphQL */ `
945975
{
@@ -983,4 +1013,30 @@ describe.each([
9831013
);
9841014
});
9851015
}
1016+
1017+
if (pgArchivedRelations || fk_children_parents) {
1018+
it(
1019+
"Only includes non-archived children when querying through a different relation",
1020+
check(
1021+
/* GraphQL */ `
1022+
{
1023+
organizationById(id: 3) {
1024+
id
1025+
childrenByOrganizationIdList(includeWhenParentByParentId${Keyword}: INHERIT) {
1026+
id
1027+
}
1028+
}
1029+
}
1030+
`,
1031+
{
1032+
organizationById: {
1033+
id: 3,
1034+
childrenByOrganizationIdList: iderize(
1035+
1001 /* 2001's parent is archived so should be excluded */,
1036+
),
1037+
},
1038+
},
1039+
),
1040+
);
1041+
}
9861042
});

src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ const makeUtils = (
143143
if (
144144
relevantRelation &&
145145
relevantColumn.class !== table &&
146+
relevantRelation.foreignClass === parentTable &&
146147
capableOfInherit &&
147148
queryBuilder.parentQueryBuilder &&
148149
parentColumnDetails &&

0 commit comments

Comments
 (0)