@constructive-io/graphql-codegen 2.18.0 → 2.20.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 (301) hide show
  1. package/README.md +1818 -113
  2. package/__tests__/codegen/input-types-generator.test.d.ts +1 -0
  3. package/__tests__/codegen/input-types-generator.test.js +635 -0
  4. package/cli/codegen/barrel.d.ts +27 -0
  5. package/cli/codegen/barrel.js +163 -0
  6. package/cli/codegen/client.d.ts +4 -0
  7. package/cli/codegen/client.js +170 -0
  8. package/cli/codegen/custom-mutations.d.ts +38 -0
  9. package/cli/codegen/custom-mutations.js +149 -0
  10. package/cli/codegen/custom-queries.d.ts +38 -0
  11. package/cli/codegen/custom-queries.js +358 -0
  12. package/cli/codegen/filters.d.ts +27 -0
  13. package/cli/codegen/filters.js +357 -0
  14. package/cli/codegen/gql-ast.d.ts +41 -0
  15. package/cli/codegen/gql-ast.js +329 -0
  16. package/cli/codegen/index.d.ts +71 -0
  17. package/cli/codegen/index.js +147 -0
  18. package/cli/codegen/mutations.d.ts +30 -0
  19. package/cli/codegen/mutations.js +410 -0
  20. package/cli/codegen/orm/barrel.d.ts +18 -0
  21. package/cli/codegen/orm/barrel.js +48 -0
  22. package/cli/codegen/orm/client-generator.d.ts +45 -0
  23. package/cli/codegen/orm/client-generator.js +646 -0
  24. package/cli/codegen/orm/custom-ops-generator.d.ts +30 -0
  25. package/cli/codegen/orm/custom-ops-generator.js +350 -0
  26. package/cli/codegen/orm/index.d.ts +38 -0
  27. package/cli/codegen/orm/index.js +88 -0
  28. package/cli/codegen/orm/input-types-generator.d.ts +21 -0
  29. package/cli/codegen/orm/input-types-generator.js +705 -0
  30. package/cli/codegen/orm/input-types-generator.test.d.ts +1 -0
  31. package/cli/codegen/orm/input-types-generator.test.js +75 -0
  32. package/cli/codegen/orm/model-generator.d.ts +32 -0
  33. package/cli/codegen/orm/model-generator.js +264 -0
  34. package/cli/codegen/orm/query-builder.d.ts +161 -0
  35. package/cli/codegen/orm/query-builder.js +366 -0
  36. package/cli/codegen/orm/select-types.d.ts +169 -0
  37. package/cli/codegen/orm/select-types.js +16 -0
  38. package/cli/codegen/orm/select-types.test.d.ts +11 -0
  39. package/cli/codegen/orm/select-types.test.js +22 -0
  40. package/cli/codegen/queries.d.ts +25 -0
  41. package/cli/codegen/queries.js +438 -0
  42. package/cli/codegen/scalars.d.ts +12 -0
  43. package/cli/codegen/scalars.js +71 -0
  44. package/cli/codegen/schema-gql-ast.d.ts +51 -0
  45. package/cli/codegen/schema-gql-ast.js +385 -0
  46. package/cli/codegen/ts-ast.d.ts +122 -0
  47. package/cli/codegen/ts-ast.js +280 -0
  48. package/cli/codegen/type-resolver.d.ts +96 -0
  49. package/cli/codegen/type-resolver.js +246 -0
  50. package/cli/codegen/types.d.ts +12 -0
  51. package/cli/codegen/types.js +69 -0
  52. package/cli/codegen/utils.d.ts +163 -0
  53. package/cli/codegen/utils.js +326 -0
  54. package/cli/commands/generate-orm.d.ts +37 -0
  55. package/cli/commands/generate-orm.js +195 -0
  56. package/cli/commands/generate.d.ts +39 -0
  57. package/cli/commands/generate.js +299 -0
  58. package/cli/commands/index.d.ts +7 -0
  59. package/cli/commands/index.js +12 -0
  60. package/cli/commands/init.d.ts +35 -0
  61. package/cli/commands/init.js +176 -0
  62. package/cli/index.d.ts +4 -0
  63. package/cli/index.js +291 -0
  64. package/cli/introspect/fetch-meta.d.ts +31 -0
  65. package/cli/introspect/fetch-meta.js +108 -0
  66. package/cli/introspect/fetch-schema.d.ts +21 -0
  67. package/cli/introspect/fetch-schema.js +86 -0
  68. package/cli/introspect/index.d.ts +8 -0
  69. package/cli/introspect/index.js +16 -0
  70. package/cli/introspect/meta-query.d.ts +111 -0
  71. package/cli/introspect/meta-query.js +191 -0
  72. package/cli/introspect/schema-query.d.ts +20 -0
  73. package/cli/introspect/schema-query.js +123 -0
  74. package/cli/introspect/transform-schema.d.ts +74 -0
  75. package/cli/introspect/transform-schema.js +269 -0
  76. package/cli/introspect/transform-schema.test.d.ts +1 -0
  77. package/cli/introspect/transform-schema.test.js +67 -0
  78. package/cli/introspect/transform.d.ts +21 -0
  79. package/cli/introspect/transform.js +216 -0
  80. package/cli/watch/cache.d.ts +45 -0
  81. package/cli/watch/cache.js +111 -0
  82. package/cli/watch/debounce.d.ts +19 -0
  83. package/cli/watch/debounce.js +89 -0
  84. package/cli/watch/hash.d.ts +17 -0
  85. package/cli/watch/hash.js +48 -0
  86. package/cli/watch/index.d.ts +10 -0
  87. package/cli/watch/index.js +22 -0
  88. package/cli/watch/orchestrator.d.ts +63 -0
  89. package/cli/watch/orchestrator.js +228 -0
  90. package/cli/watch/poller.d.ts +65 -0
  91. package/cli/watch/poller.js +203 -0
  92. package/cli/watch/types.d.ts +67 -0
  93. package/cli/watch/types.js +5 -0
  94. package/client/error.d.ts +95 -0
  95. package/client/error.js +255 -0
  96. package/client/execute.d.ts +57 -0
  97. package/client/execute.js +124 -0
  98. package/client/index.d.ts +6 -0
  99. package/client/index.js +18 -0
  100. package/client/typed-document.d.ts +31 -0
  101. package/client/typed-document.js +44 -0
  102. package/core/ast.d.ts +10 -0
  103. package/core/ast.js +593 -0
  104. package/core/custom-ast.d.ts +35 -0
  105. package/core/custom-ast.js +204 -0
  106. package/core/index.d.ts +8 -0
  107. package/core/index.js +33 -0
  108. package/core/meta-object/convert.d.ts +65 -0
  109. package/core/meta-object/convert.js +63 -0
  110. package/core/meta-object/format.json +93 -0
  111. package/core/meta-object/index.d.ts +2 -0
  112. package/core/meta-object/index.js +18 -0
  113. package/core/meta-object/validate.d.ts +9 -0
  114. package/core/meta-object/validate.js +34 -0
  115. package/core/query-builder.d.ts +46 -0
  116. package/core/query-builder.js +412 -0
  117. package/core/types.d.ts +139 -0
  118. package/core/types.js +28 -0
  119. package/esm/__tests__/codegen/input-types-generator.test.d.ts +1 -0
  120. package/esm/__tests__/codegen/input-types-generator.test.js +633 -0
  121. package/esm/cli/codegen/barrel.d.ts +27 -0
  122. package/esm/cli/codegen/barrel.js +156 -0
  123. package/esm/cli/codegen/client.d.ts +4 -0
  124. package/esm/cli/codegen/client.js +167 -0
  125. package/esm/cli/codegen/custom-mutations.d.ts +38 -0
  126. package/esm/cli/codegen/custom-mutations.js +145 -0
  127. package/esm/cli/codegen/custom-queries.d.ts +38 -0
  128. package/esm/cli/codegen/custom-queries.js +354 -0
  129. package/esm/cli/codegen/filters.d.ts +27 -0
  130. package/esm/cli/codegen/filters.js +351 -0
  131. package/esm/cli/codegen/gql-ast.d.ts +41 -0
  132. package/esm/cli/codegen/gql-ast.js +288 -0
  133. package/esm/cli/codegen/index.d.ts +71 -0
  134. package/esm/cli/codegen/index.js +124 -0
  135. package/esm/cli/codegen/mutations.d.ts +30 -0
  136. package/esm/cli/codegen/mutations.js +404 -0
  137. package/esm/cli/codegen/orm/barrel.d.ts +18 -0
  138. package/esm/cli/codegen/orm/barrel.js +44 -0
  139. package/esm/cli/codegen/orm/client-generator.d.ts +45 -0
  140. package/esm/cli/codegen/orm/client-generator.js +640 -0
  141. package/esm/cli/codegen/orm/custom-ops-generator.d.ts +30 -0
  142. package/esm/cli/codegen/orm/custom-ops-generator.js +346 -0
  143. package/esm/cli/codegen/orm/index.d.ts +38 -0
  144. package/esm/cli/codegen/orm/index.js +75 -0
  145. package/esm/cli/codegen/orm/input-types-generator.d.ts +21 -0
  146. package/esm/cli/codegen/orm/input-types-generator.js +700 -0
  147. package/esm/cli/codegen/orm/input-types-generator.test.d.ts +1 -0
  148. package/esm/cli/codegen/orm/input-types-generator.test.js +73 -0
  149. package/esm/cli/codegen/orm/model-generator.d.ts +32 -0
  150. package/esm/cli/codegen/orm/model-generator.js +260 -0
  151. package/esm/cli/codegen/orm/query-builder.d.ts +161 -0
  152. package/esm/cli/codegen/orm/query-builder.js +353 -0
  153. package/esm/cli/codegen/orm/select-types.d.ts +169 -0
  154. package/esm/cli/codegen/orm/select-types.js +15 -0
  155. package/esm/cli/codegen/orm/select-types.test.d.ts +11 -0
  156. package/esm/cli/codegen/orm/select-types.test.js +21 -0
  157. package/esm/cli/codegen/queries.d.ts +25 -0
  158. package/esm/cli/codegen/queries.js +433 -0
  159. package/esm/cli/codegen/scalars.d.ts +12 -0
  160. package/esm/cli/codegen/scalars.js +66 -0
  161. package/esm/cli/codegen/schema-gql-ast.d.ts +51 -0
  162. package/esm/cli/codegen/schema-gql-ast.js +343 -0
  163. package/esm/cli/codegen/ts-ast.d.ts +122 -0
  164. package/esm/cli/codegen/ts-ast.js +260 -0
  165. package/esm/cli/codegen/type-resolver.d.ts +96 -0
  166. package/esm/cli/codegen/type-resolver.js +224 -0
  167. package/esm/cli/codegen/types.d.ts +12 -0
  168. package/esm/cli/codegen/types.js +65 -0
  169. package/esm/cli/codegen/utils.d.ts +163 -0
  170. package/esm/cli/codegen/utils.js +288 -0
  171. package/esm/cli/commands/generate-orm.d.ts +37 -0
  172. package/esm/cli/commands/generate-orm.js +192 -0
  173. package/esm/cli/commands/generate.d.ts +39 -0
  174. package/esm/cli/commands/generate.js +262 -0
  175. package/esm/cli/commands/index.d.ts +7 -0
  176. package/esm/cli/commands/index.js +5 -0
  177. package/esm/cli/commands/init.d.ts +35 -0
  178. package/esm/cli/commands/init.js +138 -0
  179. package/esm/cli/index.d.ts +4 -0
  180. package/esm/cli/index.js +256 -0
  181. package/esm/cli/introspect/fetch-meta.d.ts +31 -0
  182. package/esm/cli/introspect/fetch-meta.js +104 -0
  183. package/esm/cli/introspect/fetch-schema.d.ts +21 -0
  184. package/esm/cli/introspect/fetch-schema.js +83 -0
  185. package/esm/cli/introspect/index.d.ts +8 -0
  186. package/esm/cli/introspect/index.js +6 -0
  187. package/esm/cli/introspect/meta-query.d.ts +111 -0
  188. package/esm/cli/introspect/meta-query.js +188 -0
  189. package/esm/cli/introspect/schema-query.d.ts +20 -0
  190. package/esm/cli/introspect/schema-query.js +120 -0
  191. package/esm/cli/introspect/transform-schema.d.ts +74 -0
  192. package/esm/cli/introspect/transform-schema.js +259 -0
  193. package/esm/cli/introspect/transform-schema.test.d.ts +1 -0
  194. package/esm/cli/introspect/transform-schema.test.js +65 -0
  195. package/esm/cli/introspect/transform.d.ts +21 -0
  196. package/esm/cli/introspect/transform.js +210 -0
  197. package/esm/cli/watch/cache.d.ts +45 -0
  198. package/esm/cli/watch/cache.js +73 -0
  199. package/esm/cli/watch/debounce.d.ts +19 -0
  200. package/esm/cli/watch/debounce.js +85 -0
  201. package/esm/cli/watch/hash.d.ts +17 -0
  202. package/esm/cli/watch/hash.js +43 -0
  203. package/esm/cli/watch/index.d.ts +10 -0
  204. package/esm/cli/watch/index.js +8 -0
  205. package/esm/cli/watch/orchestrator.d.ts +63 -0
  206. package/esm/cli/watch/orchestrator.js +223 -0
  207. package/esm/cli/watch/poller.d.ts +65 -0
  208. package/esm/cli/watch/poller.js +198 -0
  209. package/esm/cli/watch/types.d.ts +67 -0
  210. package/esm/cli/watch/types.js +4 -0
  211. package/esm/client/error.d.ts +95 -0
  212. package/esm/client/error.js +249 -0
  213. package/esm/client/execute.d.ts +57 -0
  214. package/esm/client/execute.js +120 -0
  215. package/esm/client/index.d.ts +6 -0
  216. package/esm/client/index.js +6 -0
  217. package/esm/client/typed-document.d.ts +31 -0
  218. package/esm/client/typed-document.js +40 -0
  219. package/esm/core/ast.d.ts +10 -0
  220. package/esm/core/ast.js +549 -0
  221. package/esm/core/custom-ast.d.ts +35 -0
  222. package/esm/core/custom-ast.js +161 -0
  223. package/esm/core/index.d.ts +8 -0
  224. package/esm/core/index.js +12 -0
  225. package/esm/core/meta-object/convert.d.ts +65 -0
  226. package/esm/core/meta-object/convert.js +60 -0
  227. package/esm/core/meta-object/format.json +93 -0
  228. package/esm/core/meta-object/index.d.ts +2 -0
  229. package/esm/core/meta-object/index.js +2 -0
  230. package/esm/core/meta-object/validate.d.ts +9 -0
  231. package/esm/core/meta-object/validate.js +28 -0
  232. package/esm/core/query-builder.d.ts +46 -0
  233. package/esm/core/query-builder.js +375 -0
  234. package/esm/core/types.d.ts +139 -0
  235. package/esm/core/types.js +24 -0
  236. package/esm/generators/field-selector.d.ts +30 -0
  237. package/esm/generators/field-selector.js +355 -0
  238. package/esm/generators/index.d.ts +6 -0
  239. package/esm/generators/index.js +9 -0
  240. package/esm/generators/mutations.d.ts +31 -0
  241. package/esm/generators/mutations.js +197 -0
  242. package/esm/generators/select.d.ts +50 -0
  243. package/esm/generators/select.js +636 -0
  244. package/esm/index.d.ts +12 -0
  245. package/esm/index.js +17 -3
  246. package/esm/react/index.d.ts +5 -0
  247. package/esm/react/index.js +6 -0
  248. package/esm/types/config.d.ts +199 -0
  249. package/esm/types/config.js +106 -0
  250. package/esm/types/index.d.ts +9 -0
  251. package/esm/types/index.js +4 -0
  252. package/esm/types/introspection.d.ts +121 -0
  253. package/esm/types/introspection.js +54 -0
  254. package/esm/types/mutation.d.ts +45 -0
  255. package/esm/types/mutation.js +4 -0
  256. package/esm/types/query.d.ts +82 -0
  257. package/esm/types/query.js +4 -0
  258. package/esm/types/schema.d.ts +253 -0
  259. package/esm/types/schema.js +5 -0
  260. package/esm/types/selection.d.ts +43 -0
  261. package/esm/types/selection.js +4 -0
  262. package/esm/utils/index.d.ts +4 -0
  263. package/esm/utils/index.js +4 -0
  264. package/generators/field-selector.d.ts +30 -0
  265. package/generators/field-selector.js +361 -0
  266. package/generators/index.d.ts +6 -0
  267. package/generators/index.js +27 -0
  268. package/generators/mutations.d.ts +31 -0
  269. package/generators/mutations.js +235 -0
  270. package/generators/select.d.ts +50 -0
  271. package/generators/select.js +679 -0
  272. package/index.d.ts +12 -3
  273. package/index.js +19 -3
  274. package/package.json +59 -38
  275. package/react/index.d.ts +5 -0
  276. package/react/index.js +9 -0
  277. package/types/config.d.ts +199 -0
  278. package/types/config.js +111 -0
  279. package/types/index.d.ts +9 -0
  280. package/types/index.js +10 -0
  281. package/types/introspection.d.ts +121 -0
  282. package/types/introspection.js +62 -0
  283. package/types/mutation.d.ts +45 -0
  284. package/types/mutation.js +5 -0
  285. package/types/query.d.ts +82 -0
  286. package/types/query.js +5 -0
  287. package/types/schema.d.ts +253 -0
  288. package/types/schema.js +6 -0
  289. package/types/selection.d.ts +43 -0
  290. package/types/selection.js +5 -0
  291. package/utils/index.d.ts +4 -0
  292. package/utils/index.js +7 -0
  293. package/codegen.d.ts +0 -13
  294. package/codegen.js +0 -293
  295. package/esm/codegen.js +0 -253
  296. package/esm/gql.js +0 -939
  297. package/esm/options.js +0 -27
  298. package/gql.d.ts +0 -188
  299. package/gql.js +0 -992
  300. package/options.d.ts +0 -45
  301. package/options.js +0 -31
