11import type {
2- PgConditionStep ,
2+ PgCondition ,
33 PgCodecRelation ,
44 PgCodecWithAttributes ,
55 PgRegistry ,
66 PgResource ,
7+ PgConditionCapableParent ,
78} from "@dataplan/pg" ;
89import { makeAssertAllowed } from "./utils" ;
10+ import { GraphQLInputObjectType } from "graphql" ;
911
1012const { version } = require ( "../package.json" ) ;
1113
@@ -319,7 +321,7 @@ export const PgConnectionArgFilterBackwardRelationsPlugin: GraphileConfig.Plugin
319321 inflection . filterType ( foreignTableTypeName ) ;
320322 const ForeignTableFilterType = build . getTypeByName (
321323 foreignTableFilterTypeName
322- ) ;
324+ ) as GraphQLInputObjectType ;
323325 if ( ! ForeignTableFilterType ) continue ;
324326
325327 if ( typeof foreignTable . from === "function" ) {
@@ -338,8 +340,14 @@ export const PgConnectionArgFilterBackwardRelationsPlugin: GraphileConfig.Plugin
338340 source . codec ,
339341 foreignTable
340342 ) ;
341- const FilterManyType =
342- build . getTypeByName ( filterManyTypeName ) ;
343+ const FilterManyType = build . getTypeByName (
344+ filterManyTypeName
345+ ) as GraphQLInputObjectType ;
346+ if ( ! FilterManyType ) {
347+ throw new Error (
348+ `Failed to retrieve type '${ filterManyTypeName } '`
349+ ) ;
350+ }
343351 // TODO: revisit using `_` prefixed inflector
344352 const fieldName = inflection . _manyRelation ( {
345353 registry : source . registry ,
@@ -360,7 +368,7 @@ export const PgConnectionArgFilterBackwardRelationsPlugin: GraphileConfig.Plugin
360368 ( ) => ( {
361369 description : `Filter by the object’s \`${ fieldName } \` relation.` ,
362370 type : FilterManyType ,
363- applyPlan : EXPORTABLE (
371+ apply : EXPORTABLE (
364372 (
365373 assertAllowed ,
366374 foreignTable ,
@@ -369,10 +377,10 @@ export const PgConnectionArgFilterBackwardRelationsPlugin: GraphileConfig.Plugin
369377 remoteAttributes
370378 ) =>
371379 function (
372- $where : PgConditionStep < any > ,
373- fieldArgs
380+ $where : PgCondition ,
381+ value : object | null
374382 ) {
375- assertAllowed ( fieldArgs , "object" ) ;
383+ assertAllowed ( value , "object" ) ;
376384 // $where.alias represents source; we need a condition that references the relational target
377385 const $rel = $where . andPlan ( ) ;
378386 $rel . extensions . pgFilterRelation = {
@@ -381,7 +389,7 @@ export const PgConnectionArgFilterBackwardRelationsPlugin: GraphileConfig.Plugin
381389 localAttributes,
382390 remoteAttributes,
383391 } ;
384- fieldArgs . apply ( $rel ) ;
392+ return $rel ;
385393 } ,
386394 [
387395 assertAllowed ,
@@ -417,7 +425,7 @@ export const PgConnectionArgFilterBackwardRelationsPlugin: GraphileConfig.Plugin
417425 // and in PgConnectionArgFilterForwardRelationsPlugin
418426 // are very very similar. We should extract them to a
419427 // helper function.
420- applyPlan : EXPORTABLE (
428+ apply : EXPORTABLE (
421429 (
422430 assertAllowed ,
423431 foreignTable ,
@@ -427,14 +435,15 @@ export const PgConnectionArgFilterBackwardRelationsPlugin: GraphileConfig.Plugin
427435 sql
428436 ) =>
429437 function (
430- $where : PgConditionStep < any > ,
431- fieldArgs
438+ $where : PgCondition ,
439+ value : boolean | null
432440 ) {
433- assertAllowed ( fieldArgs , "scalar" ) ;
441+ assertAllowed ( value , "scalar" ) ;
442+ if ( value == null ) return ;
434443 const $subQuery = $where . existsPlan ( {
435444 tableExpression : foreignTableExpression ,
436445 alias : foreignTable . name ,
437- $ equals : fieldArgs . get ( ) ,
446+ equals : value as boolean ,
438447 } ) ;
439448 localAttributes . forEach ( ( localAttribute , i ) => {
440449 const remoteAttribute = remoteAttributes [ i ] ;
@@ -483,7 +492,7 @@ export const PgConnectionArgFilterBackwardRelationsPlugin: GraphileConfig.Plugin
483492 ( ) => ( {
484493 description : `Filter by the object’s \`${ fieldName } \` relation.` ,
485494 type : ForeignTableFilterType ,
486- applyPlan : EXPORTABLE (
495+ apply : EXPORTABLE (
487496 (
488497 assertAllowed ,
489498 foreignTable ,
@@ -492,8 +501,11 @@ export const PgConnectionArgFilterBackwardRelationsPlugin: GraphileConfig.Plugin
492501 remoteAttributes ,
493502 sql
494503 ) =>
495- function ( $where : PgConditionStep < any > , fieldArgs ) {
496- assertAllowed ( fieldArgs , "object" ) ;
504+ function (
505+ $where : PgCondition ,
506+ value : object | null
507+ ) {
508+ assertAllowed ( value , "object" ) ;
497509 const $subQuery = $where . existsPlan ( {
498510 tableExpression : foreignTableExpression ,
499511 alias : foreignTable . name ,
@@ -508,7 +520,7 @@ export const PgConnectionArgFilterBackwardRelationsPlugin: GraphileConfig.Plugin
508520 ) } `
509521 ) ;
510522 } ) ;
511- fieldArgs . apply ( $subQuery ) ;
523+ return $subQuery ;
512524 } ,
513525 [
514526 assertAllowed ,
@@ -541,7 +553,7 @@ export const PgConnectionArgFilterBackwardRelationsPlugin: GraphileConfig.Plugin
541553 ( ) => ( {
542554 description : `A related \`${ fieldName } \` exists.` ,
543555 type : GraphQLBoolean ,
544- applyPlan : EXPORTABLE (
556+ apply : EXPORTABLE (
545557 (
546558 assertAllowed ,
547559 foreignTable ,
@@ -551,14 +563,15 @@ export const PgConnectionArgFilterBackwardRelationsPlugin: GraphileConfig.Plugin
551563 sql
552564 ) =>
553565 function (
554- $where : PgConditionStep < any > ,
555- fieldArgs
566+ $where : PgCondition ,
567+ value : boolean | null
556568 ) {
557- assertAllowed ( fieldArgs , "scalar" ) ;
569+ assertAllowed ( value , "scalar" ) ;
570+ if ( value == null ) return ;
558571 const $subQuery = $where . existsPlan ( {
559572 tableExpression : foreignTableExpression ,
560573 alias : foreignTable . name ,
561- $ equals : fieldArgs . get ( ) ,
574+ equals : value ,
562575 } ) ;
563576 localAttributes . forEach ( ( localAttribute , i ) => {
564577 const remoteAttribute = remoteAttributes [ i ] ;
@@ -596,7 +609,14 @@ export const PgConnectionArgFilterBackwardRelationsPlugin: GraphileConfig.Plugin
596609 ) ;
597610 const foreignTableFilterTypeName =
598611 inflection . filterType ( foreignTableTypeName ) ;
599- const FilterType = build . getTypeByName ( foreignTableFilterTypeName ) ;
612+ const FilterType = build . getTypeByName (
613+ foreignTableFilterTypeName
614+ ) as GraphQLInputObjectType ;
615+ if ( ! FilterType ) {
616+ throw new Error (
617+ `Failed to load type ${ foreignTableFilterTypeName } `
618+ ) ;
619+ }
600620
601621 const manyFields = {
602622 every : fieldWithHooks (
@@ -607,10 +627,14 @@ export const PgConnectionArgFilterBackwardRelationsPlugin: GraphileConfig.Plugin
607627 ( ) => ( {
608628 description : `Every related \`${ foreignTableTypeName } \` matches the filter criteria. All fields are combined with a logical ‘and.’` ,
609629 type : FilterType ,
610- applyPlan : EXPORTABLE (
630+ apply : EXPORTABLE (
611631 ( assertAllowed , sql ) =>
612- function ( $where : PgConditionStep < any > , fieldArgs ) {
613- assertAllowed ( fieldArgs , "object" ) ;
632+ function (
633+ $where : PgCondition < any > ,
634+ value : object | null
635+ ) {
636+ assertAllowed ( value , "object" ) ;
637+ if ( value == null ) return ;
614638 if ( ! $where . extensions . pgFilterRelation ) {
615639 throw new Error (
616640 `Invalid use of filter, 'pgFilterRelation' expected`
@@ -636,7 +660,7 @@ export const PgConnectionArgFilterBackwardRelationsPlugin: GraphileConfig.Plugin
636660 ) } `
637661 ) ;
638662 } ) ;
639- fieldArgs . apply ( $subQuery . notPlan ( ) . andPlan ( ) ) ;
663+ return $subQuery . notPlan ( ) . andPlan ( ) ;
640664 } ,
641665 [ assertAllowed , sql ]
642666 ) ,
@@ -650,10 +674,11 @@ export const PgConnectionArgFilterBackwardRelationsPlugin: GraphileConfig.Plugin
650674 ( ) => ( {
651675 description : `Some related \`${ foreignTableTypeName } \` matches the filter criteria. All fields are combined with a logical ‘and.’` ,
652676 type : FilterType ,
653- applyPlan : EXPORTABLE (
677+ apply : EXPORTABLE (
654678 ( assertAllowed , sql ) =>
655- function ( $where : PgConditionStep < any > , fieldArgs ) {
656- assertAllowed ( fieldArgs , "object" ) ;
679+ function ( $where : PgCondition , value : object | null ) {
680+ assertAllowed ( value , "object" ) ;
681+ if ( value == null ) return ;
657682 if ( ! $where . extensions . pgFilterRelation ) {
658683 throw new Error (
659684 `Invalid use of filter, 'pgFilterRelation' expected`
@@ -679,7 +704,7 @@ export const PgConnectionArgFilterBackwardRelationsPlugin: GraphileConfig.Plugin
679704 ) } `
680705 ) ;
681706 } ) ;
682- fieldArgs . apply ( $subQuery ) ;
707+ return $subQuery ;
683708 } ,
684709 [ assertAllowed , sql ]
685710 ) ,
@@ -693,10 +718,11 @@ export const PgConnectionArgFilterBackwardRelationsPlugin: GraphileConfig.Plugin
693718 ( ) => ( {
694719 description : `No related \`${ foreignTableTypeName } \` matches the filter criteria. All fields are combined with a logical ‘and.’` ,
695720 type : FilterType ,
696- applyPlan : EXPORTABLE (
721+ apply : EXPORTABLE (
697722 ( assertAllowed , sql ) =>
698- function ( $where : PgConditionStep < any > , fieldArgs ) {
699- assertAllowed ( fieldArgs , "object" ) ;
723+ function ( $where : PgCondition , value : object | null ) {
724+ assertAllowed ( value , "object" ) ;
725+ if ( value == null ) return ;
700726 if ( ! $where . extensions . pgFilterRelation ) {
701727 throw new Error (
702728 `Invalid use of filter, 'pgFilterRelation' expected`
@@ -722,7 +748,7 @@ export const PgConnectionArgFilterBackwardRelationsPlugin: GraphileConfig.Plugin
722748 ) } `
723749 ) ;
724750 } ) ;
725- fieldArgs . apply ( $subQuery ) ;
751+ return $subQuery ;
726752 } ,
727753 [ assertAllowed , sql ]
728754 ) ,
0 commit comments