@constructive-io/graphql-codegen 2.19.0 → 2.20.1

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,412 @@
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.QueryBuilder = exports.MetaObject = void 0;
37
+ const graphql_1 = require("graphql");
38
+ const inflection = __importStar(require("inflection"));
39
+ const ast_1 = require("./ast");
40
+ const meta_object_1 = require("./meta-object");
41
+ exports.MetaObject = __importStar(require("./meta-object"));
42
+ const isObject = (val) => val !== null && typeof val === 'object';
43
+ class QueryBuilder {
44
+ _introspection;
45
+ _meta;
46
+ _models;
47
+ _model;
48
+ _key;
49
+ _queryName;
50
+ _ast;
51
+ _edges;
52
+ _op;
53
+ _mutation;
54
+ _select;
55
+ constructor({ meta = {}, introspection, }) {
56
+ this._introspection = introspection;
57
+ this._meta = meta;
58
+ this.clear();
59
+ this.initModelMap();
60
+ this.pickScalarFields = pickScalarFields.bind(this);
61
+ this.pickAllFields = pickAllFields.bind(this);
62
+ const result = (0, meta_object_1.validateMetaObject)(this._meta);
63
+ if (typeof result === 'object' && result.errors) {
64
+ throw new Error(`QueryBuilder: meta object is invalid:\n${result.message}`);
65
+ }
66
+ }
67
+ /*
68
+ * Save all gql queries and mutations by model name for quicker lookup
69
+ */
70
+ initModelMap() {
71
+ this._models = {};
72
+ for (const [key, defn] of Object.entries(this._introspection)) {
73
+ if (!this._models[defn.model]) {
74
+ this._models[defn.model] = {};
75
+ }
76
+ this._models[defn.model][key] = defn;
77
+ }
78
+ }
79
+ clear() {
80
+ this._model = '';
81
+ this._key = null;
82
+ this._queryName = '';
83
+ this._ast = null;
84
+ this._edges = false;
85
+ this._op = '';
86
+ this._mutation = '';
87
+ this._select = [];
88
+ }
89
+ query(model) {
90
+ this.clear();
91
+ this._model = model;
92
+ return this;
93
+ }
94
+ _findQuery() {
95
+ // based on the op, finds the relevant GQL query
96
+ const queries = this._models[this._model];
97
+ if (!queries) {
98
+ throw new Error('No queries found for ' + this._model);
99
+ }
100
+ const matchQuery = Object.entries(queries).find(([_, defn]) => defn.qtype === this._op);
101
+ if (!matchQuery) {
102
+ throw new Error('No query found for ' + this._model + ':' + this._op);
103
+ }
104
+ const queryKey = matchQuery[0];
105
+ return queryKey;
106
+ }
107
+ _findMutation() {
108
+ // For mutation, there can be many defns that match the operation being requested
109
+ // .ie: deleteAction, deleteActionBySlug, deleteActionByName
110
+ const matchingDefns = Object.keys(this._introspection).reduce((arr, mutationKey) => {
111
+ const defn = this._introspection[mutationKey];
112
+ if (defn.model === this._model &&
113
+ defn.qtype === this._op &&
114
+ defn.qtype === 'mutation' &&
115
+ defn.mutationType === this._mutation) {
116
+ arr = [...arr, { defn, mutationKey }];
117
+ }
118
+ return arr;
119
+ }, []);
120
+ if (matchingDefns.length === 0) {
121
+ throw new Error('no mutation found for ' + this._model + ':' + this._mutation);
122
+ }
123
+ // We only need deleteAction from all of [deleteAction, deleteActionBySlug, deleteActionByName]
124
+ const getInputName = (mutationType) => {
125
+ switch (mutationType) {
126
+ case 'delete': {
127
+ return `Delete${inflection.camelize(this._model)}Input`;
128
+ }
129
+ case 'create': {
130
+ return `Create${inflection.camelize(this._model)}Input`;
131
+ }
132
+ case 'patch': {
133
+ return `Update${inflection.camelize(this._model)}Input`;
134
+ }
135
+ default:
136
+ throw new Error('Unhandled mutation type' + mutationType);
137
+ }
138
+ };
139
+ const matchDefn = matchingDefns.find(({ defn }) => defn.properties.input.type === getInputName(this._mutation));
140
+ if (!matchDefn) {
141
+ throw new Error('no mutation found for ' + this._model + ':' + this._mutation);
142
+ }
143
+ return matchDefn.mutationKey;
144
+ }
145
+ select(selection) {
146
+ const defn = this._introspection[this._key];
147
+ // If selection not given, pick only scalar fields
148
+ if (selection == null) {
149
+ this._select = this.pickScalarFields(null, defn);
150
+ return this;
151
+ }
152
+ this._select = this.pickAllFields(selection, defn);
153
+ return this;
154
+ }
155
+ edges(useEdges) {
156
+ this._edges = useEdges;
157
+ return this;
158
+ }
159
+ getMany({ select } = {}) {
160
+ this._op = 'getMany';
161
+ this._key = this._findQuery();
162
+ this.queryName(inflection.camelize(['get', inflection.underscore(this._key), 'query'].join('_'), true));
163
+ const defn = this._introspection[this._key];
164
+ this.select(select);
165
+ this._ast = (0, ast_1.getMany)({
166
+ builder: this,
167
+ queryName: this._queryName,
168
+ operationName: this._key,
169
+ query: defn,
170
+ selection: this._select,
171
+ });
172
+ return this;
173
+ }
174
+ all({ select } = {}) {
175
+ this._op = 'getMany';
176
+ this._key = this._findQuery();
177
+ this.queryName(inflection.camelize(['get', inflection.underscore(this._key), 'query', 'all'].join('_'), true));
178
+ const defn = this._introspection[this._key];
179
+ this.select(select);
180
+ this._ast = (0, ast_1.getAll)({
181
+ queryName: this._queryName,
182
+ operationName: this._key,
183
+ query: defn,
184
+ selection: this._select,
185
+ });
186
+ return this;
187
+ }
188
+ count() {
189
+ this._op = 'getMany';
190
+ this._key = this._findQuery();
191
+ this.queryName(inflection.camelize(['get', inflection.underscore(this._key), 'count', 'query'].join('_'), true));
192
+ const defn = this._introspection[this._key];
193
+ this._ast = (0, ast_1.getCount)({
194
+ queryName: this._queryName,
195
+ operationName: this._key,
196
+ query: defn,
197
+ });
198
+ return this;
199
+ }
200
+ getOne({ select } = {}) {
201
+ this._op = 'getOne';
202
+ this._key = this._findQuery();
203
+ this.queryName(inflection.camelize(['get', inflection.underscore(this._key), 'query'].join('_'), true));
204
+ const defn = this._introspection[this._key];
205
+ this.select(select);
206
+ this._ast = (0, ast_1.getOne)({
207
+ builder: this,
208
+ queryName: this._queryName,
209
+ operationName: this._key,
210
+ query: defn,
211
+ selection: this._select,
212
+ });
213
+ return this;
214
+ }
215
+ create({ select } = {}) {
216
+ this._op = 'mutation';
217
+ this._mutation = 'create';
218
+ this._key = this._findMutation();
219
+ this.queryName(inflection.camelize([inflection.underscore(this._key), 'mutation'].join('_'), true));
220
+ const defn = this._introspection[this._key];
221
+ this.select(select);
222
+ this._ast = (0, ast_1.createOne)({
223
+ operationName: this._key,
224
+ mutationName: this._queryName,
225
+ mutation: defn,
226
+ selection: this._select,
227
+ });
228
+ return this;
229
+ }
230
+ delete({ select } = {}) {
231
+ this._op = 'mutation';
232
+ this._mutation = 'delete';
233
+ this._key = this._findMutation();
234
+ this.queryName(inflection.camelize([inflection.underscore(this._key), 'mutation'].join('_'), true));
235
+ const defn = this._introspection[this._key];
236
+ this.select(select);
237
+ this._ast = (0, ast_1.deleteOne)({
238
+ operationName: this._key,
239
+ mutationName: this._queryName,
240
+ mutation: defn,
241
+ });
242
+ return this;
243
+ }
244
+ update({ select } = {}) {
245
+ this._op = 'mutation';
246
+ this._mutation = 'patch';
247
+ this._key = this._findMutation();
248
+ this.queryName(inflection.camelize([inflection.underscore(this._key), 'mutation'].join('_'), true));
249
+ const defn = this._introspection[this._key];
250
+ this.select(select);
251
+ this._ast = (0, ast_1.patchOne)({
252
+ operationName: this._key,
253
+ mutationName: this._queryName,
254
+ mutation: defn,
255
+ selection: this._select,
256
+ });
257
+ return this;
258
+ }
259
+ queryName(name) {
260
+ this._queryName = name;
261
+ return this;
262
+ }
263
+ print() {
264
+ if (!this._ast) {
265
+ throw new Error('No AST generated. Please call a query method first.');
266
+ }
267
+ const _hash = (0, graphql_1.print)(this._ast);
268
+ return {
269
+ _hash,
270
+ _queryName: this._queryName,
271
+ _ast: this._ast,
272
+ };
273
+ }
274
+ // Bind methods that will be called with different this context
275
+ pickScalarFields;
276
+ pickAllFields;
277
+ }
278
+ exports.QueryBuilder = QueryBuilder;
279
+ /**
280
+ * Pick scalar fields of a query definition
281
+ * @param {Object} defn Query definition
282
+ * @param {Object} meta Meta object containing info about table relations
283
+ * @returns {Array}
284
+ */
285
+ function pickScalarFields(selection, defn) {
286
+ const model = defn.model;
287
+ const modelMeta = this._meta.tables.find((t) => t.name === model);
288
+ if (!modelMeta) {
289
+ throw new Error(`Model meta not found for ${model}`);
290
+ }
291
+ const isInTableSchema = (fieldName) => !!modelMeta.fields.find((field) => field.name === fieldName);
292
+ const pickFrom = (modelSelection) => modelSelection
293
+ .filter((fieldName) => {
294
+ // If not specified or not a valid selection list, allow all
295
+ if (selection == null || !Array.isArray(selection))
296
+ return true;
297
+ return Object.keys(selection).includes(fieldName);
298
+ })
299
+ .filter((fieldName) => !isRelationalField(fieldName, modelMeta) &&
300
+ isInTableSchema(fieldName))
301
+ .map((fieldName) => ({
302
+ name: fieldName,
303
+ isObject: false,
304
+ fieldDefn: modelMeta.fields.find((f) => f.name === fieldName),
305
+ }));
306
+ // This is for inferring the sub-selection of a mutation query
307
+ // from a definition model .eg UserSetting, find its related queries in the introspection object, and pick its selection fields
308
+ if (defn.qtype === 'mutation') {
309
+ const relatedQuery = this._introspection[`${modelNameToGetMany(defn.model)}`];
310
+ return pickFrom(relatedQuery.selection);
311
+ }
312
+ return pickFrom(defn.selection);
313
+ }
314
+ /**
315
+ * Pick scalar fields and sub-selection fields of a query definition
316
+ * @param {Object} selection Selection clause object
317
+ * @param {Object} defn Query definition
318
+ * @param {Object} meta Meta object containing info about table relations
319
+ * @returns {Array}
320
+ */
321
+ function pickAllFields(selection, defn) {
322
+ const model = defn.model;
323
+ const modelMeta = this._meta.tables.find((t) => t.name === model);
324
+ if (!modelMeta) {
325
+ throw new Error(`Model meta not found for ${model}`);
326
+ }
327
+ const selectionEntries = Object.entries(selection);
328
+ let fields = [];
329
+ const isWhiteListed = (selectValue) => {
330
+ return typeof selectValue === 'boolean' && selectValue;
331
+ };
332
+ for (const entry of selectionEntries) {
333
+ const [fieldName, fieldOptions] = entry;
334
+ // Case
335
+ // {
336
+ // goalResults: // fieldName
337
+ // { select: { id: true }, variables: { first: 100 } } // fieldOptions
338
+ // }
339
+ if (isObject(fieldOptions)) {
340
+ if (!isFieldInDefinition(fieldName, defn, modelMeta)) {
341
+ continue;
342
+ }
343
+ const referencedForeignConstraint = modelMeta.foreignConstraints.find((constraint) => constraint.fromKey.name === fieldName ||
344
+ constraint.fromKey.alias === fieldName);
345
+ const selectOptions = fieldOptions;
346
+ const subFields = Object.keys(selectOptions.select).filter((subField) => {
347
+ return (!isRelationalField(subField, modelMeta) &&
348
+ isWhiteListed(selectOptions.select[subField]));
349
+ });
350
+ const isBelongTo = !!referencedForeignConstraint;
351
+ const fieldSelection = {
352
+ name: fieldName,
353
+ isObject: true,
354
+ isBelongTo,
355
+ selection: subFields.map((name) => ({ name, isObject: false })),
356
+ variables: selectOptions.variables,
357
+ };
358
+ // Need to further expand selection of object fields,
359
+ // but only non-graphql-builtin, non-relation fields
360
+ // .ie action { id location }
361
+ // location is non-scalar and non-relational, thus need to further expand into { x y ... }
362
+ if (isBelongTo) {
363
+ const getManyName = modelNameToGetMany(referencedForeignConstraint.refTable);
364
+ const refDefn = this._introspection[getManyName];
365
+ fieldSelection.selection = pickScalarFields.call(this, { [fieldName]: true }, refDefn);
366
+ }
367
+ fields = [...fields, fieldSelection];
368
+ }
369
+ else {
370
+ // Case
371
+ // {
372
+ // userId: true // [fieldName, fieldOptions]
373
+ // }
374
+ if (isWhiteListed(fieldOptions)) {
375
+ fields = [
376
+ ...fields,
377
+ {
378
+ name: fieldName,
379
+ isObject: false,
380
+ fieldDefn: modelMeta.fields.find((f) => f.name === fieldName),
381
+ },
382
+ ];
383
+ }
384
+ }
385
+ }
386
+ return fields;
387
+ }
388
+ function isFieldInDefinition(fieldName, defn, modelMeta) {
389
+ const isReferenced = !!modelMeta.foreignConstraints.find((constraint) => constraint.fromKey.name === fieldName ||
390
+ constraint.fromKey.alias === fieldName);
391
+ return (isReferenced ||
392
+ defn.selection.some((selectionItem) => {
393
+ if (typeof selectionItem === 'string') {
394
+ return fieldName === selectionItem;
395
+ }
396
+ if (isObject(selectionItem)) {
397
+ return selectionItem.name === fieldName;
398
+ }
399
+ return false;
400
+ }));
401
+ }
402
+ // TODO: see if there is a possibility of supertyping table (a key is both a foreign and primary key)
403
+ // A relational field is a foreign key but not a primary key
404
+ function isRelationalField(fieldName, modelMeta) {
405
+ return (!modelMeta.primaryConstraints.find((field) => field.name === fieldName) &&
406
+ !!modelMeta.foreignConstraints.find((constraint) => constraint.fromKey.name === fieldName));
407
+ }
408
+ // Get getMany op name from model
409
+ // ie. UserSetting => userSettings
410
+ function modelNameToGetMany(model) {
411
+ return inflection.camelize(inflection.pluralize(inflection.underscore(model)), true);
412
+ }
@@ -0,0 +1,139 @@
1
+ import type { DocumentNode, FieldNode, SelectionSetNode, VariableDefinitionNode } from 'graphql';
2
+ import type { CleanField } from '../types/schema';
3
+ export type ASTNode = DocumentNode | FieldNode | SelectionSetNode | VariableDefinitionNode;
4
+ export interface NestedProperties {
5
+ [key: string]: QueryProperty | NestedProperties;
6
+ }
7
+ export interface QueryProperty {
8
+ name: string;
9
+ type: string;
10
+ isNotNull: boolean;
11
+ isArray: boolean;
12
+ isArrayNotNull: boolean;
13
+ properties?: NestedProperties;
14
+ }
15
+ export interface QueryDefinition {
16
+ model: string;
17
+ qtype: 'getMany' | 'getOne' | 'mutation';
18
+ mutationType?: 'create' | 'patch' | 'delete';
19
+ selection: string[];
20
+ properties: Record<string, QueryProperty>;
21
+ }
22
+ export interface MutationDefinition extends QueryDefinition {
23
+ qtype: 'mutation';
24
+ mutationType: 'create' | 'patch' | 'delete';
25
+ }
26
+ export interface IntrospectionSchema {
27
+ [key: string]: QueryDefinition | MutationDefinition;
28
+ }
29
+ export interface MetaFieldType {
30
+ gqlType: string;
31
+ isArray: boolean;
32
+ modifier?: string | number | null;
33
+ pgAlias?: string | null;
34
+ pgType?: string | null;
35
+ subtype?: string | null;
36
+ typmod?: number | null;
37
+ }
38
+ export interface MetaField {
39
+ name: string;
40
+ type: MetaFieldType;
41
+ }
42
+ export interface MetaConstraint {
43
+ name: string;
44
+ type: MetaFieldType;
45
+ alias?: string;
46
+ }
47
+ export interface MetaForeignConstraint {
48
+ fromKey: MetaConstraint;
49
+ refTable: string;
50
+ toKey: MetaConstraint;
51
+ }
52
+ export interface MetaTable {
53
+ name: string;
54
+ fields: MetaField[];
55
+ primaryConstraints: MetaConstraint[];
56
+ uniqueConstraints: MetaConstraint[];
57
+ foreignConstraints: MetaForeignConstraint[];
58
+ }
59
+ export interface MetaObject {
60
+ tables: MetaTable[];
61
+ }
62
+ export type GraphQLVariableValue = string | number | boolean | null;
63
+ export interface GraphQLVariables {
64
+ [key: string]: GraphQLVariableValue | GraphQLVariableValue[] | GraphQLVariables | GraphQLVariables[];
65
+ }
66
+ export interface QueryFieldSelection {
67
+ name: string;
68
+ isObject: boolean;
69
+ fieldDefn?: MetaField | CleanField;
70
+ selection?: QueryFieldSelection[];
71
+ variables?: GraphQLVariables;
72
+ isBelongTo?: boolean;
73
+ }
74
+ export interface QuerySelectionOptions {
75
+ [fieldName: string]: boolean | {
76
+ select: Record<string, boolean>;
77
+ variables?: GraphQLVariables;
78
+ };
79
+ }
80
+ export interface QueryBuilderInstance {
81
+ _introspection: IntrospectionSchema;
82
+ _meta: MetaObject;
83
+ _edges?: boolean;
84
+ }
85
+ export interface ASTFunctionParams {
86
+ queryName: string;
87
+ operationName: string;
88
+ query: QueryDefinition;
89
+ selection: QueryFieldSelection[];
90
+ builder?: QueryBuilderInstance;
91
+ }
92
+ export interface MutationASTParams {
93
+ mutationName: string;
94
+ operationName: string;
95
+ mutation: MutationDefinition;
96
+ selection?: QueryFieldSelection[];
97
+ }
98
+ export interface QueryBuilderOptions {
99
+ meta: MetaObject;
100
+ introspection: IntrospectionSchema;
101
+ }
102
+ export interface QueryBuilderResult {
103
+ _hash: string;
104
+ _queryName: string;
105
+ _ast: DocumentNode;
106
+ }
107
+ export interface IQueryBuilder {
108
+ query(model: string): IQueryBuilder;
109
+ getMany(options?: {
110
+ select?: QuerySelectionOptions;
111
+ }): IQueryBuilder;
112
+ getOne(options?: {
113
+ select?: QuerySelectionOptions;
114
+ }): IQueryBuilder;
115
+ all(options?: {
116
+ select?: QuerySelectionOptions;
117
+ }): IQueryBuilder;
118
+ count(): IQueryBuilder;
119
+ create(options?: {
120
+ select?: QuerySelectionOptions;
121
+ }): IQueryBuilder;
122
+ update(options?: {
123
+ select?: QuerySelectionOptions;
124
+ }): IQueryBuilder;
125
+ delete(options?: {
126
+ select?: QuerySelectionOptions;
127
+ }): IQueryBuilder;
128
+ edges(useEdges: boolean): IQueryBuilder;
129
+ print(): QueryBuilderResult;
130
+ }
131
+ export interface ObjectArrayItem extends QueryProperty {
132
+ name: string;
133
+ key?: string;
134
+ }
135
+ export declare function isGraphQLVariableValue(value: unknown): value is GraphQLVariableValue;
136
+ export declare function isGraphQLVariables(obj: unknown): obj is GraphQLVariables;
137
+ export type StrictRecord<K extends PropertyKey, V> = Record<K, V> & {
138
+ [P in PropertyKey]: P extends K ? V : never;
139
+ };
package/core/types.js ADDED
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isGraphQLVariableValue = isGraphQLVariableValue;
4
+ exports.isGraphQLVariables = isGraphQLVariables;
5
+ // Type guards for runtime validation
6
+ function isGraphQLVariableValue(value) {
7
+ return (value === null ||
8
+ typeof value === 'string' ||
9
+ typeof value === 'number' ||
10
+ typeof value === 'boolean');
11
+ }
12
+ function isGraphQLVariables(obj) {
13
+ if (!obj || typeof obj !== 'object')
14
+ return false;
15
+ for (const [key, value] of Object.entries(obj)) {
16
+ if (typeof key !== 'string')
17
+ return false;
18
+ if (Array.isArray(value)) {
19
+ if (!value.every((item) => isGraphQLVariableValue(item) || isGraphQLVariables(item))) {
20
+ return false;
21
+ }
22
+ }
23
+ else if (!isGraphQLVariableValue(value) && !isGraphQLVariables(value)) {
24
+ return false;
25
+ }
26
+ }
27
+ return true;
28
+ }