@@ -0,0 +1,204 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.getCustomAst = getCustomAst;
37
+ exports.getCustomAstForCleanField = getCustomAstForCleanField;
38
+ exports.requiresSubfieldSelection = requiresSubfieldSelection;
39
+ exports.geometryPointAst = geometryPointAst;
40
+ exports.geometryCollectionAst = geometryCollectionAst;
41
+ exports.geometryAst = geometryAst;
42
+ exports.intervalAst = intervalAst;
43
+ exports.isIntervalType = isIntervalType;
44
+ const t = __importStar(require("gql-ast"));
45
+ /**
46
+ * Get custom AST for MetaField type - handles PostgreSQL types that need subfield selections
47
+ */
48
+ function getCustomAst(fieldDefn) {
49
+ if (!fieldDefn) {
50
+ return null;
51
+ }
52
+ const { pgType } = fieldDefn.type;
53
+ if (pgType === 'geometry') {
54
+ return geometryAst(fieldDefn.name);
55
+ }
56
+ if (pgType === 'interval') {
57
+ return intervalAst(fieldDefn.name);
58
+ }
59
+ return t.field({
60
+ name: fieldDefn.name,
61
+ });
62
+ }
63
+ /**
64
+ * Generate custom AST for CleanField type - handles GraphQL types that need subfield selections
65
+ */
66
+ function getCustomAstForCleanField(field) {
67
+ const { name, type } = field;
68
+ const { gqlType, pgType } = type;
69
+ // Handle by GraphQL type first (this is what the error messages reference)
70
+ if (gqlType === 'GeometryPoint') {
71
+ return geometryPointAst(name);
72
+ }
73
+ if (gqlType === 'Interval') {
74
+ return intervalAst(name);
75
+ }
76
+ if (gqlType === 'GeometryGeometryCollection') {
77
+ return geometryCollectionAst(name);
78
+ }
79
+ // Handle by pgType as fallback
80
+ if (pgType === 'geometry') {
81
+ return geometryAst(name);
82
+ }
83
+ if (pgType === 'interval') {
84
+ return intervalAst(name);
85
+ }
86
+ // Return simple field for scalar types
87
+ return t.field({
88
+ name,
89
+ });
90
+ }
91
+ /**
92
+ * Check if a CleanField requires subfield selection based on its GraphQL type
93
+ */
94
+ function requiresSubfieldSelection(field) {
95
+ const { gqlType } = field.type;
96
+ // Complex GraphQL types that require subfield selection
97
+ const complexTypes = [
98
+ 'GeometryPoint',
99
+ 'Interval',
100
+ 'GeometryGeometryCollection',
101
+ 'GeoJSON',
102
+ ];
103
+ return complexTypes.includes(gqlType);
104
+ }
105
+ /**
106
+ * Generate AST for GeometryPoint type
107
+ */
108
+ function geometryPointAst(name) {
109
+ return t.field({
110
+ name,
111
+ selectionSet: t.selectionSet({
112
+ selections: toFieldArray(['x', 'y']),
113
+ }),
114
+ });
115
+ }
116
+ /**
117
+ * Generate AST for GeometryGeometryCollection type
118
+ */
119
+ function geometryCollectionAst(name) {
120
+ // Manually create inline fragment since gql-ast doesn't support it
121
+ const inlineFragment = {
122
+ kind: 'InlineFragment',
123
+ typeCondition: {
124
+ kind: 'NamedType',
125
+ name: {
126
+ kind: 'Name',
127
+ value: 'GeometryPoint',
128
+ },
129
+ },
130
+ selectionSet: {
131
+ kind: 'SelectionSet',
132
+ selections: [
133
+ {
134
+ kind: 'Field',
135
+ name: {
136
+ kind: 'Name',
137
+ value: 'x',
138
+ },
139
+ },
140
+ {
141
+ kind: 'Field',
142
+ name: {
143
+ kind: 'Name',
144
+ value: 'y',
145
+ },
146
+ },
147
+ ],
148
+ },
149
+ };
150
+ return t.field({
151
+ name,
152
+ selectionSet: t.selectionSet({
153
+ selections: [
154
+ t.field({
155
+ name: 'geometries',
156
+ selectionSet: t.selectionSet({
157
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
158
+ selections: [inlineFragment], // gql-ast limitation with inline fragments
159
+ }),
160
+ }),
161
+ ],
162
+ }),
163
+ });
164
+ }
165
+ /**
166
+ * Generate AST for generic geometry type (returns geojson)
167
+ */
168
+ function geometryAst(name) {
169
+ return t.field({
170
+ name,
171
+ selectionSet: t.selectionSet({
172
+ selections: toFieldArray(['geojson']),
173
+ }),
174
+ });
175
+ }
176
+ /**
177
+ * Generate AST for interval type
178
+ */
179
+ function intervalAst(name) {
180
+ return t.field({
181
+ name,
182
+ selectionSet: t.selectionSet({
183
+ selections: toFieldArray([
184
+ 'days',
185
+ 'hours',
186
+ 'minutes',
187
+ 'months',
188
+ 'seconds',
189
+ 'years',
190
+ ]),
191
+ }),
192
+ });
193
+ }
194
+ function toFieldArray(strArr) {
195
+ return strArr.map((fieldName) => t.field({ name: fieldName }));
196
+ }
197
+ /**
198
+ * Check if an object has interval type shape
199
+ */
200
+ function isIntervalType(obj) {
201
+ if (!obj || typeof obj !== 'object')
202
+ return false;
203
+ return ['days', 'hours', 'minutes', 'months', 'seconds', 'years'].every((key) => Object.prototype.hasOwnProperty.call(obj, key));
204
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Core query building exports
3
+ */
4
+ export * from './types';
5
+ export * from './ast';
6
+ export * from './custom-ast';
7
+ export { QueryBuilder, MetaObject } from './query-builder';
8
+ export { validateMetaObject, convertFromMetaSchema } from './meta-object';
package/core/index.js ADDED
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ /**
3
+ * Core query building exports
4
+ */
5
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ var desc = Object.getOwnPropertyDescriptor(m, k);
8
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
9
+ desc = { enumerable: true, get: function() { return m[k]; } };
10
+ }
11
+ Object.defineProperty(o, k2, desc);
12
+ }) : (function(o, m, k, k2) {
13
+ if (k2 === undefined) k2 = k;
14
+ o[k2] = m[k];
15
+ }));
16
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
17
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
18
+ };
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ exports.convertFromMetaSchema = exports.validateMetaObject = exports.MetaObject = exports.QueryBuilder = void 0;
21
+ // Types
22
+ __exportStar(require("./types"), exports);
23
+ // AST generation
24
+ __exportStar(require("./ast"), exports);
25
+ __exportStar(require("./custom-ast"), exports);
26
+ // Query builder
27
+ var query_builder_1 = require("./query-builder");
28
+ Object.defineProperty(exports, "QueryBuilder", { enumerable: true, get: function () { return query_builder_1.QueryBuilder; } });
29
+ Object.defineProperty(exports, "MetaObject", { enumerable: true, get: function () { return query_builder_1.MetaObject; } });
30
+ // Meta object utilities
31
+ var meta_object_1 = require("./meta-object");
32
+ Object.defineProperty(exports, "validateMetaObject", { enumerable: true, get: function () { return meta_object_1.validateMetaObject; } });
33
+ Object.defineProperty(exports, "convertFromMetaSchema", { enumerable: true, get: function () { return meta_object_1.convertFromMetaSchema; } });
@@ -0,0 +1,65 @@
1
+ import type { MetaFieldType } from '../types';
2
+ interface MetaSchemaField {
3
+ name: string;
4
+ type: MetaFieldType;
5
+ }
6
+ interface MetaSchemaConstraint {
7
+ fields: MetaSchemaField[];
8
+ }
9
+ interface MetaSchemaForeignConstraint {
10
+ fields: MetaSchemaField[];
11
+ refFields: MetaSchemaField[];
12
+ refTable: {
13
+ name: string;
14
+ };
15
+ }
16
+ interface MetaSchemaBelongsTo {
17
+ keys: MetaSchemaField[];
18
+ fieldName: string;
19
+ }
20
+ interface MetaSchemaRelations {
21
+ belongsTo: MetaSchemaBelongsTo[];
22
+ }
23
+ interface MetaSchemaTable {
24
+ name: string;
25
+ fields: MetaSchemaField[];
26
+ primaryKeyConstraints: MetaSchemaConstraint[];
27
+ uniqueConstraints: MetaSchemaConstraint[];
28
+ foreignKeyConstraints: MetaSchemaForeignConstraint[];
29
+ relations: MetaSchemaRelations;
30
+ }
31
+ interface MetaSchemaInput {
32
+ _meta: {
33
+ tables: MetaSchemaTable[];
34
+ };
35
+ }
36
+ interface ConvertedField {
37
+ name: string;
38
+ type: MetaFieldType;
39
+ alias?: string;
40
+ }
41
+ interface ConvertedConstraint {
42
+ name: string;
43
+ type: MetaFieldType;
44
+ alias?: string;
45
+ }
46
+ interface ConvertedForeignConstraint {
47
+ refTable: string;
48
+ fromKey: ConvertedField;
49
+ toKey: ConvertedField;
50
+ }
51
+ interface ConvertedTable {
52
+ name: string;
53
+ fields: ConvertedField[];
54
+ primaryConstraints: ConvertedConstraint[];
55
+ uniqueConstraints: ConvertedConstraint[];
56
+ foreignConstraints: ConvertedForeignConstraint[];
57
+ }
58
+ interface ConvertedMetaObject {
59
+ tables: ConvertedTable[];
60
+ }
61
+ /**
62
+ * Convert from raw _meta schema response to internal MetaObject format
63
+ */
64
+ export declare function convertFromMetaSchema(metaSchema: MetaSchemaInput): ConvertedMetaObject;
65
+ export {};
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.convertFromMetaSchema = convertFromMetaSchema;
4
+ /**
5
+ * Convert from raw _meta schema response to internal MetaObject format
6
+ */
7
+ function convertFromMetaSchema(metaSchema) {
8
+ const { _meta: { tables }, } = metaSchema;
9
+ const result = {
10
+ tables: [],
11
+ };
12
+ for (const table of tables) {
13
+ result.tables.push({
14
+ name: table.name,
15
+ fields: table.fields.map((f) => pickField(f)),
16
+ primaryConstraints: pickArrayConstraint(table.primaryKeyConstraints),
17
+ uniqueConstraints: pickArrayConstraint(table.uniqueConstraints),
18
+ foreignConstraints: pickForeignConstraint(table.foreignKeyConstraints, table.relations),
19
+ });
20
+ }
21
+ return result;
22
+ }
23
+ function pickArrayConstraint(constraints) {
24
+ if (constraints.length === 0)
25
+ return [];
26
+ const c = constraints[0];
27
+ return c.fields.map((field) => pickConstraintField(field));
28
+ }
29
+ function pickForeignConstraint(constraints, relations) {
30
+ if (constraints.length === 0)
31
+ return [];
32
+ const { belongsTo } = relations;
33
+ return constraints.map((c) => {
34
+ const { fields, refFields, refTable } = c;
35
+ const fromKey = pickField(fields[0]);
36
+ const toKey = pickField(refFields[0]);
37
+ const matchingBelongsTo = belongsTo.find((belongsToItem) => {
38
+ const field = pickField(belongsToItem.keys[0]);
39
+ return field.name === fromKey.name;
40
+ });
41
+ // Ex: 'ownerId' will have an alias of 'owner', which has further selection of 'User' type
42
+ if (matchingBelongsTo) {
43
+ fromKey.alias = matchingBelongsTo.fieldName;
44
+ }
45
+ return {
46
+ refTable: refTable.name,
47
+ fromKey,
48
+ toKey,
49
+ };
50
+ });
51
+ }
52
+ function pickField(field) {
53
+ return {
54
+ name: field.name,
55
+ type: field.type,
56
+ };
57
+ }
58
+ function pickConstraintField(field) {
59
+ return {
60
+ name: field.name,
61
+ type: field.type,
62
+ };
63
+ }
@@ -0,0 +1,93 @@
1
+ {
2
+ "$id": "root",
3
+ "$schema": "http://json-schema.org/draft-07/schema#",
4
+ "type": "object",
5
+ "definitions": {
6
+ "field": {
7
+ "type": "object",
8
+ "required": ["name", "type"],
9
+ "additionalProperties": true,
10
+ "properties": {
11
+ "name": {
12
+ "type": "string",
13
+ "default": "id",
14
+ "examples": ["id"]
15
+ },
16
+ "type": {
17
+ "type": "object",
18
+ "required": ["pgType", "gqlType"],
19
+ "additionalProperties": true,
20
+ "properties": {
21
+ "gqlType": {
22
+ "type": ["string", "null"]
23
+ },
24
+ "pgType": {
25
+ "type": ["string", "null"]
26
+ },
27
+ "subtype": {
28
+ "type": ["string", "null"]
29
+ }
30
+ }
31
+ }
32
+ }
33
+ }
34
+ },
35
+ "properties": {
36
+ "tables": {
37
+ "type": "array",
38
+ "items": {
39
+ "type": "object",
40
+ "properties": {
41
+ "name": {
42
+ "type": "string",
43
+ "default": "User",
44
+ "examples": ["User"]
45
+ },
46
+ "fields": {
47
+ "type": "array",
48
+ "items": {
49
+ "$ref": "#/definitions/field"
50
+ }
51
+ },
52
+ "primaryConstraints": {
53
+ "type": "array",
54
+ "items": {
55
+ "$ref": "#/definitions/field"
56
+ }
57
+ },
58
+ "uniqueConstraints": {
59
+ "type": "array",
60
+ "items": {
61
+ "$ref": "#/definitions/field"
62
+ }
63
+ },
64
+ "foreignConstraints": {
65
+ "type": "array",
66
+ "items": {
67
+ "type": "object",
68
+ "properties": {
69
+ "refTable": {
70
+ "type": "string",
71
+ "default": "User",
72
+ "examples": ["User"]
73
+ },
74
+ "fromKey": {
75
+ "$ref": "#/definitions/field"
76
+ },
77
+ "toKey": {
78
+ "$ref": "#/definitions/field"
79
+ }
80
+ },
81
+ "required": ["refTable", "fromKey", "toKey"],
82
+ "additionalProperties": false
83
+ }
84
+ }
85
+ },
86
+ "required": ["name", "fields"],
87
+ "additionalProperties": false
88
+ }
89
+ }
90
+ },
91
+ "required": ["tables"],
92
+ "additionalProperties": false
93
+ }
@@ -0,0 +1,2 @@
1
+ export * from './convert';
2
+ export * from './validate';
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./convert"), exports);
18
+ __exportStar(require("./validate"), exports);
@@ -0,0 +1,9 @@
1
+ export interface ValidationResult {
2
+ errors: unknown[] | null | undefined;
3
+ message: string;
4
+ }
5
+ /**
6
+ * Validate a MetaObject against the JSON schema
7
+ * @returns true if valid, or an object with errors and message if invalid
8
+ */
9
+ export declare function validateMetaObject(obj: unknown): true | ValidationResult;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.validateMetaObject = validateMetaObject;
7
+ const ajv_1 = __importDefault(require("ajv"));
8
+ const format_json_1 = __importDefault(require("./format.json"));
9
+ let cachedAjv = null;
10
+ let cachedValidator = null;
11
+ function getValidator() {
12
+ if (!cachedAjv) {
13
+ cachedAjv = new ajv_1.default({ allErrors: true });
14
+ cachedValidator = cachedAjv.compile(format_json_1.default);
15
+ }
16
+ return {
17
+ ajv: cachedAjv,
18
+ validator: cachedValidator,
19
+ };
20
+ }
21
+ /**
22
+ * Validate a MetaObject against the JSON schema
23
+ * @returns true if valid, or an object with errors and message if invalid
24
+ */
25
+ function validateMetaObject(obj) {
26
+ const { ajv, validator } = getValidator();
27
+ const valid = validator(obj);
28
+ if (valid)
29
+ return true;
30
+ return {
31
+ errors: validator.errors,
32
+ message: ajv.errorsText(validator.errors, { separator: '\n' }),
33
+ };
34
+ }
@@ -0,0 +1,46 @@
1
+ import type { QueryFieldSelection, IntrospectionSchema, MetaObject, QueryBuilderOptions, QueryBuilderResult, QueryDefinition, QuerySelectionOptions } from './types';
2
+ export * as MetaObject from './meta-object';
3
+ export declare class QueryBuilder {
4
+ _introspection: IntrospectionSchema;
5
+ _meta: MetaObject;
6
+ private _models;
7
+ private _model;
8
+ private _key;
9
+ private _queryName;
10
+ private _ast;
11
+ _edges: boolean;
12
+ private _op;
13
+ private _mutation;
14
+ private _select;
15
+ constructor({ meta, introspection, }: QueryBuilderOptions);
16
+ initModelMap(): void;
17
+ clear(): void;
18
+ query(model: string): QueryBuilder;
19
+ _findQuery(): string;
20
+ _findMutation(): string;
21
+ select(selection?: QuerySelectionOptions | null): QueryBuilder;
22
+ edges(useEdges: boolean): QueryBuilder;
23
+ getMany({ select }?: {
24
+ select?: QuerySelectionOptions;
25
+ }): QueryBuilder;
26
+ all({ select }?: {
27
+ select?: QuerySelectionOptions;
28
+ }): QueryBuilder;
29
+ count(): QueryBuilder;
30
+ getOne({ select }?: {
31
+ select?: QuerySelectionOptions;
32
+ }): QueryBuilder;
33
+ create({ select }?: {
34
+ select?: QuerySelectionOptions;
35
+ }): QueryBuilder;
36
+ delete({ select }?: {
37
+ select?: QuerySelectionOptions;
38
+ }): QueryBuilder;
39
+ update({ select }?: {
40
+ select?: QuerySelectionOptions;
41
+ }): QueryBuilder;
42
+ queryName(name: string): QueryBuilder;
43
+ print(): QueryBuilderResult;
44
+ pickScalarFields: (selection: QuerySelectionOptions | null, defn: QueryDefinition) => QueryFieldSelection[];
45
+ pickAllFields: (selection: QuerySelectionOptions, defn: QueryDefinition) => QueryFieldSelection[];
46
+ }