@constructive-io/graphql-query 3.2.4 → 3.3.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 (88) hide show
  1. package/README.md +411 -65
  2. package/ast.d.ts +4 -4
  3. package/ast.js +24 -9
  4. package/client/error.d.ts +95 -0
  5. package/client/error.js +277 -0
  6. package/client/execute.d.ts +57 -0
  7. package/client/execute.js +124 -0
  8. package/client/index.d.ts +8 -0
  9. package/client/index.js +20 -0
  10. package/client/typed-document.d.ts +31 -0
  11. package/client/typed-document.js +44 -0
  12. package/custom-ast.d.ts +22 -8
  13. package/custom-ast.js +16 -1
  14. package/esm/ast.js +22 -7
  15. package/esm/client/error.js +271 -0
  16. package/esm/client/execute.js +120 -0
  17. package/esm/client/index.js +8 -0
  18. package/esm/client/typed-document.js +40 -0
  19. package/esm/custom-ast.js +16 -1
  20. package/esm/generators/field-selector.js +381 -0
  21. package/esm/generators/index.js +13 -0
  22. package/esm/generators/mutations.js +200 -0
  23. package/esm/generators/naming-helpers.js +154 -0
  24. package/esm/generators/select.js +661 -0
  25. package/esm/index.js +30 -0
  26. package/esm/introspect/index.js +9 -0
  27. package/esm/introspect/infer-tables.js +697 -0
  28. package/esm/introspect/schema-query.js +120 -0
  29. package/esm/introspect/transform-schema.js +271 -0
  30. package/esm/introspect/transform.js +38 -0
  31. package/esm/meta-object/convert.js +3 -0
  32. package/esm/meta-object/format.json +11 -41
  33. package/esm/meta-object/validate.js +20 -4
  34. package/esm/query-builder.js +14 -18
  35. package/esm/types/index.js +18 -0
  36. package/esm/types/introspection.js +54 -0
  37. package/esm/types/mutation.js +4 -0
  38. package/esm/types/query.js +4 -0
  39. package/esm/types/schema.js +5 -0
  40. package/esm/types/selection.js +4 -0
  41. package/esm/utils.js +69 -0
  42. package/generators/field-selector.d.ts +30 -0
  43. package/generators/field-selector.js +387 -0
  44. package/generators/index.d.ts +9 -0
  45. package/generators/index.js +42 -0
  46. package/generators/mutations.d.ts +30 -0
  47. package/generators/mutations.js +238 -0
  48. package/generators/naming-helpers.d.ts +48 -0
  49. package/generators/naming-helpers.js +169 -0
  50. package/generators/select.d.ts +39 -0
  51. package/generators/select.js +705 -0
  52. package/index.d.ts +19 -0
  53. package/index.js +34 -1
  54. package/introspect/index.d.ts +9 -0
  55. package/introspect/index.js +25 -0
  56. package/introspect/infer-tables.d.ts +42 -0
  57. package/introspect/infer-tables.js +700 -0
  58. package/introspect/schema-query.d.ts +20 -0
  59. package/introspect/schema-query.js +123 -0
  60. package/introspect/transform-schema.d.ts +86 -0
  61. package/introspect/transform-schema.js +281 -0
  62. package/introspect/transform.d.ts +20 -0
  63. package/introspect/transform.js +43 -0
  64. package/meta-object/convert.d.ts +3 -0
  65. package/meta-object/convert.js +3 -0
  66. package/meta-object/format.json +11 -41
  67. package/meta-object/validate.d.ts +8 -3
  68. package/meta-object/validate.js +20 -4
  69. package/package.json +4 -3
  70. package/query-builder.d.ts +11 -12
  71. package/query-builder.js +25 -29
  72. package/{types.d.ts → types/core.d.ts} +25 -18
  73. package/types/index.d.ts +12 -0
  74. package/types/index.js +34 -0
  75. package/types/introspection.d.ts +121 -0
  76. package/types/introspection.js +62 -0
  77. package/types/mutation.d.ts +45 -0
  78. package/types/mutation.js +5 -0
  79. package/types/query.d.ts +91 -0
  80. package/types/query.js +5 -0
  81. package/types/schema.d.ts +265 -0
  82. package/types/schema.js +6 -0
  83. package/types/selection.d.ts +43 -0
  84. package/types/selection.js +5 -0
  85. package/utils.d.ts +17 -0
  86. package/utils.js +72 -0
  87. /package/esm/{types.js → types/core.js} +0 -0
  88. /package/{types.js → types/core.js} +0 -0
