graphile-settings 4.9.3 → 4.10.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.
Files changed (99) hide show
  1. package/esm/plugins/PublicKeySignature.d.ts +11 -0
  2. package/esm/plugins/PublicKeySignature.js +184 -0
  3. package/esm/plugins/conflict-detector.d.ts +7 -0
  4. package/esm/plugins/conflict-detector.js +67 -0
  5. package/esm/plugins/custom-inflector.d.ts +9 -0
  6. package/esm/plugins/custom-inflector.js +394 -0
  7. package/esm/plugins/enable-all-filter-columns.d.ts +60 -0
  8. package/esm/plugins/enable-all-filter-columns.js +85 -0
  9. package/esm/plugins/index.d.ts +16 -4
  10. package/esm/plugins/index.js +25 -4
  11. package/esm/plugins/inflector-logger.d.ts +7 -0
  12. package/esm/plugins/inflector-logger.js +215 -0
  13. package/esm/plugins/many-to-many-preset.d.ts +62 -0
  14. package/esm/plugins/many-to-many-preset.js +86 -0
  15. package/esm/plugins/meta-schema/cache.d.ts +4 -0
  16. package/esm/plugins/meta-schema/cache.js +7 -0
  17. package/esm/plugins/meta-schema/constraint-meta-builders.d.ts +13 -0
  18. package/esm/plugins/meta-schema/constraint-meta-builders.js +51 -0
  19. package/esm/plugins/meta-schema/graphql-meta-field.d.ts +4 -0
  20. package/esm/plugins/meta-schema/graphql-meta-field.js +201 -0
  21. package/esm/plugins/meta-schema/inflection-utils.d.ts +4 -0
  22. package/esm/plugins/meta-schema/inflection-utils.js +20 -0
  23. package/esm/plugins/meta-schema/name-meta-builders.d.ts +4 -0
  24. package/esm/plugins/meta-schema/name-meta-builders.js +38 -0
  25. package/esm/plugins/meta-schema/plugin.d.ts +2 -0
  26. package/esm/plugins/meta-schema/plugin.js +23 -0
  27. package/esm/plugins/meta-schema/relation-meta-builders.d.ts +8 -0
  28. package/esm/plugins/meta-schema/relation-meta-builders.js +115 -0
  29. package/esm/plugins/meta-schema/table-meta-builder.d.ts +2 -0
  30. package/esm/plugins/meta-schema/table-meta-builder.js +69 -0
  31. package/esm/plugins/meta-schema/table-meta-context.d.ts +13 -0
  32. package/esm/plugins/meta-schema/table-meta-context.js +11 -0
  33. package/esm/plugins/meta-schema/table-resource-utils.d.ts +12 -0
  34. package/esm/plugins/meta-schema/table-resource-utils.js +50 -0
  35. package/esm/plugins/meta-schema/type-mappings.d.ts +3 -0
  36. package/esm/plugins/meta-schema/type-mappings.js +75 -0
  37. package/esm/plugins/meta-schema/types.d.ts +206 -0
  38. package/esm/plugins/meta-schema/types.js +1 -0
  39. package/esm/plugins/meta-schema.d.ts +19 -0
  40. package/esm/plugins/meta-schema.js +20 -0
  41. package/esm/plugins/minimal-preset.d.ts +7 -0
  42. package/esm/plugins/minimal-preset.js +42 -0
  43. package/esm/plugins/pg-type-mappings.d.ts +41 -0
  44. package/esm/plugins/pg-type-mappings.js +122 -0
  45. package/esm/plugins/primary-key-only.d.ts +96 -0
  46. package/esm/plugins/primary-key-only.js +143 -0
  47. package/esm/plugins/required-input-plugin.d.ts +37 -0
  48. package/esm/plugins/required-input-plugin.js +88 -0
  49. package/esm/presets/constructive-preset.js +2 -1
  50. package/package.json +11 -9
  51. package/plugins/PublicKeySignature.d.ts +11 -0
  52. package/plugins/PublicKeySignature.js +191 -0
  53. package/plugins/conflict-detector.d.ts +7 -0
  54. package/plugins/conflict-detector.js +70 -0
  55. package/plugins/custom-inflector.d.ts +9 -0
  56. package/plugins/custom-inflector.js +397 -0
  57. package/plugins/enable-all-filter-columns.d.ts +60 -0
  58. package/plugins/enable-all-filter-columns.js +88 -0
  59. package/plugins/index.d.ts +16 -4
  60. package/plugins/index.js +54 -31
  61. package/plugins/inflector-logger.d.ts +7 -0
  62. package/plugins/inflector-logger.js +218 -0
  63. package/plugins/many-to-many-preset.d.ts +62 -0
  64. package/plugins/many-to-many-preset.js +89 -0
  65. package/plugins/meta-schema/cache.d.ts +4 -0
  66. package/plugins/meta-schema/cache.js +12 -0
  67. package/plugins/meta-schema/constraint-meta-builders.d.ts +13 -0
  68. package/plugins/meta-schema/constraint-meta-builders.js +58 -0
  69. package/plugins/meta-schema/graphql-meta-field.d.ts +4 -0
  70. package/plugins/meta-schema/graphql-meta-field.js +204 -0
  71. package/plugins/meta-schema/inflection-utils.d.ts +4 -0
  72. package/plugins/meta-schema/inflection-utils.js +25 -0
  73. package/plugins/meta-schema/name-meta-builders.d.ts +4 -0
  74. package/plugins/meta-schema/name-meta-builders.js +43 -0
  75. package/plugins/meta-schema/plugin.d.ts +2 -0
  76. package/plugins/meta-schema/plugin.js +26 -0
  77. package/plugins/meta-schema/relation-meta-builders.d.ts +8 -0
  78. package/plugins/meta-schema/relation-meta-builders.js +120 -0
  79. package/plugins/meta-schema/table-meta-builder.d.ts +2 -0
  80. package/plugins/meta-schema/table-meta-builder.js +72 -0
  81. package/plugins/meta-schema/table-meta-context.d.ts +13 -0
  82. package/plugins/meta-schema/table-meta-context.js +15 -0
  83. package/plugins/meta-schema/table-resource-utils.d.ts +12 -0
  84. package/plugins/meta-schema/table-resource-utils.js +60 -0
  85. package/plugins/meta-schema/type-mappings.d.ts +3 -0
  86. package/plugins/meta-schema/type-mappings.js +79 -0
  87. package/plugins/meta-schema/types.d.ts +206 -0
  88. package/plugins/meta-schema/types.js +2 -0
  89. package/plugins/meta-schema.d.ts +19 -0
  90. package/plugins/meta-schema.js +20 -0
  91. package/plugins/minimal-preset.d.ts +7 -0
  92. package/plugins/minimal-preset.js +45 -0
  93. package/plugins/pg-type-mappings.d.ts +41 -0
  94. package/plugins/pg-type-mappings.js +128 -0
  95. package/plugins/primary-key-only.d.ts +96 -0
  96. package/plugins/primary-key-only.js +147 -0
  97. package/plugins/required-input-plugin.d.ts +37 -0
  98. package/plugins/required-input-plugin.js +91 -0
  99. package/presets/constructive-preset.js +11 -10
