Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/long-bears-kiss.md
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions grafast/dataplan-pg/src/steps/pgCondition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export class PgCondition<

private conditions: PgWhereConditionSpec<any>[] = [];
private havingConditions: PgHavingConditionSpec<any>[] = [];
private minimumConditionsLength = 0;

public readonly alias: SQL;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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'`);
}
Expand All @@ -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);
Expand Down
Loading