Skip to content

Fix: GridifyQueryBuilder.build() is not idempotent#320

Merged
alirezanet merged 7 commits intomasterfrom
copilot/fix-query-builder-idempotency
Mar 7, 2026
Merged

Fix: GridifyQueryBuilder.build() is not idempotent#320
alirezanet merged 7 commits intomasterfrom
copilot/fix-query-builder-idempotency

Conversation

Copy link
Contributor

Copilot AI commented Mar 7, 2026

build() accumulated filter expressions directly into this.query.filter via +=. Calling it more than once appended all filter expressions again on top of the previously built result, producing duplicate filters.

Root Cause

// Before: mutates shared state on every call
this.query.filter += exp.value;

On the second build() call, this.query.filter already held the first result, so all expressions were appended again.

Fix

Build the filter into a local variable; assign to this.query.filter only once at the end of build().

// After: rebuilds from scratch each call
let filter = "";
// ...
filter += exp.value;
// ...
this.query.filter = filter.replace(/[,|]?\(\)/gi, "");

This makes build() a pure projection from filteringExpressions → query string with no cumulative side effects.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist

  • I have performed a self-review of my code
  • I have added tests that prove my fix is effective or that my feature works
  • I have made corresponding changes to the documentation
  • I have commented my code, particularly in hard-to-understand areas
  • New and existing unit tests pass locally with my changes
Original prompt

This section details on the original issue you should resolve

<issue_title>[Potential] TS Query builder is not Idempotent; Calling build() multiple times does not yield the same result</issue_title>
<issue_description>### Version

=2.1.0

Details

If you create a query twice from the same class instance in the ts gridify client, filters such as "filter name by contains abc" are created twice.

E.g.

  queryOptions: (queryBuilder) => {
                return {
                    queryKey: ['demo', queryBuilder.build()], // all fine here
                    queryFn: () => {
                        console.log('query', queryBuilder.build()); // all filters are applied twice
                    },
                };
            },

NOTE: Created ticket for my own peace, I'll investigate further when I got the time for it.

Steps to reproduce

Call any instance twice or more and observe subsequent return values.</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Fix TS query builder idempotency issue Fix: GridifyQueryBuilder.build() is not idempotent Mar 7, 2026
@alirezanet alirezanet marked this pull request as ready for review March 7, 2026 17:57
Copy link
Owner

@alirezanet alirezanet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert your last commit. I don't want to ignore the lock files.

Copilot AI and others added 4 commits March 8, 2026 00:03
Co-authored-by: alirezanet <7004080+alirezanet@users.noreply.github.com>
Co-authored-by: alirezanet <7004080+alirezanet@users.noreply.github.com>
Co-authored-by: alirezanet <7004080+alirezanet@users.noreply.github.com>
@alirezanet alirezanet force-pushed the copilot/fix-query-builder-idempotency branch from 7adce05 to 00c3765 Compare March 7, 2026 23:04
@alirezanet alirezanet merged commit 44968b3 into master Mar 7, 2026
7 checks passed
@alirezanet alirezanet deleted the copilot/fix-query-builder-idempotency branch March 7, 2026 23:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Potential] TS Query builder is not Idempotent; Calling build() multiple times does not yield the same result

2 participants