feat: add connectionFilterRelationsRequireIndex option for relation filter index safety#805
Open
pyramation wants to merge 21 commits intomainfrom
Open
feat: add connectionFilterRelationsRequireIndex option for relation filter index safety#805pyramation wants to merge 21 commits intomainfrom
pyramation wants to merge 21 commits intomainfrom
Conversation
Implements a from-scratch PostGraphile v5 native connection filter plugin, replacing the upstream postgraphile-plugin-connection-filter dependency. New package: graphile/graphile-connection-filter/ Plugin architecture (7 plugins): - ConnectionFilterInflectionPlugin: filter type naming conventions - ConnectionFilterTypesPlugin: registers per-table and per-scalar filter types - ConnectionFilterArgPlugin: injects filter arg on connections via applyPlan - ConnectionFilterAttributesPlugin: adds per-column filter fields - ConnectionFilterOperatorsPlugin: standard/sort/pattern/jsonb/inet/array/range operators - ConnectionFilterCustomOperatorsPlugin: addConnectionFilterOperator API for satellite plugins - ConnectionFilterLogicalOperatorsPlugin: and/or/not logical composition Key features: - Full v5 native: uses Grafast planning, PgCondition, codec system, behavior registry - EXPORTABLE pattern for schema caching - Preserves addConnectionFilterOperator API for PostGIS, search, pgvector, textsearch plugins - No relation filter plugins (simplifies configuration vs upstream) - Preset factory: ConnectionFilterPreset(options) Also updates graphile-settings to use the new workspace package.
…Operator filterType is for table-level filter types (UserFilter), while filterFieldType is for scalar operator types (StringFilter). Satellite plugins pass scalar type names, so the lookup must use filterFieldType to match the registration in ConnectionFilterTypesPlugin. Previously worked by coincidence since both inflections produce the same output, but would silently fail if a consumer overrode one inflection but not the other.
Adds computed column filter support — allows filtering on PostgreSQL functions that take a table row as their first argument and return a scalar. Controlled by connectionFilterComputedColumns schema option. The preset factory includes the plugin only when the option is truthy (default in preset: true, but constructive-preset sets it to false).
- Remove phantom postgraphile-plugin-connection-filter dep from graphile-pgvector-plugin (never used) - Remove phantom postgraphile-plugin-connection-filter dep from graphile-pg-textsearch-plugin (never used) - Update graphile-plugin-connection-filter-postgis to use graphile-connection-filter workspace dep with typed imports - Update graphile-search-plugin to use graphile-connection-filter workspace dep with typed imports - Replace (build as any).addConnectionFilterOperator casts with properly typed build.addConnectionFilterOperator
…on-filter - Update search plugin, pgvector, and postgis test files to import from graphile-connection-filter instead of postgraphile-plugin-connection-filter - Use ConnectionFilterPreset() factory instead of PostGraphileConnectionFilterPreset - Import ConnectionFilterOperatorSpec type from graphile-connection-filter - Fix smart quote characters in filter descriptions to match existing snapshots
…ion filter tests - Add graphile-connection-filter as devDependency in graphile-pgvector-plugin (test file imports ConnectionFilterPreset but package had no dependency) - Skip connectionFilterRelations tests in search plugin (relation filters are intentionally not included in the v5-native plugin; they were disabled in production via disablePlugins with the old plugin)
…toggle - ConnectionFilterForwardRelationsPlugin: filter by FK parent relations - ConnectionFilterBackwardRelationsPlugin: filter by backward relations (one-to-one + one-to-many with some/every/none) - connectionFilterRelations toggle in preset (default: false) - Un-skip relation filter tests in search plugin - Updated augmentations, types, and exports
… at runtime The preset factory now always includes relation plugins in the plugin list. Each plugin checks build.options.connectionFilterRelations at runtime and early-returns if disabled. This allows the toggle to be set by any preset in the chain, not just the ConnectionFilterPreset() call.
Enables relation filter fields in the production schema: - Forward: filter by FK parent (e.g. clientByClientId on OrderFilter) - Backward: filter by children with some/every/none - Codegen will pick up the new filter fields automatically
- Search plugin: isPgCondition → isPgConnectionFilter scope
- BM25 plugin: isPgCondition → isPgConnectionFilter scope
- Disable PgConditionArgumentPlugin and PgConditionCustomFieldsPlugin in preset
- Update all tests from condition: {...} to filter: {...}
- Add graphile-connection-filter devDependency to BM25 plugin
- Update search plugin graceful degradation tests to use filter
BREAKING CHANGE: The condition argument has been removed entirely.
All filtering now uses the filter argument exclusively.
- Search plugin plugin.test.ts: condition → filter syntax, add ConnectionFilterPreset - Server-test: condition → filter in query with equalTo operator - Clear stale snapshots (schema-snapshot, introspection) for regeneration
- Search plugin: update snapshot keys to match renamed filter-based tests - Schema snapshot: remove all condition arguments and XxxCondition input types - Introspection snapshot: remove condition arg and UserCondition type - Kept conditionType in _meta schema (unrelated to deprecated condition arg)
… behavior for pgCodecRelation, update schema snapshot with relation filter types
… types to schema snapshot
…, and proper type ordering
…y filter at applyPlan level
Top-level empty filter {} is now treated as 'no filter' (skipped) instead of
throwing an error. Nested empty objects in and/or/not and relation filters are
still rejected. This removes the need for the connectionFilterAllowEmptyObjectInput
workaround in pgvector tests.
- Extract shared getQueryBuilder utility into graphile-connection-filter/src/utils.ts - Remove duplicate getQueryBuilder from search, BM25, and pgvector plugins - Replace (build as any).dataplanPg with build.dataplanPg (already typed on Build) - Replace (build as any).behavior with build.behavior (already typed on Build) - Replace (build as any).input.pgRegistry with build.input.pgRegistry (already typed) - Remove scope destructuring as any casts (pgCodec already typed on ScopeInputObject) - Add pgCodec comment to augmentations.ts noting it's already declared by graphile-build-pg - Export getQueryBuilder from graphile-connection-filter for satellite plugin use
Adds index safety check for relation filter fields. When enabled (default: true), relation filter fields are only created for FKs with supporting indexes. This prevents generating EXISTS subqueries that would cause sequential scans on large tables. Uses PgIndexBehaviorsPlugin's existing relation.extensions.isIndexed metadata which is set at gather time. The check runs at schema build time with zero runtime cost. Applied to both forward and backward relation filter plugins.
Contributor
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
This was referenced Mar 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
feat: add connectionFilterRelationsRequireIndex option
Summary
Adds a
connectionFilterRelationsRequireIndexschema option (default:true) to the connection filter plugin. When enabled, relation filter fields are only generated for foreign key relationships that have supporting indexes on the FK columns. This prevents the plugin from generatingEXISTSsubqueries that would cause sequential scans on large tables.The implementation leverages existing metadata from PostGraphile's
PgIndexBehaviorsPlugin, which setsrelation.extensions.isIndexed = falseduring the gather phase on relations without supporting indexes. Our check runs at schema build time with zero runtime cost — it's a simple property lookup against data already in memory.The check is applied in three places:
GraphQLInputObjectType_fieldshook) — less common since referenced PKs are always indexed, but included for consistencyinithook) — prevents registration of "many" filter types for unindexed relationsGraphQLInputObjectType_fieldshook) — prevents adding filter fields for unindexed backward relationsReview & Testing Checklist for Human
Verify
relation.extensions?.isIndexed === falsematches PgIndexBehaviorsPlugin behavior. The check uses strict=== false(not just falsy). Confirm in PgIndexBehaviorsPlugin source that the property is indeed set tofalse(notundefinedornull) for unindexed relations. This is the core correctness invariant.Test the silent skip behavior on a real schema with unindexed FKs. Create a test table with an unindexed FK (e.g.,
orders.client_idwithout an index), introspect the schema, and verify that the relation filter field (clientByClientIdonOrderFilter) is correctly omitted whenconnectionFilterRelationsRequireIndex: trueand present whenconnectionFilterRelationsRequireIndex: false.Verify CI passes (40/40 checks). The existing tests should pass because test databases typically have indexes on all FK columns. But this also means the tests don't validate the skip behavior when indexes are missing.
Check that the default
truevalue is intentional. Defaulting totruemeans relation filters are more restrictive by default (safer for performance). If you want more permissive defaults for development/testing, considerfalseor making it explicit in presets.Test Plan
Manually create an unindexed FK in your test database:
Introspect and check the generated schema:
connectionFilterRelationsRequireIndex: true(default):OrderFiltershould NOT have aclientByClientIdUnindexedfieldconnectionFilterRelationsRequireIndex: false:OrderFilterSHOULD have aclientByClientIdUnindexedfieldVerify that indexed FKs still work normally in both modes.
Notes
Silent skip behavior: The current implementation silently omits relation filter fields when indexes are missing. There's no console warning or build-time notification. This is consistent with how PostGraphile's
PgIndexBehaviorsPluginremoves behaviors silently, but it could surprise users. Consider adding a debug-level log when a relation is skipped, or documenting this clearly in the README.Forward relation check: The check is applied to forward relations "for consistency" even though the referenced PK is always indexed. In practice,
relation.extensions.isIndexedwould rarely befalsefor forward relations. If this causes unexpected behavior (e.g., a forward relation being skipped incorrectly), the forward relation check could be removed since the performance concern is primarily with backward relations.