@@ -0,0 +1,154 @@
1
+ /**
2
+ * Server-aware naming helpers for GraphQL query/mutation generation.
3
+ *
4
+ * These functions prefer names already discovered from the GraphQL schema
5
+ * (stored on `table.query` and `table.inflection` by `infer-tables.ts`)
6
+ * and fall back to local inflection when introspection data is unavailable.
7
+ *
8
+ * Back-ported from Dashboard's `packages/data/src/query-generator.ts`.
9
+ */
10
+ import { camelize, pluralize } from 'inflekt';
11
+ // ---------------------------------------------------------------------------
12
+ // Internal helpers
13
+ // ---------------------------------------------------------------------------
14
+ /**
15
+ * Safely normalise a server-provided inflection value.
16
+ * Returns `null` for null, undefined, or whitespace-only strings.
17
+ */
18
+ export function normalizeInflectionValue(value) {
19
+ if (typeof value !== 'string')
20
+ return null;
21
+ const trimmed = value.trim();
22
+ return trimmed.length > 0 ? trimmed : null;
23
+ }
24
+ // ---------------------------------------------------------------------------
25
+ // Plural / Singular
26
+ // ---------------------------------------------------------------------------
27
+ /**
28
+ * Convert PascalCase table name to camelCase plural for GraphQL queries.
29
+ * Prefers server-provided `table.query.all` / `table.inflection.allRows`
30
+ * when available, with guards against naive pluralisation drift and
31
+ * missing camelCase boundaries.
32
+ *
33
+ * Example: "ActionGoal" -> "actionGoals", "User" -> "users", "Person" -> "people"
34
+ */
35
+ export function toCamelCasePlural(tableName, table) {
36
+ const singular = camelize(tableName, true);
37
+ const inflectedPlural = pluralize(singular);
38
+ const serverPluralCandidates = [
39
+ table?.query?.all,
40
+ table?.inflection?.allRows,
41
+ ];
42
+ for (const candidateRaw of serverPluralCandidates) {
43
+ const candidate = normalizeInflectionValue(candidateRaw);
44
+ if (!candidate)
45
+ continue;
46
+ // Guard against known fallback drift:
47
+ // 1. Naive pluralisation: "activitys" instead of "activities"
48
+ const isNaivePlural = candidate === `${singular}s` && candidate !== inflectedPlural;
49
+ // 2. Missing camelCase boundaries: "deliveryzones" instead of "deliveryZones"
50
+ const isMiscased = candidate !== inflectedPlural &&
51
+ candidate.toLowerCase() === inflectedPlural.toLowerCase();
52
+ if (isNaivePlural || isMiscased)
53
+ continue;
54
+ return candidate;
55
+ }
56
+ return inflectedPlural;
57
+ }
58
+ /**
59
+ * Convert PascalCase table name to camelCase singular field name.
60
+ * Prefers server-provided names when available.
61
+ */
62
+ export function toCamelCaseSingular(tableName, table) {
63
+ const localSingular = camelize(tableName, true);
64
+ for (const candidateRaw of [
65
+ table?.query?.one,
66
+ table?.inflection?.tableFieldName,
67
+ ]) {
68
+ const candidate = normalizeInflectionValue(candidateRaw);
69
+ if (!candidate)
70
+ continue;
71
+ // Reject miscased versions: "deliveryzone" vs "deliveryZone"
72
+ if (candidate !== localSingular &&
73
+ candidate.toLowerCase() === localSingular.toLowerCase())
74
+ continue;
75
+ return candidate;
76
+ }
77
+ return localSingular;
78
+ }
79
+ // ---------------------------------------------------------------------------
80
+ // Mutation names
81
+ // ---------------------------------------------------------------------------
82
+ export function toCreateMutationName(tableName, table) {
83
+ return (normalizeInflectionValue(table?.query?.create) ?? `create${tableName}`);
84
+ }
85
+ export function toUpdateMutationName(tableName, table) {
86
+ return (normalizeInflectionValue(table?.query?.update) ?? `update${tableName}`);
87
+ }
88
+ export function toDeleteMutationName(tableName, table) {
89
+ return (normalizeInflectionValue(table?.query?.delete) ?? `delete${tableName}`);
90
+ }
91
+ // ---------------------------------------------------------------------------
92
+ // Input / type names
93
+ // ---------------------------------------------------------------------------
94
+ export function toCreateInputTypeName(tableName, table) {
95
+ return (normalizeInflectionValue(table?.inflection?.createInputType) ??
96
+ `Create${tableName}Input`);
97
+ }
98
+ export function toUpdateInputTypeName(tableName) {
99
+ return `Update${tableName}Input`;
100
+ }
101
+ export function toDeleteInputTypeName(tableName) {
102
+ return `Delete${tableName}Input`;
103
+ }
104
+ export function toFilterTypeName(tableName, table) {
105
+ return (normalizeInflectionValue(table?.inflection?.filterType) ??
106
+ `${tableName}Filter`);
107
+ }
108
+ // ---------------------------------------------------------------------------
109
+ // Patch field name
110
+ // ---------------------------------------------------------------------------
111
+ /**
112
+ * Resolve PostGraphile patch field name.
113
+ * In v5 this is typically entity-specific: e.g. "userPatch", "contactPatch".
114
+ * Prefers the value discovered from the schema (`table.query.patchFieldName`
115
+ * or `table.inflection.patchField`), falls back to `${singularName}Patch`.
116
+ */
117
+ export function toPatchFieldName(tableName, table) {
118
+ // First check the patch field name discovered from UpdateXxxInput
119
+ const introspectedPatch = normalizeInflectionValue(table?.query?.patchFieldName);
120
+ if (introspectedPatch)
121
+ return introspectedPatch;
122
+ // Then check the inflection table
123
+ const explicitPatchField = normalizeInflectionValue(table?.inflection?.patchField);
124
+ if (explicitPatchField)
125
+ return explicitPatchField;
126
+ return `${toCamelCaseSingular(tableName, table)}Patch`;
127
+ }
128
+ // ---------------------------------------------------------------------------
129
+ // OrderBy helpers
130
+ // ---------------------------------------------------------------------------
131
+ /**
132
+ * Convert camelCase field name to SCREAMING_SNAKE_CASE for PostGraphile
133
+ * orderBy enums.
134
+ *
135
+ * "displayName" -> "DISPLAY_NAME_ASC"
136
+ * "createdAt" -> "CREATED_AT_DESC"
137
+ * "id" -> "ID_ASC"
138
+ */
139
+ export function toOrderByEnumValue(fieldName, direction) {
140
+ const screaming = fieldName
141
+ .replace(/([a-z0-9])([A-Z])/g, '$1_$2')
142
+ .toUpperCase();
143
+ return `${screaming}_${direction.toUpperCase()}`;
144
+ }
145
+ /**
146
+ * Generate the PostGraphile OrderBy enum type name for a table.
147
+ * Prefers server-provided `table.inflection.orderByType` when available.
148
+ */
149
+ export function toOrderByTypeName(tableName, table) {
150
+ if (table?.inflection?.orderByType)
151
+ return table.inflection.orderByType;
152
+ const plural = toCamelCasePlural(tableName, table);
153
+ return `${plural.charAt(0).toUpperCase() + plural.slice(1)}OrderBy`;
154
+ }