Skip to content

Commit f28eaf2

Browse files
committed
fix: enable orderBy on all columns alongside filterBy in v5 plugin
1 parent 2f6e083 commit f28eaf2

1 file changed

Lines changed: 17 additions & 15 deletions

File tree

graphile/graphile-settings/src/plugins/enable-all-filter-columns.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import type { GraphileConfig } from 'graphile-config';
22

33
/**
4-
* EnableAllFilterColumnsPlugin - Enables filtering on ALL columns, not just indexed ones.
4+
* EnableAllFilterColumnsPlugin - Enables filtering and ordering on ALL columns, not just indexed ones.
55
*
66
* WHY THIS EXISTS:
7-
* PostGraphile v5's `PgIndexBehaviorsPlugin` restricts filtering to only indexed columns
8-
* by default. This is a performance optimization - filtering on non-indexed columns can
7+
* PostGraphile v5's `PgIndexBehaviorsPlugin` restricts filtering and ordering to only indexed columns
8+
* by default. This is a performance optimization - filtering/ordering on non-indexed columns can
99
* cause slow table scans. However, for development and flexibility, we want to allow
10-
* filtering on all columns and let developers/DBAs decide which columns need indexes.
10+
* filtering and ordering on all columns and let developers/DBAs decide which columns need indexes.
1111
*
1212
* SOURCE CODE REFERENCE:
1313
* PgIndexBehaviorsPlugin marks non-indexed columns with `extensions.isIndexed = false`
14-
* and then adds `-filterBy` behavior to remove them from filters:
14+
* and then adds `-filterBy` and `-orderBy` behaviors to remove them from filters and ordering:
1515
* https://github.com/graphile/crystal/blob/924b2515c6bd30e5905ac1419a25244b40c8bb4d/graphile-build/graphile-build-pg/src/plugins/PgIndexBehaviorsPlugin.ts
1616
*
1717
* The relevant v5 code (from PgIndexBehaviorsPlugin):
@@ -25,7 +25,7 @@ import type { GraphileConfig } from 'graphile-config';
2525
* const newBehavior = [behavior];
2626
* const attr = codec.attributes[attributeName];
2727
* if (attr.extensions?.isIndexed === false) {
28-
* newBehavior.push("-filterBy", "-orderBy"); // <-- This removes filterBy!
28+
* newBehavior.push("-filterBy", "-orderBy"); // <-- This removes filterBy AND orderBy!
2929
* }
3030
* return newBehavior;
3131
* },
@@ -36,15 +36,16 @@ import type { GraphileConfig } from 'graphile-config';
3636
*
3737
* OUR FIX:
3838
* We add a behavior callback that runs AFTER PgIndexBehaviorsPlugin's "postInferred" phase
39-
* and adds `+attribute:filterBy` back to ALL columns, regardless of index status.
39+
* and adds `+attribute:filterBy` and `+attribute:orderBy` back to ALL columns, regardless of index status.
4040
*
4141
* This means:
4242
* - All columns will appear in the connection filter's filter argument
43-
* - Developers can filter by any column
44-
* - It's the developer's/DBA's responsibility to add indexes for frequently filtered columns
43+
* - All columns will appear in the connection's orderBy enum
44+
* - Developers can filter and sort by any column
45+
* - It's the developer's/DBA's responsibility to add indexes for frequently filtered/sorted columns
4546
*
4647
* PERFORMANCE WARNING:
47-
* Filtering on non-indexed columns can cause full table scans, which may be slow on large
48+
* Filtering or ordering on non-indexed columns can cause full table scans, which may be slow on large
4849
* tables. Monitor your query performance and add indexes as needed. You can check which
4950
* columns are indexed by querying pg_indexes or using EXPLAIN ANALYZE on your queries.
5051
*
@@ -56,23 +57,24 @@ import type { GraphileConfig } from 'graphile-config';
5657
export const EnableAllFilterColumnsPlugin: GraphileConfig.Plugin = {
5758
name: 'EnableAllFilterColumnsPlugin',
5859
version: '1.0.0',
59-
description: 'Enables filtering on all columns, not just indexed ones',
60+
description: 'Enables filtering and ordering on all columns, not just indexed ones',
6061

6162
schema: {
6263
entityBehavior: {
6364
pgCodecAttribute: {
6465
/**
6566
* This callback runs in the "inferred" phase AFTER PgIndexBehaviorsPlugin's
66-
* "postInferred" phase. It adds `filterBy` back to ALL columns, overriding
67-
* the `-filterBy` that PgIndexBehaviorsPlugin adds to non-indexed columns.
67+
* "postInferred" phase. It adds `filterBy` and `orderBy` back to ALL columns,
68+
* overriding the `-filterBy` and `-orderBy` that PgIndexBehaviorsPlugin adds
69+
* to non-indexed columns.
6870
*/
6971
inferred: {
7072
after: ['postInferred'],
7173
provides: ['enableAllFilters'],
7274
callback(behavior) {
73-
// Add filterBy to override any -filterBy from PgIndexBehaviorsPlugin
75+
// Add filterBy and orderBy to override PgIndexBehaviorsPlugin restrictions
7476
// The behavior system will resolve conflicts, with later additions winning
75-
return [behavior, 'filterBy'];
77+
return [behavior, 'filterBy', 'orderBy'];
7678
},
7779
},
7880
},

0 commit comments

Comments
 (0)