diff --git a/.changeset/long-bears-kiss.md b/.changeset/long-bears-kiss.md new file mode 100644 index 0000000000..318224041d --- /dev/null +++ b/.changeset/long-bears-kiss.md @@ -0,0 +1,7 @@ +--- +"@dataplan/pg": patch +--- + +Add new `PgCondition::ignoreUnlessAmended()` method to have PgCondition +modifiers only apply if additional conditions are added. Needed to fix a +regression in postgraphile-plugin-connection-filter diff --git a/grafast/dataplan-pg/src/steps/pgCondition.ts b/grafast/dataplan-pg/src/steps/pgCondition.ts index 3a40ea744b..1981f6d924 100644 --- a/grafast/dataplan-pg/src/steps/pgCondition.ts +++ b/grafast/dataplan-pg/src/steps/pgCondition.ts @@ -66,6 +66,7 @@ export class PgCondition< private conditions: PgWhereConditionSpec[] = []; private havingConditions: PgHavingConditionSpec[] = []; + private minimumConditionsLength = 0; public readonly alias: SQL; @@ -102,6 +103,17 @@ export class PgCondition< } } + /** + * Indicates that additional conditions must be added for this filter to take + * place. Exists specifically so that postgraphile-plugin-connection-filter + * can return a condition for children to use, but then not actually add that + * condition if no children add any requirements to it. + */ + public ignoreUnlessAmended() { + this.minimumConditionsLength = + (this.isHaving ? this.havingConditions : this.conditions).length + 1; + } + /** * Manipulating an ancestor may have unintended consequences. Please exercise * extreme caution, and think through all possible side effects. In @@ -199,6 +211,10 @@ where ${sqlCondition}`})`; apply(): void { if (this.isHaving) { + if (this.havingConditions.length < this.minimumConditionsLength) { + // Do nothing + return; + } if (!this.parent.having) { throw new Error(`${this.parent} doesn't support 'having'`); } @@ -213,6 +229,10 @@ where ${sqlCondition}`})`; } } } else { + if (this.conditions.length < this.minimumConditionsLength) { + // Do nothing + return; + } if (this.resolvedMode.mode === "PASS_THRU") { this.conditions.forEach((condition) => { this.parent.where(condition);