@@ -0,0 +1,147 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NoUniqueLookupPreset = exports.PrimaryKeyOnlyPreset = exports.NoUniqueLookupPlugin = exports.PrimaryKeyOnlyPlugin = void 0;
4
+ exports.createUniqueLookupPlugin = createUniqueLookupPlugin;
5
+ /**
6
+ * PrimaryKeyOnlyPlugin - Disables non-primary-key unique constraint lookups for
7
+ * BOTH queries AND mutations.
8
+ *
9
+ * WHY THIS EXISTS:
10
+ * PostGraphile v5 creates fields for EVERY unique constraint on a table:
11
+ *
12
+ * QUERIES (PgRowByUniquePlugin):
13
+ * - `user(id)`, `userByEmail(email)`, `userByUsername(username)`
14
+ *
15
+ * MUTATIONS (PgMutationUpdateDeletePlugin):
16
+ * - `updateUser`, `updateUserByEmail`, `updateUserByUsername`
17
+ * - `deleteUser`, `deleteUserByEmail`, `deleteUserByUsername`
18
+ *
19
+ * For code generation (React Query, etc.), this creates unnecessary complexity.
20
+ * The same operations can be done using the primary key lookup or filters.
21
+ * Standardizing on primary keys reduces the API surface and generated code.
22
+ *
23
+ * SOURCE CODE REFERENCES:
24
+ *
25
+ * 1. Query fields (PgRowByUniquePlugin):
26
+ * https://github.com/graphile/crystal/blob/924b2515c6bd30e5905ac1419a25244b40c8bb4d/graphile-build/graphile-build-pg/src/plugins/PgRowByUniquePlugin.ts#L42-L257
27
+ *
28
+ * The behavior check for queries:
29
+ * ```typescript
30
+ * const fieldBehaviorScope = "query:resource:single";
31
+ * if (!build.behavior.pgResourceUniqueMatches([resource, unique], fieldBehaviorScope)) {
32
+ * return memo; // Skip this field
33
+ * }
34
+ * ```
35
+ *
36
+ * 2. Mutation fields (PgMutationUpdateDeletePlugin):
37
+ * https://github.com/graphile/crystal/blob/924b2515c6bd30e5905ac1419a25244b40c8bb4d/graphile-build/graphile-build-pg/src/plugins/PgMutationUpdateDeletePlugin.ts
38
+ *
39
+ * The behavior check for mutations:
40
+ * ```typescript
41
+ * const constraintMode = `constraint:${mode}`; // "constraint:resource:update" or "constraint:resource:delete"
42
+ * ...resource.uniques.filter((unique) => {
43
+ * return build.behavior.pgResourceUniqueMatches([resource, unique], constraintMode);
44
+ * })
45
+ * ```
46
+ *
47
+ * OUR FIX:
48
+ * We use the behavior system's OVERRIDE phase (not inferred) to disable these behaviors.
49
+ * The override phase runs AFTER the behavior multiplication/preferences system processes
50
+ * behaviors, giving us the final say on what's enabled/disabled.
51
+ *
52
+ * Behaviors we control:
53
+ * - `-single` - Disables query lookups (userByEmail, etc.)
54
+ * - `-constraint:resource:update` - Disables updateByX mutations
55
+ * - `-constraint:resource:delete` - Disables deleteByX mutations
56
+ *
57
+ * CONFIGURATION OPTIONS:
58
+ *
59
+ * 1. `disableAllUniqueLookups: false` (default - PrimaryKeyOnlyPreset):
60
+ * - Primary key: query lookup + mutations enabled
61
+ * - Non-primary-key: everything disabled
62
+ * Result: `user(id)`, `updateUser`, `deleteUser` only
63
+ *
64
+ * 2. `disableAllUniqueLookups: true` (NoUniqueLookupPreset):
65
+ * - Primary key: query lookup DISABLED, mutations ENABLED
66
+ * - Non-primary-key: everything disabled
67
+ * Result: No query lookups (use filters), but `updateUser`, `deleteUser` still work
68
+ */
69
+ /**
70
+ * Creates a plugin that controls unique constraint lookup behavior.
71
+ *
72
+ * @param options - Configuration options
73
+ * @param options.disableAllUniqueLookups - If true, disables ALL unique lookups including primary keys
74
+ */
75
+ function createUniqueLookupPlugin(options = {}) {
76
+ const { disableAllUniqueLookups = false } = options;
77
+ return {
78
+ name: 'UniqueLookupPlugin',
79
+ version: '1.0.0',
80
+ description: disableAllUniqueLookups
81
+ ? 'Disables all unique constraint lookups (use filters instead)'
82
+ : 'Disables non-primary-key unique constraint lookups to reduce API surface',
83
+ schema: {
84
+ entityBehavior: {
85
+ pgResourceUnique: {
86
+ // Use 'override' phase instead of 'inferred' - override runs AFTER
87
+ // the behavior multiplication/preferences system processes behaviors,
88
+ // so it has the final say on what behaviors are enabled/disabled.
89
+ override: {
90
+ provides: ['uniqueLookupControl'],
91
+ callback(behavior, [_resource, unique]) {
92
+ if (disableAllUniqueLookups) {
93
+ // Disable ALL unique QUERY lookups - users must use filters
94
+ // But KEEP primary key mutations (updateX, deleteX)
95
+ if (unique.isPrimary) {
96
+ // Primary key: only disable query lookups, keep mutations
97
+ return [behavior, '-single'];
98
+ }
99
+ // Non-primary-key: disable everything (queries and mutations)
100
+ return [
101
+ behavior,
102
+ '-single',
103
+ '-constraint:resource:update',
104
+ '-constraint:resource:delete',
105
+ ];
106
+ }
107
+ // Only allow primary key lookups (both queries and mutations)
108
+ if (!unique.isPrimary) {
109
+ // Disable non-primary-key unique constraint lookups for queries and mutations
110
+ return [
111
+ behavior,
112
+ '-single',
113
+ '-constraint:resource:update',
114
+ '-constraint:resource:delete',
115
+ ];
116
+ }
117
+ return behavior;
118
+ },
119
+ },
120
+ },
121
+ },
122
+ },
123
+ };
124
+ }
125
+ // Default plugin instance (primary key only)
126
+ exports.PrimaryKeyOnlyPlugin = createUniqueLookupPlugin({
127
+ disableAllUniqueLookups: false,
128
+ });
129
+ // Plugin that disables ALL unique lookups
130
+ exports.NoUniqueLookupPlugin = createUniqueLookupPlugin({
131
+ disableAllUniqueLookups: true,
132
+ });
133
+ /**
134
+ * Preset that keeps only primary key lookups.
135
+ * Use this in your main preset's `extends` array.
136
+ */
137
+ exports.PrimaryKeyOnlyPreset = {
138
+ plugins: [exports.PrimaryKeyOnlyPlugin],
139
+ };
140
+ /**
141
+ * Preset that disables ALL unique lookups (including primary keys).
142
+ * Users must use collection queries with filters instead.
143
+ * Use this in your main preset's `extends` array.
144
+ */
145
+ exports.NoUniqueLookupPreset = {
146
+ plugins: [exports.NoUniqueLookupPlugin],
147
+ };
@@ -0,0 +1,37 @@
1
+ import 'graphile-build';
2
+ import 'graphile-build-pg';
3
+ import type { GraphileConfig } from 'graphile-config';
4
+ /**
5
+ * RequiredInputPlugin - Makes @requiredInput tagged fields non-nullable in mutation inputs.
6
+ *
7
+ * WHY THIS EXISTS:
8
+ * Some foreign key columns are nullable at the database level (to allow SET NULL cascades
9
+ * on FK deletion) but should be required at the API level (the application should always
10
+ * provide a value when creating or updating a record).
11
+ *
12
+ * The `api_required` column in constructive-db's metaschema injects a `@requiredInput`
13
+ * smart tag via PostgreSQL COMMENT ON COLUMN. This plugin reads that tag and wraps the
14
+ * corresponding GraphQL input field type with NonNull, making it required in mutations
15
+ * while keeping the output type nullable.
16
+ *
17
+ * USAGE:
18
+ * In the database, set the smart tag on a column:
19
+ * COMMENT ON COLUMN app_public.membership.grantor_id IS E'@requiredInput';
20
+ *
21
+ * Or via constructive-db's metaschema:
22
+ * UPDATE metaschema_public.field SET api_required = true WHERE ...;
23
+ *
24
+ * The trigger `tg_sync_api_required_smart_tag` will inject `{"requiredInput": true}`
25
+ * into the column's smart_tags JSONB, which PostGraphile reads as `@requiredInput`.
26
+ *
27
+ * RESULT:
28
+ * - Mutation input types (create, patch, base): field becomes non-nullable (required)
29
+ * - Output types: field remains nullable (unchanged)
30
+ * - Query filters: unaffected
31
+ */
32
+ export declare const RequiredInputPlugin: GraphileConfig.Plugin;
33
+ /**
34
+ * Preset that includes the RequiredInputPlugin.
35
+ * Add this to your main preset's `extends` array.
36
+ */
37
+ export declare const RequiredInputPreset: GraphileConfig.Preset;
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RequiredInputPreset = exports.RequiredInputPlugin = void 0;
4
+ require("graphile-build");
5
+ require("graphile-build-pg");
6
+ /**
7
+ * RequiredInputPlugin - Makes @requiredInput tagged fields non-nullable in mutation inputs.
8
+ *
9
+ * WHY THIS EXISTS:
10
+ * Some foreign key columns are nullable at the database level (to allow SET NULL cascades
11
+ * on FK deletion) but should be required at the API level (the application should always
12
+ * provide a value when creating or updating a record).
13
+ *
14
+ * The `api_required` column in constructive-db's metaschema injects a `@requiredInput`
15
+ * smart tag via PostgreSQL COMMENT ON COLUMN. This plugin reads that tag and wraps the
16
+ * corresponding GraphQL input field type with NonNull, making it required in mutations
17
+ * while keeping the output type nullable.
18
+ *
19
+ * USAGE:
20
+ * In the database, set the smart tag on a column:
21
+ * COMMENT ON COLUMN app_public.membership.grantor_id IS E'@requiredInput';
22
+ *
23
+ * Or via constructive-db's metaschema:
24
+ * UPDATE metaschema_public.field SET api_required = true WHERE ...;
25
+ *
26
+ * The trigger `tg_sync_api_required_smart_tag` will inject `{"requiredInput": true}`
27
+ * into the column's smart_tags JSONB, which PostGraphile reads as `@requiredInput`.
28
+ *
29
+ * RESULT:
30
+ * - Mutation input types (create, patch, base): field becomes non-nullable (required)
31
+ * - Output types: field remains nullable (unchanged)
32
+ * - Query filters: unaffected
33
+ */
34
+ exports.RequiredInputPlugin = {
35
+ name: 'RequiredInputPlugin',
36
+ version: '1.0.0',
37
+ description: 'Makes @requiredInput tagged fields non-nullable in mutation input types',
38
+ schema: {
39
+ hooks: {
40
+ GraphQLInputObjectType_fields(fields, build, context) {
41
+ const { scope: { pgCodec, isPgPatch, isPgBaseInput, isMutationInput }, } = context;
42
+ // Only process row-based input types (create inputs, patch inputs, base inputs)
43
+ if (!pgCodec || !pgCodec.attributes) {
44
+ return fields;
45
+ }
46
+ // Must be a mutation-related input type
47
+ if (!isPgPatch && !isPgBaseInput && !isMutationInput) {
48
+ return fields;
49
+ }
50
+ const { graphql: { GraphQLNonNull }, } = build;
51
+ let newFields = fields;
52
+ for (const [attributeName, attribute] of Object.entries(pgCodec.attributes)) {
53
+ // Check for @requiredInput smart tag
54
+ const tags = attribute.extensions?.tags;
55
+ if (!tags || !tags.requiredInput) {
56
+ continue;
57
+ }
58
+ // Find the corresponding GraphQL field name using inflection
59
+ const fieldName = build.inflection.attribute({
60
+ codec: pgCodec,
61
+ attributeName,
62
+ });
63
+ const existingField = newFields[fieldName];
64
+ if (!existingField) {
65
+ continue;
66
+ }
67
+ // If the field type is already NonNull, skip
68
+ const fieldType = existingField.type;
69
+ if (fieldType instanceof GraphQLNonNull) {
70
+ continue;
71
+ }
72
+ // Wrap the field type with NonNull to make it required
73
+ newFields = build.extend(newFields, {
74
+ [fieldName]: {
75
+ ...existingField,
76
+ type: new GraphQLNonNull(fieldType),
77
+ },
78
+ }, `RequiredInputPlugin making '${fieldName}' non-nullable on '${pgCodec.name}' (via @requiredInput smart tag)`);
79
+ }
80
+ return newFields;
81
+ },
82
+ },
83
+ },
84
+ };
85
+ /**
86
+ * Preset that includes the RequiredInputPlugin.
87
+ * Add this to your main preset's `extends` array.
88
+ */
89
+ exports.RequiredInputPreset = {
90
+ plugins: [exports.RequiredInputPlugin],
91
+ };
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ConstructivePreset = void 0;
4
4
  const graphile_connection_filter_1 = require("graphile-connection-filter");
