@@ -20,6 +20,14 @@ const {
20
20
const SQL = `
21
21
drop schema if exists omit_archived cascade;
22
22
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
+ );
23
31
create table omit_archived.parents (
24
32
id int primary key,
25
33
name text,
@@ -30,26 +38,30 @@ create table omit_archived.parents (
30
38
);
31
39
create table omit_archived.children (
32
40
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,
34
43
name text,
35
44
is_archived boolean not null default false,
36
45
archived_at timestamptz default null,
37
46
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
39
49
);
40
50
create index on omit_archived.children(parent_id);
41
51
create table omit_archived.other_children (
42
52
id int primary key,
43
53
parent_id int not null references omit_archived.parents,
44
54
title text
45
55
);
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);
46
58
insert into omit_archived.parents (id, name, is_archived, archived_at, is_published, published_at)
47
59
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);
53
65
insert into omit_archived.other_children (id, parent_id, title) values
54
66
(101, 1, 'First other child 1'),
55
67
(102, 1, 'First other child 2'),
@@ -67,11 +79,18 @@ beforeAll(() => {
67
79
connectionString : process . env . TEST_DATABASE_URL || "pggql_test" ,
68
80
} ) ;
69
81
} ) ;
70
- beforeAll ( ( ) => pgPool . query ( SQL ) ) ;
71
82
afterAll ( ( ) => pgPool . end ( ) ) ;
72
83
73
84
describe . each ( [
74
85
[ "default" ] ,
86
+ [
87
+ "default_w_comment" ,
88
+ undefined ,
89
+ undefined ,
90
+ {
91
+ fk_children_parents : true ,
92
+ } ,
93
+ ] ,
75
94
[
76
95
"is_archived" ,
77
96
"archived" ,
@@ -100,7 +119,8 @@ describe.each([
100
119
pgDraftRelations : true ,
101
120
} ,
102
121
] ,
103
- ] ) ( "%s" , ( _columnName , keyword , graphileBuildOptions ) => {
122
+ ] ) ( "%s" , ( _columnName , keyword , graphileBuildOptions , config = { } ) => {
123
+ const { fk_children_parents = null } = config ;
104
124
const Keyword = keyword
105
125
? keyword [ 0 ] . toUpperCase ( ) + keyword . slice ( 1 )
106
126
: `Archived` ;
@@ -117,6 +137,16 @@ describe.each([
117
137
? graphileBuildOptions [ pgRelationsAttr ] || false
118
138
: false ;
119
139
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
120
150
schema = await createPostGraphileSchema ( pgPool , [ "omit_archived" ] , options ) ;
121
151
} ) ;
122
152
@@ -209,7 +239,7 @@ describe.each([
209
239
} ) ;
210
240
211
241
describe ( "children" , ( ) => {
212
- if ( pgArchivedRelations ) {
242
+ if ( pgArchivedRelations || fk_children_parents ) {
213
243
test (
214
244
"Omits archived children (and those with archived parents) by default" ,
215
245
check (
@@ -239,7 +269,7 @@ describe.each([
239
269
) ;
240
270
}
241
271
242
- if ( pgArchivedRelations ) {
272
+ if ( pgArchivedRelations || fk_children_parents ) {
243
273
test (
244
274
"Omits archived children (and those with archived parents) when NO" ,
245
275
check (
@@ -269,7 +299,7 @@ describe.each([
269
299
) ;
270
300
}
271
301
272
- if ( pgArchivedRelations ) {
302
+ if ( pgArchivedRelations || fk_children_parents ) {
273
303
test (
274
304
"Includes everything (except those with archived parents) when YES" ,
275
305
check (
@@ -324,7 +354,7 @@ describe.each([
324
354
) ;
325
355
}
326
356
327
- if ( pgArchivedRelations ) {
357
+ if ( pgArchivedRelations || fk_children_parents ) {
328
358
test (
329
359
"Includes only archived (with non-archived parents) when EXCLUSIVELY" ,
330
360
check (
@@ -552,7 +582,7 @@ describe.each([
552
582
} ) ;
553
583
554
584
describe ( "children" , ( ) => {
555
- if ( pgArchivedRelations ) {
585
+ if ( pgArchivedRelations || fk_children_parents ) {
556
586
test (
557
587
"Omits archived children (and those with archived parents) by default" ,
558
588
check (
@@ -578,7 +608,7 @@ describe.each([
578
608
) ;
579
609
}
580
610
581
- if ( pgArchivedRelations ) {
611
+ if ( pgArchivedRelations || fk_children_parents ) {
582
612
test (
583
613
"Omits archived children (and those with archived parents) when NO" ,
584
614
check (
@@ -604,7 +634,7 @@ describe.each([
604
634
) ;
605
635
}
606
636
607
- if ( pgArchivedRelations ) {
637
+ if ( pgArchivedRelations || fk_children_parents ) {
608
638
test (
609
639
"Includes everything (except those with archived parents) when YES" ,
610
640
check (
@@ -647,7 +677,7 @@ describe.each([
647
677
) ;
648
678
}
649
679
650
- if ( pgArchivedRelations ) {
680
+ if ( pgArchivedRelations || fk_children_parents ) {
651
681
test (
652
682
"Includes only archived (with non-archived parents) when EXCLUSIVELY" ,
653
683
check (
@@ -791,7 +821,7 @@ describe.each([
791
821
} ) ;
792
822
} ) ;
793
823
794
- if ( graphileBuildOptions && graphileBuildOptions [ pgRelationsAttr ] ) {
824
+ if ( pgArchivedRelations ) {
795
825
describe ( pgRelationsAttr , ( ) => {
796
826
it (
797
827
"Defaults to omitting other_children where parent is archived" ,
@@ -939,7 +969,7 @@ describe.each([
939
969
) ,
940
970
) ;
941
971
it (
942
- "Only ncludes archived other children within relation when explicitly EXCLUSIVELY" ,
972
+ "Only includes archived other children within relation when explicitly EXCLUSIVELY" ,
943
973
check (
944
974
/* GraphQL */ `
945
975
{
@@ -983,4 +1013,30 @@ describe.each([
983
1013
) ;
984
1014
} ) ;
985
1015
}
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
+ }
986
1042
} ) ;
0 commit comments