graphile-settings 3.1.0 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +170 -85
- package/esm/index.d.ts +37 -0
- package/esm/index.js +54 -103
- package/esm/plugins/conflict-detector.d.ts +7 -0
- package/esm/plugins/conflict-detector.js +55 -0
- package/esm/plugins/custom-inflector.d.ts +9 -0
- package/esm/plugins/custom-inflector.js +382 -0
- package/esm/plugins/enable-all-filter-columns.d.ts +60 -0
- package/esm/plugins/enable-all-filter-columns.js +85 -0
- package/esm/plugins/index.d.ts +19 -0
- package/esm/plugins/index.js +26 -0
- package/esm/plugins/inflector-logger.d.ts +7 -0
- package/esm/plugins/inflector-logger.js +215 -0
- package/esm/plugins/many-to-many-preset.d.ts +62 -0
- package/esm/plugins/many-to-many-preset.js +86 -0
- package/esm/plugins/meta-schema/cache.d.ts +4 -0
- package/esm/plugins/meta-schema/cache.js +7 -0
- package/esm/plugins/meta-schema/constraint-meta-builders.d.ts +13 -0
- package/esm/plugins/meta-schema/constraint-meta-builders.js +51 -0
- package/esm/plugins/meta-schema/graphql-meta-field.d.ts +4 -0
- package/esm/plugins/meta-schema/graphql-meta-field.js +201 -0
- package/esm/plugins/meta-schema/inflection-utils.d.ts +4 -0
- package/esm/plugins/meta-schema/inflection-utils.js +20 -0
- package/esm/plugins/meta-schema/name-meta-builders.d.ts +4 -0
- package/esm/plugins/meta-schema/name-meta-builders.js +38 -0
- package/esm/plugins/meta-schema/plugin.d.ts +2 -0
- package/esm/plugins/meta-schema/plugin.js +23 -0
- package/esm/plugins/meta-schema/relation-meta-builders.d.ts +8 -0
- package/esm/plugins/meta-schema/relation-meta-builders.js +115 -0
- package/esm/plugins/meta-schema/table-meta-builder.d.ts +2 -0
- package/esm/plugins/meta-schema/table-meta-builder.js +69 -0
- package/esm/plugins/meta-schema/table-meta-context.d.ts +13 -0
- package/esm/plugins/meta-schema/table-meta-context.js +11 -0
- package/esm/plugins/meta-schema/table-resource-utils.d.ts +12 -0
- package/esm/plugins/meta-schema/table-resource-utils.js +50 -0
- package/esm/plugins/meta-schema/type-mappings.d.ts +3 -0
- package/esm/plugins/meta-schema/type-mappings.js +75 -0
- package/esm/plugins/meta-schema/types.d.ts +206 -0
- package/esm/plugins/meta-schema/types.js +1 -0
- package/esm/plugins/meta-schema.d.ts +19 -0
- package/esm/plugins/meta-schema.js +20 -0
- package/esm/plugins/minimal-preset.d.ts +7 -0
- package/esm/plugins/minimal-preset.js +42 -0
- package/esm/plugins/pg-type-mappings.d.ts +41 -0
- package/esm/plugins/pg-type-mappings.js +122 -0
- package/esm/plugins/primary-key-only.d.ts +96 -0
- package/esm/plugins/primary-key-only.js +143 -0
- package/esm/presets/constructive-preset.d.ts +40 -0
- package/esm/presets/constructive-preset.js +137 -0
- package/esm/presets/index.d.ts +7 -0
- package/esm/presets/index.js +7 -0
- package/index.d.ts +37 -3
- package/index.js +56 -129
- package/package.json +16 -22
- package/plugins/conflict-detector.d.ts +7 -0
- package/plugins/conflict-detector.js +58 -0
- package/plugins/custom-inflector.d.ts +9 -0
- package/plugins/custom-inflector.js +385 -0
- package/plugins/enable-all-filter-columns.d.ts +60 -0
- package/plugins/enable-all-filter-columns.js +88 -0
- package/plugins/index.d.ts +19 -0
- package/plugins/index.js +56 -0
- package/plugins/inflector-logger.d.ts +7 -0
- package/plugins/inflector-logger.js +218 -0
- package/plugins/many-to-many-preset.d.ts +62 -0
- package/plugins/many-to-many-preset.js +89 -0
- package/plugins/meta-schema/cache.d.ts +4 -0
- package/plugins/meta-schema/cache.js +12 -0
- package/plugins/meta-schema/constraint-meta-builders.d.ts +13 -0
- package/plugins/meta-schema/constraint-meta-builders.js +58 -0
- package/plugins/meta-schema/graphql-meta-field.d.ts +4 -0
- package/plugins/meta-schema/graphql-meta-field.js +204 -0
- package/plugins/meta-schema/inflection-utils.d.ts +4 -0
- package/plugins/meta-schema/inflection-utils.js +25 -0
- package/plugins/meta-schema/name-meta-builders.d.ts +4 -0
- package/plugins/meta-schema/name-meta-builders.js +43 -0
- package/plugins/meta-schema/plugin.d.ts +2 -0
- package/plugins/meta-schema/plugin.js +26 -0
- package/plugins/meta-schema/relation-meta-builders.d.ts +8 -0
- package/plugins/meta-schema/relation-meta-builders.js +120 -0
- package/plugins/meta-schema/table-meta-builder.d.ts +2 -0
- package/plugins/meta-schema/table-meta-builder.js +72 -0
- package/plugins/meta-schema/table-meta-context.d.ts +13 -0
- package/plugins/meta-schema/table-meta-context.js +15 -0
- package/plugins/meta-schema/table-resource-utils.d.ts +12 -0
- package/plugins/meta-schema/table-resource-utils.js +60 -0
- package/plugins/meta-schema/type-mappings.d.ts +3 -0
- package/plugins/meta-schema/type-mappings.js +79 -0
- package/plugins/meta-schema/types.d.ts +206 -0
- package/plugins/meta-schema/types.js +2 -0
- package/plugins/meta-schema.d.ts +19 -0
- package/plugins/meta-schema.js +20 -0
- package/plugins/minimal-preset.d.ts +7 -0
- package/plugins/minimal-preset.js +45 -0
- package/plugins/pg-type-mappings.d.ts +41 -0
- package/plugins/pg-type-mappings.js +128 -0
- package/plugins/primary-key-only.d.ts +96 -0
- package/plugins/primary-key-only.js +147 -0
- package/presets/constructive-preset.d.ts +40 -0
- package/presets/constructive-preset.js +140 -0
- package/presets/index.d.ts +7 -0
- package/presets/index.js +11 -0
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
import { singularize, singularizeLast, pluralizeLast, distinctPluralize, fixCapitalisedPlural, camelize, } from 'inflekt';
|
|
2
|
+
/**
|
|
3
|
+
* Custom inflector plugin for Constructive using the inflekt library.
|
|
4
|
+
*
|
|
5
|
+
* This plugin provides inflection rules based on the inflekt package from dev-utils.
|
|
6
|
+
* It gives us full control over naming conventions and handles Latin plural suffixes
|
|
7
|
+
* correctly (e.g., "schemata" -> "schema" instead of "schematum").
|
|
8
|
+
*
|
|
9
|
+
* Key features:
|
|
10
|
+
* - Uses inflekt for pluralization/singularization with PostGraphile-compatible Latin handling
|
|
11
|
+
* - Simplifies field names (allUsers -> users, postsByAuthorId -> posts)
|
|
12
|
+
* - Customizable opposite name mappings for relations
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* Custom opposite name mappings for relations.
|
|
16
|
+
* For example, if you have a `parent_id` column, this determines
|
|
17
|
+
* what the reverse relation should be called.
|
|
18
|
+
*
|
|
19
|
+
* Add your own mappings here as needed.
|
|
20
|
+
*/
|
|
21
|
+
const CUSTOM_OPPOSITES = {
|
|
22
|
+
parent: 'child',
|
|
23
|
+
child: 'parent',
|
|
24
|
+
author: 'authored',
|
|
25
|
+
editor: 'edited',
|
|
26
|
+
reviewer: 'reviewed',
|
|
27
|
+
owner: 'owned',
|
|
28
|
+
creator: 'created',
|
|
29
|
+
updater: 'updated',
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Extract base name from attribute names like "author_id" -> "author"
|
|
33
|
+
*/
|
|
34
|
+
function getBaseName(attributeName) {
|
|
35
|
+
const matches = attributeName.match(/^(.+?)(_row_id|_id|_uuid|_fk|_pk|RowId|Id|Uuid|UUID|Fk|Pk)$/);
|
|
36
|
+
if (matches) {
|
|
37
|
+
return matches[1];
|
|
38
|
+
}
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Check if a base name matches another name (singularized)
|
|
43
|
+
*/
|
|
44
|
+
function baseNameMatches(baseName, otherName) {
|
|
45
|
+
const singularizedName = singularize(otherName);
|
|
46
|
+
return camelize(baseName, true) === camelize(singularizedName, true);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Get the opposite name for a relation base name
|
|
50
|
+
*/
|
|
51
|
+
function getOppositeBaseName(baseName) {
|
|
52
|
+
return CUSTOM_OPPOSITES[baseName] || null;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Returns true if array1 and array2 have the same length and values
|
|
56
|
+
*/
|
|
57
|
+
function arraysMatch(array1, array2, comparator = (v1, v2) => v1 === v2) {
|
|
58
|
+
if (array1 === array2)
|
|
59
|
+
return true;
|
|
60
|
+
const l = array1.length;
|
|
61
|
+
if (l !== array2.length)
|
|
62
|
+
return false;
|
|
63
|
+
for (let i = 0; i < l; i++) {
|
|
64
|
+
if (!comparator(array1[i], array2[i]))
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
export const InflektPlugin = {
|
|
70
|
+
name: 'InflektPlugin',
|
|
71
|
+
version: '1.0.0',
|
|
72
|
+
inflection: {
|
|
73
|
+
replace: {
|
|
74
|
+
/**
|
|
75
|
+
* Remove schema prefixes from all schemas.
|
|
76
|
+
*
|
|
77
|
+
* WHY THIS EXISTS:
|
|
78
|
+
* PostGraphile v5's default `_schemaPrefix` inflector only removes the prefix
|
|
79
|
+
* for the FIRST schema in the pgServices.schemas array. All other schemas get
|
|
80
|
+
* prefixed with their schema name (e.g., "services_public_api" -> "servicesPublicApi").
|
|
81
|
+
*
|
|
82
|
+
* This is problematic for multi-schema setups where you want clean, consistent
|
|
83
|
+
* naming across all schemas.
|
|
84
|
+
*
|
|
85
|
+
* SOURCE CODE REFERENCE:
|
|
86
|
+
* https://github.com/graphile/crystal/blob/924b2515c6bd30e5905ac1419a25244b40c8bb4d/graphile-build/graphile-build-pg/src/plugins/PgTablesPlugin.ts#L261-L271
|
|
87
|
+
*
|
|
88
|
+
* The relevant v5 code:
|
|
89
|
+
* ```typescript
|
|
90
|
+
* _schemaPrefix(options, { pgNamespace, serviceName }) {
|
|
91
|
+
* const pgService = options.pgServices?.find((db) => db.name === serviceName);
|
|
92
|
+
* const databasePrefix = serviceName === "main" ? "" : `${serviceName}_`;
|
|
93
|
+
* const schemaPrefix =
|
|
94
|
+
* pgNamespace.nspname === pgService?.schemas?.[0] // <-- Only first schema!
|
|
95
|
+
* ? ""
|
|
96
|
+
* : `${pgNamespace.nspname}_`;
|
|
97
|
+
* return `${databasePrefix}${schemaPrefix}`;
|
|
98
|
+
* }
|
|
99
|
+
* ```
|
|
100
|
+
*
|
|
101
|
+
* OUR FIX:
|
|
102
|
+
* We override this to always return an empty string, giving clean names for
|
|
103
|
+
* all schemas. Use the ConflictDetectorPlugin to detect naming conflicts.
|
|
104
|
+
*
|
|
105
|
+
* WARNING: This may cause naming conflicts if you have tables with the
|
|
106
|
+
* same name in different schemas. Use @name smart tags to disambiguate.
|
|
107
|
+
*/
|
|
108
|
+
_schemaPrefix(_previous, _options, _details) {
|
|
109
|
+
return '';
|
|
110
|
+
},
|
|
111
|
+
/**
|
|
112
|
+
* Keep `id` columns as `id` instead of renaming to `rowId`.
|
|
113
|
+
*
|
|
114
|
+
* WHY THIS EXISTS:
|
|
115
|
+
* PostGraphile v5's default `_attributeName` inflector renames any column
|
|
116
|
+
* named "id" to "row_id" to avoid conflicts with the Relay Global Object
|
|
117
|
+
* Identification spec's `id` field. Since we don't use Relay/Node (we use
|
|
118
|
+
* UUIDs), there's no conflict to avoid.
|
|
119
|
+
*
|
|
120
|
+
* NOTE: Disabling NodePlugin does NOT fix this! The renaming happens in
|
|
121
|
+
* PgAttributesPlugin which is a core plugin we need for basic column
|
|
122
|
+
* functionality.
|
|
123
|
+
*
|
|
124
|
+
* SOURCE CODE REFERENCE:
|
|
125
|
+
* https://github.com/graphile/crystal/blob/924b2515c6bd30e5905ac1419a25244b40c8bb4d/graphile-build/graphile-build-pg/src/plugins/PgAttributesPlugin.ts#L289-L298
|
|
126
|
+
*
|
|
127
|
+
* The relevant v5 code:
|
|
128
|
+
* ```typescript
|
|
129
|
+
* _attributeName(options, { attributeName, codec, skipRowId }) {
|
|
130
|
+
* const attribute = codec.attributes[attributeName];
|
|
131
|
+
* const name = attribute.extensions?.tags?.name || attributeName;
|
|
132
|
+
* // Avoid conflict with 'id' field used for Relay.
|
|
133
|
+
* const nonconflictName =
|
|
134
|
+
* !skipRowId && name.toLowerCase() === "id" && !codec.isAnonymous
|
|
135
|
+
* ? "row_id" // <-- This renames id to row_id!
|
|
136
|
+
* : name;
|
|
137
|
+
* return this.coerceToGraphQLName(nonconflictName);
|
|
138
|
+
* }
|
|
139
|
+
* ```
|
|
140
|
+
*
|
|
141
|
+
* OUR FIX:
|
|
142
|
+
* We override this to always use the original attribute name, never
|
|
143
|
+
* renaming `id` to `row_id`. Since we use UUIDs and don't use Relay,
|
|
144
|
+
* there's no naming conflict.
|
|
145
|
+
*/
|
|
146
|
+
_attributeName(_previous, _options, details) {
|
|
147
|
+
const attribute = details.codec.attributes[details.attributeName];
|
|
148
|
+
const name = attribute?.extensions?.tags?.name || details.attributeName;
|
|
149
|
+
return this.coerceToGraphQLName(name);
|
|
150
|
+
},
|
|
151
|
+
/**
|
|
152
|
+
* Fix capitalized plurals (e.g., "Table1S" -> "Table1s")
|
|
153
|
+
*/
|
|
154
|
+
camelCase(previous, _preset, str) {
|
|
155
|
+
const original = previous(str);
|
|
156
|
+
return fixCapitalisedPlural(original);
|
|
157
|
+
},
|
|
158
|
+
upperCamelCase(previous, _preset, str) {
|
|
159
|
+
const original = previous(str);
|
|
160
|
+
return fixCapitalisedPlural(original);
|
|
161
|
+
},
|
|
162
|
+
/**
|
|
163
|
+
* Use inflekt's singularize/pluralize which only changes the last word
|
|
164
|
+
*/
|
|
165
|
+
pluralize(_previous, _preset, str) {
|
|
166
|
+
return pluralizeLast(str);
|
|
167
|
+
},
|
|
168
|
+
singularize(_previous, _preset, str) {
|
|
169
|
+
return singularizeLast(str);
|
|
170
|
+
},
|
|
171
|
+
/**
|
|
172
|
+
* Simplify root query connection fields (allUsers -> users)
|
|
173
|
+
*/
|
|
174
|
+
allRowsConnection(_previous, _options, resource) {
|
|
175
|
+
const resourceName = this._singularizedResourceName(resource);
|
|
176
|
+
return camelize(distinctPluralize(resourceName), true);
|
|
177
|
+
},
|
|
178
|
+
/**
|
|
179
|
+
* Simplify root query list fields
|
|
180
|
+
*/
|
|
181
|
+
allRowsList(_previous, _options, resource) {
|
|
182
|
+
const resourceName = this._singularizedResourceName(resource);
|
|
183
|
+
return camelize(distinctPluralize(resourceName), true) + 'List';
|
|
184
|
+
},
|
|
185
|
+
/**
|
|
186
|
+
* Simplify single relation field names (userByAuthorId -> author)
|
|
187
|
+
*/
|
|
188
|
+
singleRelation(previous, _options, details) {
|
|
189
|
+
const { registry, codec, relationName } = details;
|
|
190
|
+
const relation = registry.pgRelations[codec.name]?.[relationName];
|
|
191
|
+
if (typeof relation.extensions?.tags?.fieldName === 'string') {
|
|
192
|
+
return relation.extensions.tags.fieldName;
|
|
193
|
+
}
|
|
194
|
+
// Try to extract base name from the local attribute
|
|
195
|
+
if (relation.localAttributes.length === 1) {
|
|
196
|
+
const attributeName = relation.localAttributes[0];
|
|
197
|
+
const baseName = getBaseName(attributeName);
|
|
198
|
+
if (baseName) {
|
|
199
|
+
return camelize(baseName, true);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
// Fall back to the remote resource name
|
|
203
|
+
const foreignPk = relation.remoteResource.uniques.find((u) => u.isPrimary);
|
|
204
|
+
if (foreignPk &&
|
|
205
|
+
arraysMatch(foreignPk.attributes, relation.remoteAttributes)) {
|
|
206
|
+
return camelize(this._singularizedCodecName(relation.remoteResource.codec), true);
|
|
207
|
+
}
|
|
208
|
+
return previous(details);
|
|
209
|
+
},
|
|
210
|
+
/**
|
|
211
|
+
* Simplify backwards single relation field names
|
|
212
|
+
*/
|
|
213
|
+
singleRelationBackwards(previous, _options, details) {
|
|
214
|
+
const { registry, codec, relationName } = details;
|
|
215
|
+
const relation = registry.pgRelations[codec.name]?.[relationName];
|
|
216
|
+
if (typeof relation.extensions?.tags?.foreignSingleFieldName === 'string') {
|
|
217
|
+
return relation.extensions.tags.foreignSingleFieldName;
|
|
218
|
+
}
|
|
219
|
+
if (typeof relation.extensions?.tags?.foreignFieldName === 'string') {
|
|
220
|
+
return relation.extensions.tags.foreignFieldName;
|
|
221
|
+
}
|
|
222
|
+
// Try to extract base name from the remote attribute
|
|
223
|
+
if (relation.remoteAttributes.length === 1) {
|
|
224
|
+
const attributeName = relation.remoteAttributes[0];
|
|
225
|
+
const baseName = getBaseName(attributeName);
|
|
226
|
+
if (baseName) {
|
|
227
|
+
const oppositeBaseName = getOppositeBaseName(baseName);
|
|
228
|
+
if (oppositeBaseName) {
|
|
229
|
+
return camelize(`${oppositeBaseName}_${this._singularizedCodecName(relation.remoteResource.codec)}`, true);
|
|
230
|
+
}
|
|
231
|
+
if (baseNameMatches(baseName, codec.name)) {
|
|
232
|
+
return camelize(this._singularizedCodecName(relation.remoteResource.codec), true);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return previous(details);
|
|
237
|
+
},
|
|
238
|
+
/**
|
|
239
|
+
* Simplify many relation field names (postsByAuthorId -> posts)
|
|
240
|
+
*/
|
|
241
|
+
_manyRelation(previous, _options, details) {
|
|
242
|
+
const { registry, codec, relationName } = details;
|
|
243
|
+
const relation = registry.pgRelations[codec.name]?.[relationName];
|
|
244
|
+
const baseOverride = relation.extensions?.tags.foreignFieldName;
|
|
245
|
+
if (typeof baseOverride === 'string') {
|
|
246
|
+
return baseOverride;
|
|
247
|
+
}
|
|
248
|
+
// Try to extract base name from the remote attribute
|
|
249
|
+
if (relation.remoteAttributes.length === 1) {
|
|
250
|
+
const attributeName = relation.remoteAttributes[0];
|
|
251
|
+
const baseName = getBaseName(attributeName);
|
|
252
|
+
if (baseName) {
|
|
253
|
+
const oppositeBaseName = getOppositeBaseName(baseName);
|
|
254
|
+
if (oppositeBaseName) {
|
|
255
|
+
return camelize(`${oppositeBaseName}_${distinctPluralize(this._singularizedCodecName(relation.remoteResource.codec))}`, true);
|
|
256
|
+
}
|
|
257
|
+
if (baseNameMatches(baseName, codec.name)) {
|
|
258
|
+
return camelize(distinctPluralize(this._singularizedCodecName(relation.remoteResource.codec)), true);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
// Fall back to pluralized remote resource name
|
|
263
|
+
const pk = relation.remoteResource.uniques.find((u) => u.isPrimary);
|
|
264
|
+
if (pk && arraysMatch(pk.attributes, relation.remoteAttributes)) {
|
|
265
|
+
return camelize(distinctPluralize(this._singularizedCodecName(relation.remoteResource.codec)), true);
|
|
266
|
+
}
|
|
267
|
+
return previous(details);
|
|
268
|
+
},
|
|
269
|
+
/**
|
|
270
|
+
* Simplify many-to-many relation field names with conflict detection.
|
|
271
|
+
*
|
|
272
|
+
* Default pg-many-to-many naming: tagsByPostTagPostIdAndTagId
|
|
273
|
+
* Our simplified naming: tags
|
|
274
|
+
*
|
|
275
|
+
* Falls back to verbose naming if:
|
|
276
|
+
* - Smart tag override exists (manyToManyFieldName)
|
|
277
|
+
* - There's a direct relation to the same target table (would conflict)
|
|
278
|
+
* - There are multiple many-to-many relations to the same target table
|
|
279
|
+
*/
|
|
280
|
+
_manyToManyRelation(previous, _options, details) {
|
|
281
|
+
const { leftTable, rightTable, junctionTable, rightRelationName } = details;
|
|
282
|
+
const junctionRightRelation = junctionTable.getRelation(rightRelationName);
|
|
283
|
+
const baseOverride = junctionRightRelation.extensions?.tags?.manyToManyFieldName;
|
|
284
|
+
if (typeof baseOverride === 'string') {
|
|
285
|
+
return baseOverride;
|
|
286
|
+
}
|
|
287
|
+
const simpleName = camelize(distinctPluralize(this._singularizedCodecName(rightTable.codec)), true);
|
|
288
|
+
const leftRelations = leftTable.getRelations();
|
|
289
|
+
let hasDirectRelation = false;
|
|
290
|
+
let manyToManyCount = 0;
|
|
291
|
+
for (const [_relName, rel] of Object.entries(leftRelations)) {
|
|
292
|
+
if (rel.remoteResource?.codec?.name === rightTable.codec.name) {
|
|
293
|
+
if (!rel.isReferencee) {
|
|
294
|
+
hasDirectRelation = true;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
if (rel.isReferencee &&
|
|
298
|
+
rel.remoteResource?.codec?.name !== rightTable.codec.name) {
|
|
299
|
+
const junctionRelations = rel.remoteResource?.getRelations?.() || {};
|
|
300
|
+
for (const [_jRelName, jRel] of Object.entries(junctionRelations)) {
|
|
301
|
+
if (!jRel.isReferencee &&
|
|
302
|
+
jRel.remoteResource?.codec?.name === rightTable.codec.name) {
|
|
303
|
+
manyToManyCount++;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
if (hasDirectRelation || manyToManyCount > 1) {
|
|
309
|
+
return previous(details);
|
|
310
|
+
}
|
|
311
|
+
return simpleName;
|
|
312
|
+
},
|
|
313
|
+
/**
|
|
314
|
+
* Shorten primary key lookups (userById -> user)
|
|
315
|
+
*/
|
|
316
|
+
rowByUnique(previous, _options, details) {
|
|
317
|
+
const { unique, resource } = details;
|
|
318
|
+
if (typeof unique.extensions?.tags?.fieldName === 'string') {
|
|
319
|
+
return unique.extensions?.tags?.fieldName;
|
|
320
|
+
}
|
|
321
|
+
if (unique.isPrimary) {
|
|
322
|
+
return camelize(this._singularizedCodecName(resource.codec), true);
|
|
323
|
+
}
|
|
324
|
+
return previous(details);
|
|
325
|
+
},
|
|
326
|
+
/**
|
|
327
|
+
* Shorten update mutation names
|
|
328
|
+
*/
|
|
329
|
+
updateByKeysField(previous, _options, details) {
|
|
330
|
+
const { resource, unique } = details;
|
|
331
|
+
if (typeof unique.extensions?.tags.updateFieldName === 'string') {
|
|
332
|
+
return unique.extensions.tags.updateFieldName;
|
|
333
|
+
}
|
|
334
|
+
if (unique.isPrimary) {
|
|
335
|
+
return camelize(`update_${this._singularizedCodecName(resource.codec)}`, true);
|
|
336
|
+
}
|
|
337
|
+
return previous(details);
|
|
338
|
+
},
|
|
339
|
+
/**
|
|
340
|
+
* Shorten delete mutation names
|
|
341
|
+
*/
|
|
342
|
+
deleteByKeysField(previous, _options, details) {
|
|
343
|
+
const { resource, unique } = details;
|
|
344
|
+
if (typeof unique.extensions?.tags.deleteFieldName === 'string') {
|
|
345
|
+
return unique.extensions.tags.deleteFieldName;
|
|
346
|
+
}
|
|
347
|
+
if (unique.isPrimary) {
|
|
348
|
+
return camelize(`delete_${this._singularizedCodecName(resource.codec)}`, true);
|
|
349
|
+
}
|
|
350
|
+
return previous(details);
|
|
351
|
+
},
|
|
352
|
+
/**
|
|
353
|
+
* Uppercase enum values to match GraphQL CONSTANT_CASE convention.
|
|
354
|
+
*
|
|
355
|
+
* WHY THIS EXISTS:
|
|
356
|
+
* In PostGraphile v4, custom PostgreSQL enum values (e.g., 'app', 'core', 'module')
|
|
357
|
+
* were automatically uppercased to CONSTANT_CASE ('APP', 'CORE', 'MODULE').
|
|
358
|
+
* In PostGraphile v5, the default `enumValue` inflector preserves the original
|
|
359
|
+
* PostgreSQL casing via `coerceToGraphQLName(value)`, resulting in lowercase
|
|
360
|
+
* enum values in the GraphQL schema.
|
|
361
|
+
*
|
|
362
|
+
* OUR FIX:
|
|
363
|
+
* We call the previous inflector to retain all special character handling
|
|
364
|
+
* (asterisks, symbols, etc.), then uppercase the result to restore v4 behavior.
|
|
365
|
+
*/
|
|
366
|
+
enumValue(previous, _options, value, codec) {
|
|
367
|
+
const result = previous(value, codec);
|
|
368
|
+
return result.toUpperCase();
|
|
369
|
+
},
|
|
370
|
+
},
|
|
371
|
+
},
|
|
372
|
+
};
|
|
373
|
+
/**
|
|
374
|
+
* Preset that includes the inflekt-based inflector plugin.
|
|
375
|
+
* Use this in your main preset's `extends` array.
|
|
376
|
+
*/
|
|
377
|
+
export const InflektPreset = {
|
|
378
|
+
plugins: [InflektPlugin],
|
|
379
|
+
};
|
|
380
|
+
// Re-export for backwards compatibility
|
|
381
|
+
export const CustomInflectorPlugin = InflektPlugin;
|
|
382
|
+
export const CustomInflectorPreset = InflektPreset;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { GraphileConfig } from 'graphile-config';
|
|
2
|
+
/**
|
|
3
|
+
* EnableAllFilterColumnsPlugin - Enables filtering on ALL columns, not just indexed ones.
|
|
4
|
+
*
|
|
5
|
+
* WHY THIS EXISTS:
|
|
6
|
+
* PostGraphile v5's `PgIndexBehaviorsPlugin` restricts filtering to only indexed columns
|
|
7
|
+
* by default. This is a performance optimization - filtering on non-indexed columns can
|
|
8
|
+
* cause slow table scans. However, for development and flexibility, we want to allow
|
|
9
|
+
* filtering on all columns and let developers/DBAs decide which columns need indexes.
|
|
10
|
+
*
|
|
11
|
+
* SOURCE CODE REFERENCE:
|
|
12
|
+
* PgIndexBehaviorsPlugin marks non-indexed columns with `extensions.isIndexed = false`
|
|
13
|
+
* and then adds `-filterBy` behavior to remove them from filters:
|
|
14
|
+
* https://github.com/graphile/crystal/blob/924b2515c6bd30e5905ac1419a25244b40c8bb4d/graphile-build/graphile-build-pg/src/plugins/PgIndexBehaviorsPlugin.ts
|
|
15
|
+
*
|
|
16
|
+
* The relevant v5 code (from PgIndexBehaviorsPlugin):
|
|
17
|
+
* ```typescript
|
|
18
|
+
* entityBehavior: {
|
|
19
|
+
* pgCodecAttribute: {
|
|
20
|
+
* inferred: {
|
|
21
|
+
* after: ["inferred"],
|
|
22
|
+
* provides: ["postInferred"],
|
|
23
|
+
* callback(behavior, [codec, attributeName]) {
|
|
24
|
+
* const newBehavior = [behavior];
|
|
25
|
+
* const attr = codec.attributes[attributeName];
|
|
26
|
+
* if (attr.extensions?.isIndexed === false) {
|
|
27
|
+
* newBehavior.push("-filterBy", "-orderBy"); // <-- This removes filterBy!
|
|
28
|
+
* }
|
|
29
|
+
* return newBehavior;
|
|
30
|
+
* },
|
|
31
|
+
* },
|
|
32
|
+
* },
|
|
33
|
+
* },
|
|
34
|
+
* ```
|
|
35
|
+
*
|
|
36
|
+
* OUR FIX:
|
|
37
|
+
* We add a behavior callback that runs AFTER PgIndexBehaviorsPlugin's "postInferred" phase
|
|
38
|
+
* and adds `+attribute:filterBy` back to ALL columns, regardless of index status.
|
|
39
|
+
*
|
|
40
|
+
* This means:
|
|
41
|
+
* - All columns will appear in the connection filter's filter argument
|
|
42
|
+
* - Developers can filter by any column
|
|
43
|
+
* - It's the developer's/DBA's responsibility to add indexes for frequently filtered columns
|
|
44
|
+
*
|
|
45
|
+
* PERFORMANCE WARNING:
|
|
46
|
+
* Filtering on non-indexed columns can cause full table scans, which may be slow on large
|
|
47
|
+
* tables. Monitor your query performance and add indexes as needed. You can check which
|
|
48
|
+
* columns are indexed by querying pg_indexes or using EXPLAIN ANALYZE on your queries.
|
|
49
|
+
*
|
|
50
|
+
* To identify non-indexed columns being filtered, you can:
|
|
51
|
+
* 1. Enable slow query logging in PostgreSQL (log_min_duration_statement)
|
|
52
|
+
* 2. Use EXPLAIN ANALYZE on queries to see if they're doing sequential scans
|
|
53
|
+
* 3. Check pg_stat_user_tables for seq_scan counts
|
|
54
|
+
*/
|
|
55
|
+
export declare const EnableAllFilterColumnsPlugin: GraphileConfig.Plugin;
|
|
56
|
+
/**
|
|
57
|
+
* Preset that includes the EnableAllFilterColumnsPlugin.
|
|
58
|
+
* Add this to your main preset's `extends` array.
|
|
59
|
+
*/
|
|
60
|
+
export declare const EnableAllFilterColumnsPreset: GraphileConfig.Preset;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* EnableAllFilterColumnsPlugin - Enables filtering on ALL columns, not just indexed ones.
|
|
3
|
+
*
|
|
4
|
+
* WHY THIS EXISTS:
|
|
5
|
+
* PostGraphile v5's `PgIndexBehaviorsPlugin` restricts filtering to only indexed columns
|
|
6
|
+
* by default. This is a performance optimization - filtering on non-indexed columns can
|
|
7
|
+
* cause slow table scans. However, for development and flexibility, we want to allow
|
|
8
|
+
* filtering on all columns and let developers/DBAs decide which columns need indexes.
|
|
9
|
+
*
|
|
10
|
+
* SOURCE CODE REFERENCE:
|
|
11
|
+
* PgIndexBehaviorsPlugin marks non-indexed columns with `extensions.isIndexed = false`
|
|
12
|
+
* and then adds `-filterBy` behavior to remove them from filters:
|
|
13
|
+
* https://github.com/graphile/crystal/blob/924b2515c6bd30e5905ac1419a25244b40c8bb4d/graphile-build/graphile-build-pg/src/plugins/PgIndexBehaviorsPlugin.ts
|
|
14
|
+
*
|
|
15
|
+
* The relevant v5 code (from PgIndexBehaviorsPlugin):
|
|
16
|
+
* ```typescript
|
|
17
|
+
* entityBehavior: {
|
|
18
|
+
* pgCodecAttribute: {
|
|
19
|
+
* inferred: {
|
|
20
|
+
* after: ["inferred"],
|
|
21
|
+
* provides: ["postInferred"],
|
|
22
|
+
* callback(behavior, [codec, attributeName]) {
|
|
23
|
+
* const newBehavior = [behavior];
|
|
24
|
+
* const attr = codec.attributes[attributeName];
|
|
25
|
+
* if (attr.extensions?.isIndexed === false) {
|
|
26
|
+
* newBehavior.push("-filterBy", "-orderBy"); // <-- This removes filterBy!
|
|
27
|
+
* }
|
|
28
|
+
* return newBehavior;
|
|
29
|
+
* },
|
|
30
|
+
* },
|
|
31
|
+
* },
|
|
32
|
+
* },
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* OUR FIX:
|
|
36
|
+
* We add a behavior callback that runs AFTER PgIndexBehaviorsPlugin's "postInferred" phase
|
|
37
|
+
* and adds `+attribute:filterBy` back to ALL columns, regardless of index status.
|
|
38
|
+
*
|
|
39
|
+
* This means:
|
|
40
|
+
* - All columns will appear in the connection filter's filter argument
|
|
41
|
+
* - Developers can filter by any column
|
|
42
|
+
* - It's the developer's/DBA's responsibility to add indexes for frequently filtered columns
|
|
43
|
+
*
|
|
44
|
+
* PERFORMANCE WARNING:
|
|
45
|
+
* Filtering on non-indexed columns can cause full table scans, which may be slow on large
|
|
46
|
+
* tables. Monitor your query performance and add indexes as needed. You can check which
|
|
47
|
+
* columns are indexed by querying pg_indexes or using EXPLAIN ANALYZE on your queries.
|
|
48
|
+
*
|
|
49
|
+
* To identify non-indexed columns being filtered, you can:
|
|
50
|
+
* 1. Enable slow query logging in PostgreSQL (log_min_duration_statement)
|
|
51
|
+
* 2. Use EXPLAIN ANALYZE on queries to see if they're doing sequential scans
|
|
52
|
+
* 3. Check pg_stat_user_tables for seq_scan counts
|
|
53
|
+
*/
|
|
54
|
+
export const EnableAllFilterColumnsPlugin = {
|
|
55
|
+
name: 'EnableAllFilterColumnsPlugin',
|
|
56
|
+
version: '1.0.0',
|
|
57
|
+
description: 'Enables filtering on all columns, not just indexed ones',
|
|
58
|
+
schema: {
|
|
59
|
+
entityBehavior: {
|
|
60
|
+
pgCodecAttribute: {
|
|
61
|
+
/**
|
|
62
|
+
* This callback runs in the "inferred" phase AFTER PgIndexBehaviorsPlugin's
|
|
63
|
+
* "postInferred" phase. It adds `filterBy` back to ALL columns, overriding
|
|
64
|
+
* the `-filterBy` that PgIndexBehaviorsPlugin adds to non-indexed columns.
|
|
65
|
+
*/
|
|
66
|
+
inferred: {
|
|
67
|
+
after: ['postInferred'],
|
|
68
|
+
provides: ['enableAllFilters'],
|
|
69
|
+
callback(behavior) {
|
|
70
|
+
// Add filterBy to override any -filterBy from PgIndexBehaviorsPlugin
|
|
71
|
+
// The behavior system will resolve conflicts, with later additions winning
|
|
72
|
+
return [behavior, 'filterBy'];
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* Preset that includes the EnableAllFilterColumnsPlugin.
|
|
81
|
+
* Add this to your main preset's `extends` array.
|
|
82
|
+
*/
|
|
83
|
+
export const EnableAllFilterColumnsPreset = {
|
|
84
|
+
plugins: [EnableAllFilterColumnsPlugin],
|
|
85
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PostGraphile v5 Plugins
|
|
3
|
+
*
|
|
4
|
+
* This module exports all custom plugins for PostGraphile v5.
|
|
5
|
+
* Each plugin can be used individually or combined via the presets.
|
|
6
|
+
*/
|
|
7
|
+
export { MinimalPreset } from './minimal-preset';
|
|
8
|
+
export { InflektPlugin, InflektPreset, CustomInflectorPlugin, CustomInflectorPreset, } from './custom-inflector';
|
|
9
|
+
export { ConflictDetectorPlugin, ConflictDetectorPreset, } from './conflict-detector';
|
|
10
|
+
export { InflectorLoggerPlugin, InflectorLoggerPreset, } from './inflector-logger';
|
|
11
|
+
export { EnableAllFilterColumnsPlugin, EnableAllFilterColumnsPreset, } from './enable-all-filter-columns';
|
|
12
|
+
export { ManyToManyOptInPlugin, ManyToManyOptInPreset, } from './many-to-many-preset';
|
|
13
|
+
export { createUniqueLookupPlugin, PrimaryKeyOnlyPlugin, NoUniqueLookupPlugin, PrimaryKeyOnlyPreset, NoUniqueLookupPreset, } from './primary-key-only';
|
|
14
|
+
export type { UniqueLookupOptions } from './primary-key-only';
|
|
15
|
+
export { MetaSchemaPlugin, MetaSchemaPreset, } from './meta-schema';
|
|
16
|
+
export { PgTypeMappingsPlugin, PgTypeMappingsPreset, } from './pg-type-mappings';
|
|
17
|
+
export type { TypeMapping } from './pg-type-mappings';
|
|
18
|
+
export { PgSearchPlugin, PgSearchPreset, createPgSearchPlugin, TsvectorCodecPlugin, TsvectorCodecPreset, } from 'graphile-search-plugin';
|
|
19
|
+
export type { PgSearchPluginOptions } from 'graphile-search-plugin';
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PostGraphile v5 Plugins
|
|
3
|
+
*
|
|
4
|
+
* This module exports all custom plugins for PostGraphile v5.
|
|
5
|
+
* Each plugin can be used individually or combined via the presets.
|
|
6
|
+
*/
|
|
7
|
+
// Minimal preset - PostGraphile without Node/Relay features
|
|
8
|
+
export { MinimalPreset } from './minimal-preset';
|
|
9
|
+
// Custom inflector using inflekt library
|
|
10
|
+
export { InflektPlugin, InflektPreset, CustomInflectorPlugin, CustomInflectorPreset, } from './custom-inflector';
|
|
11
|
+
// Conflict detector for multi-schema setups
|
|
12
|
+
export { ConflictDetectorPlugin, ConflictDetectorPreset, } from './conflict-detector';
|
|
13
|
+
// Inflector logger for debugging
|
|
14
|
+
export { InflectorLoggerPlugin, InflectorLoggerPreset, } from './inflector-logger';
|
|
15
|
+
// Enable filtering on all columns (not just indexed)
|
|
16
|
+
export { EnableAllFilterColumnsPlugin, EnableAllFilterColumnsPreset, } from './enable-all-filter-columns';
|
|
17
|
+
// Many-to-many with opt-in behavior
|
|
18
|
+
export { ManyToManyOptInPlugin, ManyToManyOptInPreset, } from './many-to-many-preset';
|
|
19
|
+
// Primary key only lookups (disable non-PK unique constraints)
|
|
20
|
+
export { createUniqueLookupPlugin, PrimaryKeyOnlyPlugin, NoUniqueLookupPlugin, PrimaryKeyOnlyPreset, NoUniqueLookupPreset, } from './primary-key-only';
|
|
21
|
+
// Meta schema plugin for introspection (tables, fields, indexes, constraints)
|
|
22
|
+
export { MetaSchemaPlugin, MetaSchemaPreset, } from './meta-schema';
|
|
23
|
+
// PG type mappings for custom PostgreSQL types (email, url, etc.)
|
|
24
|
+
export { PgTypeMappingsPlugin, PgTypeMappingsPreset, } from './pg-type-mappings';
|
|
25
|
+
// Search plugin for tsvector full-text search conditions (includes TsvectorCodec)
|
|
26
|
+
export { PgSearchPlugin, PgSearchPreset, createPgSearchPlugin, TsvectorCodecPlugin, TsvectorCodecPreset, } from 'graphile-search-plugin';
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { GraphileConfig } from 'graphile-config';
|
|
2
|
+
export declare const InflectorLoggerPlugin: GraphileConfig.Plugin;
|
|
3
|
+
/**
|
|
4
|
+
* Preset that includes the inflector logger plugin.
|
|
5
|
+
* Use this in your main preset's `extends` array.
|
|
6
|
+
*/
|
|
7
|
+
export declare const InflectorLoggerPreset: GraphileConfig.Preset;
|