@@ -7,9 +7,75 @@ import type {
77} from "@dataplan/pg" ;
88import { makeAssertAllowed } from "./utils" ;
99import type { GraphQLInputObjectType } from "graphql" ;
10+ import { EXPORTABLE } from "./EXPORTABLE" ;
11+ import type { SQL } from "pg-sql2" ;
1012
1113import { version } from "./version" ;
1214
15+ const pgConnectionFilterApplySingleRelation = EXPORTABLE (
16+ ( ) =>
17+ (
18+ assertAllowed : ReturnType < typeof makeAssertAllowed > ,
19+ foreignTable : PgResource ,
20+ foreignTableExpression : SQL ,
21+ localAttributes : string [ ] ,
22+ remoteAttributes : string [ ] ,
23+ sql : GraphileBuild . Build [ "sql" ] ,
24+ $where : PgCondition ,
25+ value : object | null
26+ ) => {
27+ assertAllowed ( value , "object" ) ;
28+ if ( value == null ) return ;
29+ const $subQuery = $where . existsPlan ( {
30+ tableExpression : foreignTableExpression ,
31+ alias : foreignTable . name ,
32+ } ) ;
33+ localAttributes . forEach ( ( localAttribute , i ) => {
34+ const remoteAttribute = remoteAttributes [ i ] ;
35+ $subQuery . where (
36+ sql `${ $where . alias } .${ sql . identifier ( localAttribute as string ) } = ${
37+ $subQuery . alias
38+ } .${ sql . identifier ( remoteAttribute as string ) } `
39+ ) ;
40+ } ) ;
41+ return $subQuery ;
42+ } ,
43+ [ ] ,
44+ "pgConnectionFilterApplySingleRelation"
45+ ) ;
46+
47+ const pgConnectionFilterApplyForwardRelationExists = EXPORTABLE (
48+ ( ) =>
49+ (
50+ assertAllowed : ReturnType < typeof makeAssertAllowed > ,
51+ foreignTable : PgResource ,
52+ foreignTableExpression : SQL ,
53+ localAttributes : string [ ] ,
54+ remoteAttributes : string [ ] ,
55+ sql : GraphileBuild . Build [ "sql" ] ,
56+ $where : PgCondition ,
57+ value : boolean | null
58+ ) => {
59+ assertAllowed ( value , "scalar" ) ;
60+ if ( value == null ) return ;
61+ const $subQuery = $where . existsPlan ( {
62+ tableExpression : foreignTableExpression ,
63+ alias : foreignTable . name ,
64+ equals : value ,
65+ } ) ;
66+ localAttributes . forEach ( ( localAttribute , i ) => {
67+ const remoteAttribute = remoteAttributes [ i ] ;
68+ $subQuery . where (
69+ sql `${ $where . alias } .${ sql . identifier ( localAttribute as string ) } = ${
70+ $subQuery . alias
71+ } .${ sql . identifier ( remoteAttribute as string ) } `
72+ ) ;
73+ } ) ;
74+ } ,
75+ [ ] ,
76+ "pgConnectionFilterApplyForwardRelationExists"
77+ ) ;
78+
1379export const PgConnectionArgFilterForwardRelationsPlugin : GraphileConfig . Plugin =
1480 {
1581 name : "PgConnectionArgFilterForwardRelationsPlugin" ,
@@ -132,23 +198,16 @@ export const PgConnectionArgFilterForwardRelationsPlugin: GraphileConfig.Plugin
132198 sql
133199 ) =>
134200 function ( $where : PgCondition , value : object | null ) {
135- assertAllowed ( value , "object" ) ;
136- if ( value == null ) return ;
137- const $subQuery = $where . existsPlan ( {
138- tableExpression : foreignTableExpression ,
139- alias : foreignTable . name ,
140- } ) ;
141- localAttributes . forEach ( ( localAttribute , i ) => {
142- const remoteAttribute = remoteAttributes [ i ] ;
143- $subQuery . where (
144- sql `${ $where . alias } .${ sql . identifier (
145- localAttribute as string
146- ) } = ${ $subQuery . alias } .${ sql . identifier (
147- remoteAttribute as string
148- ) } `
149- ) ;
150- } ) ;
151- return $subQuery ;
201+ return pgConnectionFilterApplySingleRelation (
202+ assertAllowed ,
203+ foreignTable ,
204+ foreignTableExpression ,
205+ localAttributes ,
206+ remoteAttributes ,
207+ sql ,
208+ $where ,
209+ value
210+ ) ;
152211 } ,
153212 [
154213 assertAllowed ,
@@ -196,23 +255,16 @@ export const PgConnectionArgFilterForwardRelationsPlugin: GraphileConfig.Plugin
196255 $where : PgCondition ,
197256 value : boolean | null
198257 ) {
199- assertAllowed ( value , "scalar" ) ;
200- if ( value == null ) return ;
201- const $subQuery = $where . existsPlan ( {
202- tableExpression : foreignTableExpression ,
203- alias : foreignTable . name ,
204- equals : value ,
205- } ) ;
206- localAttributes . forEach ( ( localAttribute , i ) => {
207- const remoteAttribute = remoteAttributes [ i ] ;
208- $subQuery . where (
209- sql `${ $where . alias } .${ sql . identifier (
210- localAttribute as string
211- ) } = ${ $subQuery . alias } .${ sql . identifier (
212- remoteAttribute as string
213- ) } `
214- ) ;
215- } ) ;
258+ return pgConnectionFilterApplyForwardRelationExists (
259+ assertAllowed ,
260+ foreignTable ,
261+ foreignTableExpression ,
262+ localAttributes ,
263+ remoteAttributes ,
264+ sql ,
265+ $where ,
266+ value
267+ ) ;
216268 } ,
217269 [
218270 assertAllowed ,
0 commit comments