@constructive-io/graphql-codegen 4.6.1 → 4.7.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 (71) hide show
  1. package/client/error.d.ts +2 -93
  2. package/client/error.js +9 -273
  3. package/client/execute.d.ts +2 -55
  4. package/client/execute.js +5 -120
  5. package/client/typed-document.d.ts +2 -29
  6. package/client/typed-document.js +3 -39
  7. package/core/ast.d.ts +8 -10
  8. package/core/ast.js +17 -592
  9. package/core/custom-ast.d.ts +5 -33
  10. package/core/custom-ast.js +16 -203
  11. package/core/introspect/infer-tables.d.ts +2 -40
  12. package/core/introspect/infer-tables.js +4 -696
  13. package/core/introspect/schema-query.d.ts +3 -18
  14. package/core/introspect/schema-query.js +3 -118
  15. package/core/introspect/transform-schema.d.ts +2 -84
  16. package/core/introspect/transform-schema.js +14 -279
  17. package/core/introspect/transform.d.ts +2 -18
  18. package/core/introspect/transform.js +6 -39
  19. package/core/meta-object/convert.d.ts +2 -63
  20. package/core/meta-object/convert.js +4 -59
  21. package/core/meta-object/validate.d.ts +2 -7
  22. package/core/meta-object/validate.js +4 -30
  23. package/core/query-builder.d.ts +7 -46
  24. package/core/query-builder.js +8 -408
  25. package/core/types.d.ts +9 -139
  26. package/core/types.js +12 -26
  27. package/esm/client/error.d.ts +2 -93
  28. package/esm/client/error.js +2 -269
  29. package/esm/client/execute.d.ts +2 -55
  30. package/esm/client/execute.js +2 -118
  31. package/esm/client/typed-document.d.ts +2 -29
  32. package/esm/client/typed-document.js +2 -38
  33. package/esm/core/ast.d.ts +8 -10
  34. package/esm/core/ast.js +8 -550
  35. package/esm/core/custom-ast.d.ts +5 -33
  36. package/esm/core/custom-ast.js +5 -160
  37. package/esm/core/introspect/infer-tables.d.ts +2 -40
  38. package/esm/core/introspect/infer-tables.js +2 -695
  39. package/esm/core/introspect/schema-query.d.ts +3 -18
  40. package/esm/core/introspect/schema-query.js +2 -118
  41. package/esm/core/introspect/transform-schema.d.ts +2 -84
  42. package/esm/core/introspect/transform-schema.js +2 -269
  43. package/esm/core/introspect/transform.d.ts +2 -18
  44. package/esm/core/introspect/transform.js +2 -36
  45. package/esm/core/meta-object/convert.d.ts +2 -63
  46. package/esm/core/meta-object/convert.js +2 -58
  47. package/esm/core/meta-object/validate.d.ts +2 -7
  48. package/esm/core/meta-object/validate.js +2 -26
  49. package/esm/core/query-builder.d.ts +7 -46
  50. package/esm/core/query-builder.js +5 -373
  51. package/esm/core/types.d.ts +9 -139
  52. package/esm/core/types.js +9 -24
  53. package/esm/generators/field-selector.d.ts +5 -28
  54. package/esm/generators/field-selector.js +5 -379
  55. package/esm/generators/mutations.d.ts +5 -28
  56. package/esm/generators/mutations.js +5 -198
  57. package/esm/generators/naming-helpers.d.ts +3 -45
  58. package/esm/generators/naming-helpers.js +3 -151
  59. package/esm/generators/select.d.ts +6 -37
  60. package/esm/generators/select.js +6 -659
  61. package/generators/field-selector.d.ts +5 -28
  62. package/generators/field-selector.js +12 -385
  63. package/generators/mutations.d.ts +5 -28
  64. package/generators/mutations.js +9 -234
  65. package/generators/naming-helpers.d.ts +3 -45
  66. package/generators/naming-helpers.js +15 -164
  67. package/generators/select.d.ts +6 -37
  68. package/generators/select.js +17 -703
  69. package/package.json +7 -6
  70. package/core/meta-object/format.json +0 -93
  71. package/esm/core/meta-object/format.json +0 -93
