Skip to content

feat: add connectionFilterRelationsRequireIndex option for relation filter index safety#805

Open
pyramation wants to merge 21 commits intomainfrom
devin/1773369815-relation-filter-index-safety
Open

feat: add connectionFilterRelationsRequireIndex option for relation filter index safety#805
pyramation wants to merge 21 commits intomainfrom
devin/1773369815-relation-filter-index-safety

Conversation

@pyramation
Copy link
Contributor

feat: add connectionFilterRelationsRequireIndex option

Summary

Adds a connectionFilterRelationsRequireIndex schema 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 generating EXISTS subqueries that would cause sequential scans on large tables.

The implementation leverages existing metadata from PostGraphile's PgIndexBehaviorsPlugin, which sets relation.extensions.isIndexed = false during 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:

  • Forward relations plugin (GraphQLInputObjectType_fields hook) — less common since referenced PKs are always indexed, but included for consistency
  • Backward relations plugin (init hook) — prevents registration of "many" filter types for unindexed relations
  • Backward relations plugin (GraphQLInputObjectType_fields hook) — prevents adding filter fields for unindexed backward relations

Note: This branch is based on PRs #797 (v5-native connection-filter), #801 (condition deprecation), and #804 (type safety). The cumulative diff includes changes from all of those. The changes specific to this PR are limited to ~38 added lines across 5 files in graphile-connection-filter/src/.

Review & Testing Checklist for Human

  • Verify relation.extensions?.isIndexed === false matches PgIndexBehaviorsPlugin behavior. The check uses strict === false (not just falsy). Confirm in PgIndexBehaviorsPlugin source that the property is indeed set to false (not undefined or null) 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_id without an index), introspect the schema, and verify that the relation filter field (clientByClientId on OrderFilter) is correctly omitted when connectionFilterRelationsRequireIndex: true and present when connectionFilterRelationsRequireIndex: 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 true value is intentional. Defaulting to true means relation filters are more restrictive by default (safer for performance). If you want more permissive defaults for development/testing, consider false or making it explicit in presets.

Test Plan

  1. Manually create an unindexed FK in your test database:

    ALTER TABLE orders ADD COLUMN client_id_unindexed UUID REFERENCES clients(id);
    -- Intentionally don't create an index
  2. Introspect and check the generated schema:

    • With connectionFilterRelationsRequireIndex: true (default): OrderFilter should NOT have a clientByClientIdUnindexed field
    • With connectionFilterRelationsRequireIndex: false: OrderFilter SHOULD have a clientByClientIdUnindexed field
  3. Verify 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 PgIndexBehaviorsPlugin removes 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.isIndexed would rarely be false for 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.

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
…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.
@devin-ai-integration
Copy link
Contributor

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

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.

1 participant