5
- const graphile_misc_plugins_1 = require("graphile-misc-plugins");
5
+ const plugins_1 = require("../plugins");
6
6
  const graphile_search_1 = require("graphile-search");
7
7
  const graphile_postgis_1 = require("graphile-postgis");
8
8
  const graphile_upload_plugin_1 = require("graphile-upload-plugin");
@@ -62,15 +62,15 @@ const upload_resolver_1 = require("../upload-resolver");
62
62
  */
63
63
  exports.ConstructivePreset = {
64
64
  extends: [
65
- graphile_misc_plugins_1.MinimalPreset,
66
- graphile_misc_plugins_1.ConflictDetectorPreset,
67
- graphile_misc_plugins_1.InflektPreset,
68
- graphile_misc_plugins_1.InflectorLoggerPreset,
69
- graphile_misc_plugins_1.NoUniqueLookupPreset,
65
+ plugins_1.MinimalPreset,
66
+ plugins_1.ConflictDetectorPreset,
67
+ plugins_1.InflektPreset,
68
+ plugins_1.InflectorLoggerPreset,
69
+ plugins_1.NoUniqueLookupPreset,
70
70
  (0, graphile_connection_filter_1.ConnectionFilterPreset)({ connectionFilterRelations: true }),
71
- graphile_misc_plugins_1.EnableAllFilterColumnsPreset,
72
- graphile_misc_plugins_1.ManyToManyOptInPreset,
73
- graphile_misc_plugins_1.MetaSchemaPreset,
71
+ plugins_1.EnableAllFilterColumnsPreset,
72
+ plugins_1.ManyToManyOptInPreset,
73
+ plugins_1.MetaSchemaPreset,
74
74
  (0, graphile_search_1.UnifiedSearchPreset)({ fullTextScalarName: 'FullText', tsConfig: 'english' }),
75
75
  graphile_postgis_1.GraphilePostgisPreset,
76
76
  (0, graphile_upload_plugin_1.UploadPreset)({
@@ -78,7 +78,8 @@ exports.ConstructivePreset = {
78
78
  maxFileSize: 10 * 1024 * 1024, // 10MB
79
79
  }),
80
80
  (0, graphile_sql_expression_validator_1.SqlExpressionValidatorPreset)(),
81
- graphile_misc_plugins_1.PgTypeMappingsPreset,
81
+ plugins_1.PgTypeMappingsPreset,
82
+ plugins_1.RequiredInputPreset,
82
83
  ],
83
84
  /**
84
85
  * Disable PostGraphile core's condition argument entirely.