@@ -1,63 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.convertFromMetaSchema = convertFromMetaSchema;
3
+ exports.convertFromMetaSchema = void 0;
4
4
  /**
5
- * Convert from raw _meta schema response to internal MetaObject format
5
+ * Re-export meta-object conversion from @constructive-io/graphql-query.
6
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
- }
7
+ var graphql_query_1 = require("@constructive-io/graphql-query");
8
+ Object.defineProperty(exports, "convertFromMetaSchema", { enumerable: true, get: function () { return graphql_query_1.convertFromMetaSchema; } });
@@ -1,9 +1,4 @@
1
- export interface ValidationResult {
2
- errors: unknown[] | null | undefined;
3
- message: string;
4
- }
5
1
  /**
6
- * Validate a MetaObject against the JSON schema
7
- * @returns true if valid, or an object with errors and message if invalid
2
+ * Re-export meta-object validation from @constructive-io/graphql-query.
8
3
  */
9
- export declare function validateMetaObject(obj: unknown): true | ValidationResult;
4
+ export { validateMetaObject, type ValidationResult, } from '@constructive-io/graphql-query';
@@ -1,34 +1,8 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  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
- }
3
+ exports.validateMetaObject = void 0;
21
4
  /**
22
- * Validate a MetaObject against the JSON schema
23
- * @returns true if valid, or an object with errors and message if invalid
5
+ * Re-export meta-object validation from @constructive-io/graphql-query.
24
6
  */
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
- }
7
+ var graphql_query_1 = require("@constructive-io/graphql-query");
8
+ Object.defineProperty(exports, "validateMetaObject", { enumerable: true, get: function () { return graphql_query_1.validateMetaObject; } });
@@ -1,46 +1,7 @@
1
- import type { IntrospectionSchema, MetaObject, QueryBuilderOptions, QueryBuilderResult, QueryDefinition, QueryFieldSelection, 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
- }
1
+ /**
2
+ * Re-export QueryBuilder from @constructive-io/graphql-query.
3
+ *
4
+ * The QueryBuilder class and MetaObject namespace now live in graphql-query.
5
+ * Re-exported here for backward compatibility.
6
+ */
7
+ export { QueryBuilder, MetaObject } from '@constructive-io/graphql-query';
@@ -1,412 +1,12 @@
1
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
2
  Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.QueryBuilder = exports.MetaObject = void 0;
37
- const graphql_1 = require("graphql");
38
- const inflekt_1 = require("inflekt");
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${(0, inflekt_1.camelize)(this._model)}Input`;
128
- }
129
- case 'create': {
130
- return `Create${(0, inflekt_1.camelize)(this._model)}Input`;
131
- }
132
- case 'patch': {
133
- return `Update${(0, inflekt_1.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((0, inflekt_1.camelize)(['get', (0, inflekt_1.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((0, inflekt_1.camelize)(['get', (0, inflekt_1.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((0, inflekt_1.camelize)(['get', (0, inflekt_1.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((0, inflekt_1.camelize)(['get', (0, inflekt_1.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((0, inflekt_1.camelize)([(0, inflekt_1.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((0, inflekt_1.camelize)([(0, inflekt_1.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((0, inflekt_1.camelize)([(0, inflekt_1.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;
3
+ exports.MetaObject = exports.QueryBuilder = void 0;
279
4
  /**
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}
5
+ * Re-export QueryBuilder from @constructive-io/graphql-query.
6
+ *
7
+ * The QueryBuilder class and MetaObject namespace now live in graphql-query.
8
+ * Re-exported here for backward compatibility.
284
9
  */
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 (0, inflekt_1.camelize)((0, inflekt_1.pluralize)((0, inflekt_1.underscore)(model)), true);
412
- }
10
+ var graphql_query_1 = require("@constructive-io/graphql-query");
11
+ Object.defineProperty(exports, "QueryBuilder", { enumerable: true, get: function () { return graphql_query_1.QueryBuilder; } });
12
+ Object.defineProperty(exports, "MetaObject", { enumerable: true, get: function () { return graphql_query_1.MetaObject; } });