@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.
- package/README.md +1818 -113
- package/__tests__/codegen/input-types-generator.test.d.ts +1 -0
- package/__tests__/codegen/input-types-generator.test.js +635 -0
- package/cli/codegen/barrel.d.ts +27 -0
- package/cli/codegen/barrel.js +163 -0
- package/cli/codegen/client.d.ts +4 -0
- package/cli/codegen/client.js +170 -0
- package/cli/codegen/custom-mutations.d.ts +38 -0
- package/cli/codegen/custom-mutations.js +149 -0
- package/cli/codegen/custom-queries.d.ts +38 -0
- package/cli/codegen/custom-queries.js +358 -0
- package/cli/codegen/filters.d.ts +27 -0
- package/cli/codegen/filters.js +357 -0
- package/cli/codegen/gql-ast.d.ts +41 -0
- package/cli/codegen/gql-ast.js +329 -0
- package/cli/codegen/index.d.ts +71 -0
- package/cli/codegen/index.js +147 -0
- package/cli/codegen/mutations.d.ts +30 -0
- package/cli/codegen/mutations.js +410 -0
- package/cli/codegen/orm/barrel.d.ts +18 -0
- package/cli/codegen/orm/barrel.js +48 -0
- package/cli/codegen/orm/client-generator.d.ts +45 -0
- package/cli/codegen/orm/client-generator.js +646 -0
- package/cli/codegen/orm/custom-ops-generator.d.ts +30 -0
- package/cli/codegen/orm/custom-ops-generator.js +350 -0
- package/cli/codegen/orm/index.d.ts +38 -0
- package/cli/codegen/orm/index.js +88 -0
- package/cli/codegen/orm/input-types-generator.d.ts +21 -0
- package/cli/codegen/orm/input-types-generator.js +705 -0
- package/cli/codegen/orm/input-types-generator.test.d.ts +1 -0
- package/cli/codegen/orm/input-types-generator.test.js +75 -0
- package/cli/codegen/orm/model-generator.d.ts +32 -0
- package/cli/codegen/orm/model-generator.js +264 -0
- package/cli/codegen/orm/query-builder.d.ts +161 -0
- package/cli/codegen/orm/query-builder.js +366 -0
- package/cli/codegen/orm/select-types.d.ts +169 -0
- package/cli/codegen/orm/select-types.js +16 -0
- package/cli/codegen/orm/select-types.test.d.ts +11 -0
- package/cli/codegen/orm/select-types.test.js +22 -0
- package/cli/codegen/queries.d.ts +25 -0
- package/cli/codegen/queries.js +438 -0
- package/cli/codegen/scalars.d.ts +12 -0
- package/cli/codegen/scalars.js +71 -0
- package/cli/codegen/schema-gql-ast.d.ts +51 -0
- package/cli/codegen/schema-gql-ast.js +385 -0
- package/cli/codegen/ts-ast.d.ts +122 -0
- package/cli/codegen/ts-ast.js +280 -0
- package/cli/codegen/type-resolver.d.ts +96 -0
- package/cli/codegen/type-resolver.js +246 -0
- package/cli/codegen/types.d.ts +12 -0
- package/cli/codegen/types.js +69 -0
- package/cli/codegen/utils.d.ts +163 -0
- package/cli/codegen/utils.js +326 -0
- package/cli/commands/generate-orm.d.ts +37 -0
- package/cli/commands/generate-orm.js +195 -0
- package/cli/commands/generate.d.ts +39 -0
- package/cli/commands/generate.js +299 -0
- package/cli/commands/index.d.ts +7 -0
- package/cli/commands/index.js +12 -0
- package/cli/commands/init.d.ts +35 -0
- package/cli/commands/init.js +176 -0
- package/cli/index.d.ts +4 -0
- package/cli/index.js +291 -0
- package/cli/introspect/fetch-meta.d.ts +31 -0
- package/cli/introspect/fetch-meta.js +108 -0
- package/cli/introspect/fetch-schema.d.ts +21 -0
- package/cli/introspect/fetch-schema.js +86 -0
- package/cli/introspect/index.d.ts +8 -0
- package/cli/introspect/index.js +16 -0
- package/cli/introspect/meta-query.d.ts +111 -0
- package/cli/introspect/meta-query.js +191 -0
- package/cli/introspect/schema-query.d.ts +20 -0
- package/cli/introspect/schema-query.js +123 -0
- package/cli/introspect/transform-schema.d.ts +74 -0
- package/cli/introspect/transform-schema.js +269 -0
- package/cli/introspect/transform-schema.test.d.ts +1 -0
- package/cli/introspect/transform-schema.test.js +67 -0
- package/cli/introspect/transform.d.ts +21 -0
- package/cli/introspect/transform.js +216 -0
- package/cli/watch/cache.d.ts +45 -0
- package/cli/watch/cache.js +111 -0
- package/cli/watch/debounce.d.ts +19 -0
- package/cli/watch/debounce.js +89 -0
- package/cli/watch/hash.d.ts +17 -0
- package/cli/watch/hash.js +48 -0
- package/cli/watch/index.d.ts +10 -0
- package/cli/watch/index.js +22 -0
- package/cli/watch/orchestrator.d.ts +63 -0
- package/cli/watch/orchestrator.js +228 -0
- package/cli/watch/poller.d.ts +65 -0
- package/cli/watch/poller.js +203 -0
- package/cli/watch/types.d.ts +67 -0
- package/cli/watch/types.js +5 -0
- package/client/error.d.ts +95 -0
- package/client/error.js +255 -0
- package/client/execute.d.ts +57 -0
- package/client/execute.js +124 -0
- package/client/index.d.ts +6 -0
- package/client/index.js +18 -0
- package/client/typed-document.d.ts +31 -0
- package/client/typed-document.js +44 -0
- package/core/ast.d.ts +10 -0
- package/core/ast.js +593 -0
- package/core/custom-ast.d.ts +35 -0
- package/core/custom-ast.js +204 -0
- package/core/index.d.ts +8 -0
- package/core/index.js +33 -0
- package/core/meta-object/convert.d.ts +65 -0
- package/core/meta-object/convert.js +63 -0
- package/core/meta-object/format.json +93 -0
- package/core/meta-object/index.d.ts +2 -0
- package/core/meta-object/index.js +18 -0
- package/core/meta-object/validate.d.ts +9 -0
- package/core/meta-object/validate.js +34 -0
- package/core/query-builder.d.ts +46 -0
- package/core/query-builder.js +412 -0
- package/core/types.d.ts +139 -0
- package/core/types.js +28 -0
- package/esm/__tests__/codegen/input-types-generator.test.d.ts +1 -0
- package/esm/__tests__/codegen/input-types-generator.test.js +633 -0
- package/esm/cli/codegen/barrel.d.ts +27 -0
- package/esm/cli/codegen/barrel.js +156 -0
- package/esm/cli/codegen/client.d.ts +4 -0
- package/esm/cli/codegen/client.js +167 -0
- package/esm/cli/codegen/custom-mutations.d.ts +38 -0
- package/esm/cli/codegen/custom-mutations.js +145 -0
- package/esm/cli/codegen/custom-queries.d.ts +38 -0
- package/esm/cli/codegen/custom-queries.js +354 -0
- package/esm/cli/codegen/filters.d.ts +27 -0
- package/esm/cli/codegen/filters.js +351 -0
- package/esm/cli/codegen/gql-ast.d.ts +41 -0
- package/esm/cli/codegen/gql-ast.js +288 -0
- package/esm/cli/codegen/index.d.ts +71 -0
- package/esm/cli/codegen/index.js +124 -0
- package/esm/cli/codegen/mutations.d.ts +30 -0
- package/esm/cli/codegen/mutations.js +404 -0
- package/esm/cli/codegen/orm/barrel.d.ts +18 -0
- package/esm/cli/codegen/orm/barrel.js +44 -0
- package/esm/cli/codegen/orm/client-generator.d.ts +45 -0
- package/esm/cli/codegen/orm/client-generator.js +640 -0
- package/esm/cli/codegen/orm/custom-ops-generator.d.ts +30 -0
- package/esm/cli/codegen/orm/custom-ops-generator.js +346 -0
- package/esm/cli/codegen/orm/index.d.ts +38 -0
- package/esm/cli/codegen/orm/index.js +75 -0
- package/esm/cli/codegen/orm/input-types-generator.d.ts +21 -0
- package/esm/cli/codegen/orm/input-types-generator.js +700 -0
- package/esm/cli/codegen/orm/input-types-generator.test.d.ts +1 -0
- package/esm/cli/codegen/orm/input-types-generator.test.js +73 -0
- package/esm/cli/codegen/orm/model-generator.d.ts +32 -0
- package/esm/cli/codegen/orm/model-generator.js +260 -0
- package/esm/cli/codegen/orm/query-builder.d.ts +161 -0
- package/esm/cli/codegen/orm/query-builder.js +353 -0
- package/esm/cli/codegen/orm/select-types.d.ts +169 -0
- package/esm/cli/codegen/orm/select-types.js +15 -0
- package/esm/cli/codegen/orm/select-types.test.d.ts +11 -0
- package/esm/cli/codegen/orm/select-types.test.js +21 -0
- package/esm/cli/codegen/queries.d.ts +25 -0
- package/esm/cli/codegen/queries.js +433 -0
- package/esm/cli/codegen/scalars.d.ts +12 -0
- package/esm/cli/codegen/scalars.js +66 -0
- package/esm/cli/codegen/schema-gql-ast.d.ts +51 -0
- package/esm/cli/codegen/schema-gql-ast.js +343 -0
- package/esm/cli/codegen/ts-ast.d.ts +122 -0
- package/esm/cli/codegen/ts-ast.js +260 -0
- package/esm/cli/codegen/type-resolver.d.ts +96 -0
- package/esm/cli/codegen/type-resolver.js +224 -0
- package/esm/cli/codegen/types.d.ts +12 -0
- package/esm/cli/codegen/types.js +65 -0
- package/esm/cli/codegen/utils.d.ts +163 -0
- package/esm/cli/codegen/utils.js +288 -0
- package/esm/cli/commands/generate-orm.d.ts +37 -0
- package/esm/cli/commands/generate-orm.js +192 -0
- package/esm/cli/commands/generate.d.ts +39 -0
- package/esm/cli/commands/generate.js +262 -0
- package/esm/cli/commands/index.d.ts +7 -0
- package/esm/cli/commands/index.js +5 -0
- package/esm/cli/commands/init.d.ts +35 -0
- package/esm/cli/commands/init.js +138 -0
- package/esm/cli/index.d.ts +4 -0
- package/esm/cli/index.js +256 -0
- package/esm/cli/introspect/fetch-meta.d.ts +31 -0
- package/esm/cli/introspect/fetch-meta.js +104 -0
- package/esm/cli/introspect/fetch-schema.d.ts +21 -0
- package/esm/cli/introspect/fetch-schema.js +83 -0
- package/esm/cli/introspect/index.d.ts +8 -0
- package/esm/cli/introspect/index.js +6 -0
- package/esm/cli/introspect/meta-query.d.ts +111 -0
- package/esm/cli/introspect/meta-query.js +188 -0
- package/esm/cli/introspect/schema-query.d.ts +20 -0
- package/esm/cli/introspect/schema-query.js +120 -0
- package/esm/cli/introspect/transform-schema.d.ts +74 -0
- package/esm/cli/introspect/transform-schema.js +259 -0
- package/esm/cli/introspect/transform-schema.test.d.ts +1 -0
- package/esm/cli/introspect/transform-schema.test.js +65 -0
- package/esm/cli/introspect/transform.d.ts +21 -0
- package/esm/cli/introspect/transform.js +210 -0
- package/esm/cli/watch/cache.d.ts +45 -0
- package/esm/cli/watch/cache.js +73 -0
- package/esm/cli/watch/debounce.d.ts +19 -0
- package/esm/cli/watch/debounce.js +85 -0
- package/esm/cli/watch/hash.d.ts +17 -0
- package/esm/cli/watch/hash.js +43 -0
- package/esm/cli/watch/index.d.ts +10 -0
- package/esm/cli/watch/index.js +8 -0
- package/esm/cli/watch/orchestrator.d.ts +63 -0
- package/esm/cli/watch/orchestrator.js +223 -0
- package/esm/cli/watch/poller.d.ts +65 -0
- package/esm/cli/watch/poller.js +198 -0
- package/esm/cli/watch/types.d.ts +67 -0
- package/esm/cli/watch/types.js +4 -0
- package/esm/client/error.d.ts +95 -0
- package/esm/client/error.js +249 -0
- package/esm/client/execute.d.ts +57 -0
- package/esm/client/execute.js +120 -0
- package/esm/client/index.d.ts +6 -0
- package/esm/client/index.js +6 -0
- package/esm/client/typed-document.d.ts +31 -0
- package/esm/client/typed-document.js +40 -0
- package/esm/core/ast.d.ts +10 -0
- package/esm/core/ast.js +549 -0
- package/esm/core/custom-ast.d.ts +35 -0
- package/esm/core/custom-ast.js +161 -0
- package/esm/core/index.d.ts +8 -0
- package/esm/core/index.js +12 -0
- package/esm/core/meta-object/convert.d.ts +65 -0
- package/esm/core/meta-object/convert.js +60 -0
- package/esm/core/meta-object/format.json +93 -0
- package/esm/core/meta-object/index.d.ts +2 -0
- package/esm/core/meta-object/index.js +2 -0
- package/esm/core/meta-object/validate.d.ts +9 -0
- package/esm/core/meta-object/validate.js +28 -0
- package/esm/core/query-builder.d.ts +46 -0
- package/esm/core/query-builder.js +375 -0
- package/esm/core/types.d.ts +139 -0
- package/esm/core/types.js +24 -0
- package/esm/generators/field-selector.d.ts +30 -0
- package/esm/generators/field-selector.js +355 -0
- package/esm/generators/index.d.ts +6 -0
- package/esm/generators/index.js +9 -0
- package/esm/generators/mutations.d.ts +31 -0
- package/esm/generators/mutations.js +197 -0
- package/esm/generators/select.d.ts +50 -0
- package/esm/generators/select.js +636 -0
- package/esm/index.d.ts +12 -0
- package/esm/index.js +17 -3
- package/esm/react/index.d.ts +5 -0
- package/esm/react/index.js +6 -0
- package/esm/types/config.d.ts +199 -0
- package/esm/types/config.js +106 -0
- package/esm/types/index.d.ts +9 -0
- package/esm/types/index.js +4 -0
- package/esm/types/introspection.d.ts +121 -0
- package/esm/types/introspection.js +54 -0
- package/esm/types/mutation.d.ts +45 -0
- package/esm/types/mutation.js +4 -0
- package/esm/types/query.d.ts +82 -0
- package/esm/types/query.js +4 -0
- package/esm/types/schema.d.ts +253 -0
- package/esm/types/schema.js +5 -0
- package/esm/types/selection.d.ts +43 -0
- package/esm/types/selection.js +4 -0
- package/esm/utils/index.d.ts +4 -0
- package/esm/utils/index.js +4 -0
- package/generators/field-selector.d.ts +30 -0
- package/generators/field-selector.js +361 -0
- package/generators/index.d.ts +6 -0
- package/generators/index.js +27 -0
- package/generators/mutations.d.ts +31 -0
- package/generators/mutations.js +235 -0
- package/generators/select.d.ts +50 -0
- package/generators/select.js +679 -0
- package/index.d.ts +12 -3
- package/index.js +19 -3
- package/package.json +59 -38
- package/react/index.d.ts +5 -0
- package/react/index.js +9 -0
- package/types/config.d.ts +199 -0
- package/types/config.js +111 -0
- package/types/index.d.ts +9 -0
- package/types/index.js +10 -0
- package/types/introspection.d.ts +121 -0
- package/types/introspection.js +62 -0
- package/types/mutation.d.ts +45 -0
- package/types/mutation.js +5 -0
- package/types/query.d.ts +82 -0
- package/types/query.js +5 -0
- package/types/schema.d.ts +253 -0
- package/types/schema.js +6 -0
- package/types/selection.d.ts +43 -0
- package/types/selection.js +5 -0
- package/utils/index.d.ts +4 -0
- package/utils/index.js +7 -0
- package/codegen.d.ts +0 -13
- package/codegen.js +0 -293
- package/esm/codegen.js +0 -253
- package/esm/gql.js +0 -939
- package/esm/options.js +0 -27
- package/gql.d.ts +0 -188
- package/gql.js +0 -992
- package/options.d.ts +0 -45
- package/options.js +0 -31
package/gql.js
DELETED
|
@@ -1,992 +0,0 @@
|
|
|
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
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
-
};
|
|
38
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.generateGranular = exports.generate = exports.createMutation = exports.deleteOne = exports.patchOne = exports.createOne = exports.getOne = exports.getFragment = exports.getOrderByEnums = exports.getManyPaginatedNodes = exports.getManyPaginatedEdges = exports.getMany = exports.createGqlMutation = void 0;
|
|
40
|
-
exports.getSelections = getSelections;
|
|
41
|
-
// TODO: use inflection for all the things
|
|
42
|
-
// const { singularize } = require('inflection');
|
|
43
|
-
const t = __importStar(require("gql-ast"));
|
|
44
|
-
const inflection_1 = __importDefault(require("inflection"));
|
|
45
|
-
const NON_MUTABLE_PROPS = [
|
|
46
|
-
'id',
|
|
47
|
-
'createdAt',
|
|
48
|
-
'createdBy',
|
|
49
|
-
'updatedAt',
|
|
50
|
-
'updatedBy',
|
|
51
|
-
];
|
|
52
|
-
const objectToArray = (obj) => Object.keys(obj).map((k) => ({ name: k, ...obj[k] }));
|
|
53
|
-
function refToTypeNode(ref, overrides) {
|
|
54
|
-
if (!ref)
|
|
55
|
-
return null;
|
|
56
|
-
if (ref.kind === 'NON_NULL') {
|
|
57
|
-
const inner = refToTypeNode(ref.ofType, overrides);
|
|
58
|
-
return t.nonNullType({ type: inner });
|
|
59
|
-
}
|
|
60
|
-
if (ref.kind === 'LIST') {
|
|
61
|
-
const inner = refToTypeNode(ref.ofType, overrides);
|
|
62
|
-
return t.listType({ type: inner });
|
|
63
|
-
}
|
|
64
|
-
const name = (overrides && overrides[ref.name]) || ref.name;
|
|
65
|
-
return t.namedType({ type: name });
|
|
66
|
-
}
|
|
67
|
-
function resolveTypeName(name, type, overrides) {
|
|
68
|
-
if (typeof type === 'string') {
|
|
69
|
-
const base = type;
|
|
70
|
-
const mapped = overrides && overrides[base];
|
|
71
|
-
return mapped || base;
|
|
72
|
-
}
|
|
73
|
-
if (type && typeof type === 'object') {
|
|
74
|
-
if (typeof type.name === 'string' && type.name.length > 0)
|
|
75
|
-
return type.name;
|
|
76
|
-
let t = type;
|
|
77
|
-
while (t && typeof t === 'object' && t.ofType)
|
|
78
|
-
t = t.ofType;
|
|
79
|
-
if (t && typeof t.name === 'string' && t.name.length > 0) {
|
|
80
|
-
const base = t.name;
|
|
81
|
-
const mapped = overrides && overrides[base];
|
|
82
|
-
return mapped || base;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
return 'JSON';
|
|
86
|
-
}
|
|
87
|
-
function refToNamedTypeName(ref) {
|
|
88
|
-
let r = ref;
|
|
89
|
-
while (r && (r.kind === 'NON_NULL' || r.kind === 'LIST'))
|
|
90
|
-
r = r.ofType;
|
|
91
|
-
return r && r.name ? r.name : null;
|
|
92
|
-
}
|
|
93
|
-
function extractNamedTypeName(node) {
|
|
94
|
-
let n = node;
|
|
95
|
-
while (n && !n.name && n.type)
|
|
96
|
-
n = n.type;
|
|
97
|
-
return n && n.name ? n.name : null;
|
|
98
|
-
}
|
|
99
|
-
function singularModel(name) {
|
|
100
|
-
return inflection_1.default.singularize(name);
|
|
101
|
-
}
|
|
102
|
-
const createGqlMutation = ({ operationName, mutationName, selectArgs, variableDefinitions, modelName, selections, useModel = true, }) => {
|
|
103
|
-
const opSel = !modelName
|
|
104
|
-
? [
|
|
105
|
-
t.field({
|
|
106
|
-
name: operationName,
|
|
107
|
-
args: selectArgs,
|
|
108
|
-
selectionSet: t.selectionSet({ selections }),
|
|
109
|
-
}),
|
|
110
|
-
]
|
|
111
|
-
: [
|
|
112
|
-
t.field({
|
|
113
|
-
name: operationName,
|
|
114
|
-
args: selectArgs,
|
|
115
|
-
selectionSet: t.selectionSet({
|
|
116
|
-
selections: useModel
|
|
117
|
-
? [
|
|
118
|
-
t.field({
|
|
119
|
-
name: modelName,
|
|
120
|
-
selectionSet: t.selectionSet({ selections }),
|
|
121
|
-
}),
|
|
122
|
-
]
|
|
123
|
-
: selections,
|
|
124
|
-
}),
|
|
125
|
-
}),
|
|
126
|
-
];
|
|
127
|
-
return t.document({
|
|
128
|
-
definitions: [
|
|
129
|
-
t.operationDefinition({
|
|
130
|
-
operation: 'mutation',
|
|
131
|
-
name: mutationName,
|
|
132
|
-
variableDefinitions,
|
|
133
|
-
selectionSet: t.selectionSet({ selections: opSel }),
|
|
134
|
-
}),
|
|
135
|
-
],
|
|
136
|
-
});
|
|
137
|
-
};
|
|
138
|
-
exports.createGqlMutation = createGqlMutation;
|
|
139
|
-
const getMany = ({ operationName, query, fields, }) => {
|
|
140
|
-
const queryName = inflection_1.default.camelize(['get', inflection_1.default.underscore(operationName), 'query', 'all'].join('_'), true);
|
|
141
|
-
const selections = getSelections(query, fields);
|
|
142
|
-
const opSel = [
|
|
143
|
-
t.field({
|
|
144
|
-
name: operationName,
|
|
145
|
-
selectionSet: t.selectionSet({
|
|
146
|
-
selections: [
|
|
147
|
-
t.field({ name: 'totalCount' }),
|
|
148
|
-
t.field({
|
|
149
|
-
name: 'pageInfo',
|
|
150
|
-
selectionSet: t.selectionSet({
|
|
151
|
-
selections: [
|
|
152
|
-
t.field({ name: 'hasNextPage' }),
|
|
153
|
-
t.field({ name: 'hasPreviousPage' }),
|
|
154
|
-
t.field({ name: 'endCursor' }),
|
|
155
|
-
t.field({ name: 'startCursor' }),
|
|
156
|
-
],
|
|
157
|
-
}),
|
|
158
|
-
}),
|
|
159
|
-
t.field({
|
|
160
|
-
name: 'edges',
|
|
161
|
-
selectionSet: t.selectionSet({
|
|
162
|
-
selections: [
|
|
163
|
-
t.field({ name: 'cursor' }),
|
|
164
|
-
t.field({
|
|
165
|
-
name: 'node',
|
|
166
|
-
selectionSet: t.selectionSet({ selections }),
|
|
167
|
-
}),
|
|
168
|
-
],
|
|
169
|
-
}),
|
|
170
|
-
}),
|
|
171
|
-
],
|
|
172
|
-
}),
|
|
173
|
-
}),
|
|
174
|
-
];
|
|
175
|
-
const ast = t.document({
|
|
176
|
-
definitions: [
|
|
177
|
-
t.operationDefinition({
|
|
178
|
-
operation: 'query',
|
|
179
|
-
name: queryName,
|
|
180
|
-
selectionSet: t.selectionSet({ selections: opSel }),
|
|
181
|
-
}),
|
|
182
|
-
],
|
|
183
|
-
});
|
|
184
|
-
return { name: queryName, ast };
|
|
185
|
-
};
|
|
186
|
-
exports.getMany = getMany;
|
|
187
|
-
const getManyPaginatedEdges = ({ operationName, query, fields, }) => {
|
|
188
|
-
const queryName = inflection_1.default.camelize(['get', inflection_1.default.underscore(operationName), 'paginated'].join('_'), true);
|
|
189
|
-
const Plural = operationName.charAt(0).toUpperCase() + operationName.slice(1);
|
|
190
|
-
const Singular = query.model;
|
|
191
|
-
const Condition = `${Singular}Condition`;
|
|
192
|
-
const Filter = `${Singular}Filter`;
|
|
193
|
-
const OrderBy = `${Plural}OrderBy`;
|
|
194
|
-
const selections = getSelections(query, fields);
|
|
195
|
-
const variableDefinitions = [
|
|
196
|
-
'first',
|
|
197
|
-
'last',
|
|
198
|
-
'offset',
|
|
199
|
-
'after',
|
|
200
|
-
'before',
|
|
201
|
-
].map((name) => t.variableDefinition({
|
|
202
|
-
variable: t.variable({ name }),
|
|
203
|
-
type: t.namedType({ type: name === 'after' || name === 'before' ? 'Cursor' : 'Int' }),
|
|
204
|
-
}));
|
|
205
|
-
variableDefinitions.push(t.variableDefinition({
|
|
206
|
-
variable: t.variable({ name: 'condition' }),
|
|
207
|
-
type: t.namedType({ type: Condition }),
|
|
208
|
-
}), t.variableDefinition({
|
|
209
|
-
variable: t.variable({ name: 'filter' }),
|
|
210
|
-
type: t.namedType({ type: Filter }),
|
|
211
|
-
}), t.variableDefinition({
|
|
212
|
-
variable: t.variable({ name: 'orderBy' }),
|
|
213
|
-
type: t.listType({
|
|
214
|
-
type: t.nonNullType({
|
|
215
|
-
type: t.namedType({ type: OrderBy }),
|
|
216
|
-
}),
|
|
217
|
-
}),
|
|
218
|
-
}));
|
|
219
|
-
const args = [
|
|
220
|
-
'first',
|
|
221
|
-
'last',
|
|
222
|
-
'offset',
|
|
223
|
-
'after',
|
|
224
|
-
'before',
|
|
225
|
-
'condition',
|
|
226
|
-
'filter',
|
|
227
|
-
'orderBy',
|
|
228
|
-
].map((name) => t.argument({
|
|
229
|
-
name,
|
|
230
|
-
value: t.variable({ name }),
|
|
231
|
-
}));
|
|
232
|
-
const ast = t.document({
|
|
233
|
-
definitions: [
|
|
234
|
-
t.operationDefinition({
|
|
235
|
-
operation: 'query',
|
|
236
|
-
name: queryName,
|
|
237
|
-
variableDefinitions,
|
|
238
|
-
selectionSet: t.selectionSet({
|
|
239
|
-
selections: [
|
|
240
|
-
t.field({
|
|
241
|
-
name: operationName,
|
|
242
|
-
args,
|
|
243
|
-
selectionSet: t.selectionSet({
|
|
244
|
-
selections: [
|
|
245
|
-
t.field({ name: 'totalCount' }),
|
|
246
|
-
t.field({
|
|
247
|
-
name: 'pageInfo',
|
|
248
|
-
selectionSet: t.selectionSet({
|
|
249
|
-
selections: [
|
|
250
|
-
t.field({ name: 'hasNextPage' }),
|
|
251
|
-
t.field({ name: 'hasPreviousPage' }),
|
|
252
|
-
t.field({ name: 'endCursor' }),
|
|
253
|
-
t.field({ name: 'startCursor' }),
|
|
254
|
-
],
|
|
255
|
-
}),
|
|
256
|
-
}),
|
|
257
|
-
t.field({
|
|
258
|
-
name: 'edges',
|
|
259
|
-
selectionSet: t.selectionSet({
|
|
260
|
-
selections: [
|
|
261
|
-
t.field({ name: 'cursor' }),
|
|
262
|
-
t.field({
|
|
263
|
-
name: 'node',
|
|
264
|
-
selectionSet: t.selectionSet({ selections }),
|
|
265
|
-
}),
|
|
266
|
-
],
|
|
267
|
-
}),
|
|
268
|
-
}),
|
|
269
|
-
],
|
|
270
|
-
}),
|
|
271
|
-
}),
|
|
272
|
-
],
|
|
273
|
-
}),
|
|
274
|
-
}),
|
|
275
|
-
],
|
|
276
|
-
});
|
|
277
|
-
return { name: queryName, ast };
|
|
278
|
-
};
|
|
279
|
-
exports.getManyPaginatedEdges = getManyPaginatedEdges;
|
|
280
|
-
const getManyPaginatedNodes = ({ operationName, query, fields, }) => {
|
|
281
|
-
const queryName = inflection_1.default.camelize(['get', inflection_1.default.underscore(operationName), 'query'].join('_'), true);
|
|
282
|
-
const Singular = query.model;
|
|
283
|
-
const Plural = operationName.charAt(0).toUpperCase() + operationName.slice(1);
|
|
284
|
-
const Condition = `${Singular}Condition`;
|
|
285
|
-
const Filter = `${Singular}Filter`;
|
|
286
|
-
const OrderBy = `${Plural}OrderBy`;
|
|
287
|
-
const selections = getSelections(query, fields);
|
|
288
|
-
const variableDefinitions = [
|
|
289
|
-
'first',
|
|
290
|
-
'last',
|
|
291
|
-
'after',
|
|
292
|
-
'before',
|
|
293
|
-
'offset',
|
|
294
|
-
].map((name) => t.variableDefinition({
|
|
295
|
-
variable: t.variable({ name }),
|
|
296
|
-
type: t.namedType({ type: name === 'after' || name === 'before' ? 'Cursor' : 'Int' }),
|
|
297
|
-
}));
|
|
298
|
-
variableDefinitions.push(t.variableDefinition({
|
|
299
|
-
variable: t.variable({ name: 'condition' }),
|
|
300
|
-
type: t.namedType({ type: Condition }),
|
|
301
|
-
}), t.variableDefinition({
|
|
302
|
-
variable: t.variable({ name: 'filter' }),
|
|
303
|
-
type: t.namedType({ type: Filter }),
|
|
304
|
-
}), t.variableDefinition({
|
|
305
|
-
variable: t.variable({ name: 'orderBy' }),
|
|
306
|
-
type: t.listType({
|
|
307
|
-
type: t.nonNullType({
|
|
308
|
-
type: t.namedType({ type: OrderBy }),
|
|
309
|
-
}),
|
|
310
|
-
}),
|
|
311
|
-
}));
|
|
312
|
-
const args = [
|
|
313
|
-
'first',
|
|
314
|
-
'last',
|
|
315
|
-
'offset',
|
|
316
|
-
'after',
|
|
317
|
-
'before',
|
|
318
|
-
'condition',
|
|
319
|
-
'filter',
|
|
320
|
-
'orderBy',
|
|
321
|
-
].map((name) => t.argument({
|
|
322
|
-
name,
|
|
323
|
-
value: t.variable({ name }),
|
|
324
|
-
}));
|
|
325
|
-
const ast = t.document({
|
|
326
|
-
definitions: [
|
|
327
|
-
t.operationDefinition({
|
|
328
|
-
operation: 'query',
|
|
329
|
-
name: queryName,
|
|
330
|
-
variableDefinitions,
|
|
331
|
-
selectionSet: t.selectionSet({
|
|
332
|
-
selections: [
|
|
333
|
-
t.field({
|
|
334
|
-
name: operationName,
|
|
335
|
-
args,
|
|
336
|
-
selectionSet: t.selectionSet({
|
|
337
|
-
selections: [
|
|
338
|
-
t.field({ name: 'totalCount' }),
|
|
339
|
-
t.field({
|
|
340
|
-
name: 'pageInfo',
|
|
341
|
-
selectionSet: t.selectionSet({
|
|
342
|
-
selections: [
|
|
343
|
-
t.field({ name: 'hasNextPage' }),
|
|
344
|
-
t.field({ name: 'hasPreviousPage' }),
|
|
345
|
-
t.field({ name: 'endCursor' }),
|
|
346
|
-
t.field({ name: 'startCursor' }),
|
|
347
|
-
],
|
|
348
|
-
}),
|
|
349
|
-
}),
|
|
350
|
-
t.field({
|
|
351
|
-
name: 'nodes',
|
|
352
|
-
selectionSet: t.selectionSet({ selections }),
|
|
353
|
-
}),
|
|
354
|
-
],
|
|
355
|
-
}),
|
|
356
|
-
}),
|
|
357
|
-
],
|
|
358
|
-
}),
|
|
359
|
-
}),
|
|
360
|
-
],
|
|
361
|
-
});
|
|
362
|
-
return { name: queryName, ast };
|
|
363
|
-
};
|
|
364
|
-
exports.getManyPaginatedNodes = getManyPaginatedNodes;
|
|
365
|
-
const getOrderByEnums = ({ operationName, query, }) => {
|
|
366
|
-
const queryName = inflection_1.default.camelize(['get', inflection_1.default.underscore(operationName), 'Order', 'By', 'Enums'].join('_'), true);
|
|
367
|
-
const Model = operationName.charAt(0).toUpperCase() + operationName.slice(1);
|
|
368
|
-
const OrderBy = `${Model}OrderBy`;
|
|
369
|
-
const ast = t.document({
|
|
370
|
-
definitions: [
|
|
371
|
-
t.operationDefinition({
|
|
372
|
-
operation: 'query',
|
|
373
|
-
name: queryName,
|
|
374
|
-
selectionSet: t.selectionSet({
|
|
375
|
-
selections: [
|
|
376
|
-
t.field({
|
|
377
|
-
name: '__type',
|
|
378
|
-
args: [
|
|
379
|
-
t.argument({
|
|
380
|
-
name: 'name',
|
|
381
|
-
value: t.stringValue({ value: OrderBy }),
|
|
382
|
-
}),
|
|
383
|
-
],
|
|
384
|
-
selectionSet: t.selectionSet({
|
|
385
|
-
selections: [
|
|
386
|
-
t.field({
|
|
387
|
-
name: 'enumValues',
|
|
388
|
-
selectionSet: t.selectionSet({
|
|
389
|
-
selections: [t.field({ name: 'name' })],
|
|
390
|
-
}),
|
|
391
|
-
}),
|
|
392
|
-
],
|
|
393
|
-
}),
|
|
394
|
-
}),
|
|
395
|
-
],
|
|
396
|
-
}),
|
|
397
|
-
}),
|
|
398
|
-
],
|
|
399
|
-
});
|
|
400
|
-
return { name: queryName, ast };
|
|
401
|
-
};
|
|
402
|
-
exports.getOrderByEnums = getOrderByEnums;
|
|
403
|
-
const getFragment = ({ operationName, query, }) => {
|
|
404
|
-
const queryName = inflection_1.default.camelize([inflection_1.default.underscore(query.model), 'Fragment'].join('_'), true);
|
|
405
|
-
const selections = getSelections(query);
|
|
406
|
-
const ast = t.document({
|
|
407
|
-
definitions: [
|
|
408
|
-
t.fragmentDefinition({
|
|
409
|
-
name: queryName,
|
|
410
|
-
typeCondition: t.namedType({
|
|
411
|
-
type: query.model,
|
|
412
|
-
}),
|
|
413
|
-
selectionSet: t.selectionSet({
|
|
414
|
-
selections,
|
|
415
|
-
}),
|
|
416
|
-
}),
|
|
417
|
-
],
|
|
418
|
-
});
|
|
419
|
-
return { name: queryName, ast };
|
|
420
|
-
};
|
|
421
|
-
exports.getFragment = getFragment;
|
|
422
|
-
const getOne = ({ operationName, query, fields, }, typeNameOverrides) => {
|
|
423
|
-
const queryName = inflection_1.default.camelize(['get', inflection_1.default.underscore(operationName), 'query'].join('_'), true);
|
|
424
|
-
const variableDefinitions = objectToArray(query.properties)
|
|
425
|
-
.filter((field) => field.isNotNull)
|
|
426
|
-
.map(({ name, type, isNotNull, isArray, isArrayNotNull }) => {
|
|
427
|
-
const typeName = resolveTypeName(name, type, typeNameOverrides);
|
|
428
|
-
let gqlType = t.namedType({ type: typeName });
|
|
429
|
-
if (isNotNull) {
|
|
430
|
-
gqlType = t.nonNullType({ type: gqlType });
|
|
431
|
-
}
|
|
432
|
-
if (isArray) {
|
|
433
|
-
gqlType = t.listType({ type: gqlType });
|
|
434
|
-
if (isArrayNotNull) {
|
|
435
|
-
gqlType = t.nonNullType({ type: gqlType });
|
|
436
|
-
}
|
|
437
|
-
}
|
|
438
|
-
return t.variableDefinition({
|
|
439
|
-
variable: t.variable({ name }),
|
|
440
|
-
type: gqlType,
|
|
441
|
-
});
|
|
442
|
-
});
|
|
443
|
-
const selectArgs = objectToArray(query.properties)
|
|
444
|
-
.filter((field) => field.isNotNull)
|
|
445
|
-
.map((field) => t.argument({
|
|
446
|
-
name: field.name,
|
|
447
|
-
value: t.variable({ name: field.name }),
|
|
448
|
-
}));
|
|
449
|
-
const selections = getSelections(query, fields);
|
|
450
|
-
const opSel = [
|
|
451
|
-
t.field({
|
|
452
|
-
name: operationName,
|
|
453
|
-
args: selectArgs,
|
|
454
|
-
selectionSet: t.selectionSet({ selections }),
|
|
455
|
-
}),
|
|
456
|
-
];
|
|
457
|
-
const ast = t.document({
|
|
458
|
-
definitions: [
|
|
459
|
-
t.operationDefinition({
|
|
460
|
-
operation: 'query',
|
|
461
|
-
name: queryName,
|
|
462
|
-
variableDefinitions,
|
|
463
|
-
selectionSet: t.selectionSet({ selections: opSel }),
|
|
464
|
-
}),
|
|
465
|
-
],
|
|
466
|
-
});
|
|
467
|
-
return { name: queryName, ast };
|
|
468
|
-
};
|
|
469
|
-
exports.getOne = getOne;
|
|
470
|
-
const createOne = ({ operationName, mutation, selection, }, typeNameOverrides, typeIndex) => {
|
|
471
|
-
const mutationName = inflection_1.default.camelize([inflection_1.default.underscore(operationName), 'mutation'].join('_'), true);
|
|
472
|
-
if (!mutation.properties?.input?.properties) {
|
|
473
|
-
console.log('no input field for mutation for ' + mutationName);
|
|
474
|
-
return;
|
|
475
|
-
}
|
|
476
|
-
const modelName = inflection_1.default.camelize([singularModel(mutation.model)].join('_'), true);
|
|
477
|
-
const allAttrs = objectToArray(mutation.properties.input.properties[modelName].properties);
|
|
478
|
-
const attrs = allAttrs.filter((field) => field.name === 'id' ? Boolean(field.isNotNull) : !NON_MUTABLE_PROPS.includes(field.name));
|
|
479
|
-
const useRaw = selection?.mutationInputMode === 'raw';
|
|
480
|
-
const inputTypeName = resolveTypeName('input', mutation.properties?.input?.type || mutation.properties?.input, typeNameOverrides);
|
|
481
|
-
let unresolved = 0;
|
|
482
|
-
let modelInputName = null;
|
|
483
|
-
if (typeIndex && inputTypeName) {
|
|
484
|
-
const modelRef = typeIndex.getInputFieldType(inputTypeName, modelName);
|
|
485
|
-
modelInputName = refToNamedTypeName(modelRef);
|
|
486
|
-
}
|
|
487
|
-
const variableDefinitions = attrs.map(({ name, type, isNotNull, isArray, isArrayNotNull }) => {
|
|
488
|
-
let gqlType = null;
|
|
489
|
-
if (typeIndex && modelInputName) {
|
|
490
|
-
const fieldTypeRef = typeIndex.getInputFieldType(modelInputName, name);
|
|
491
|
-
const tn = refToTypeNode(fieldTypeRef, typeNameOverrides);
|
|
492
|
-
if (tn)
|
|
493
|
-
gqlType = tn;
|
|
494
|
-
}
|
|
495
|
-
if (!gqlType) {
|
|
496
|
-
const typeName = resolveTypeName(name, type, typeNameOverrides);
|
|
497
|
-
gqlType = t.namedType({ type: typeName });
|
|
498
|
-
if (isNotNull) {
|
|
499
|
-
gqlType = t.nonNullType({ type: gqlType });
|
|
500
|
-
}
|
|
501
|
-
if (isArray) {
|
|
502
|
-
gqlType = t.listType({ type: gqlType });
|
|
503
|
-
if (isArrayNotNull) {
|
|
504
|
-
gqlType = t.nonNullType({ type: gqlType });
|
|
505
|
-
}
|
|
506
|
-
}
|
|
507
|
-
}
|
|
508
|
-
const nn = extractNamedTypeName(gqlType);
|
|
509
|
-
if (nn === 'JSON')
|
|
510
|
-
unresolved++;
|
|
511
|
-
return t.variableDefinition({ variable: t.variable({ name }), type: gqlType });
|
|
512
|
-
});
|
|
513
|
-
const mustUseRaw = useRaw || unresolved > 0;
|
|
514
|
-
const selectArgs = mustUseRaw
|
|
515
|
-
? [t.argument({ name: 'input', value: t.variable({ name: 'input' }) })]
|
|
516
|
-
: [
|
|
517
|
-
t.argument({
|
|
518
|
-
name: 'input',
|
|
519
|
-
value: t.objectValue({
|
|
520
|
-
fields: [
|
|
521
|
-
t.objectField({
|
|
522
|
-
name: modelName,
|
|
523
|
-
value: t.objectValue({
|
|
524
|
-
fields: attrs.map((field) => t.objectField({ name: field.name, value: t.variable({ name: field.name }) })),
|
|
525
|
-
}),
|
|
526
|
-
}),
|
|
527
|
-
],
|
|
528
|
-
}),
|
|
529
|
-
}),
|
|
530
|
-
];
|
|
531
|
-
let idExists = true;
|
|
532
|
-
let availableFieldNames = [];
|
|
533
|
-
if (typeIndex) {
|
|
534
|
-
const typ = typeIndex.byName?.[mutation.model];
|
|
535
|
-
const fields = (typ && Array.isArray(typ.fields)) ? typ.fields : [];
|
|
536
|
-
idExists = fields.some((f) => f && f.name === 'id');
|
|
537
|
-
availableFieldNames = fields
|
|
538
|
-
.filter((f) => {
|
|
539
|
-
let r = f.type;
|
|
540
|
-
while (r && (r.kind === 'NON_NULL' || r.kind === 'LIST'))
|
|
541
|
-
r = r.ofType;
|
|
542
|
-
const kind = r?.kind;
|
|
543
|
-
return kind === 'SCALAR' || kind === 'ENUM';
|
|
544
|
-
})
|
|
545
|
-
.map((f) => f.name);
|
|
546
|
-
}
|
|
547
|
-
const finalFields = Array.from(new Set([...(idExists ? ['id'] : []), ...availableFieldNames]));
|
|
548
|
-
const nested = (finalFields.length > 0)
|
|
549
|
-
? [t.field({
|
|
550
|
-
name: modelName,
|
|
551
|
-
selectionSet: t.selectionSet({ selections: finalFields.map((f) => t.field({ name: f })) }),
|
|
552
|
-
})]
|
|
553
|
-
: [];
|
|
554
|
-
const ast = (0, exports.createGqlMutation)({
|
|
555
|
-
operationName,
|
|
556
|
-
mutationName,
|
|
557
|
-
selectArgs,
|
|
558
|
-
selections: [...nested, t.field({ name: 'clientMutationId' })],
|
|
559
|
-
variableDefinitions: mustUseRaw
|
|
560
|
-
? [t.variableDefinition({ variable: t.variable({ name: 'input' }), type: t.nonNullType({ type: t.namedType({ type: inputTypeName }) }) })]
|
|
561
|
-
: variableDefinitions,
|
|
562
|
-
useModel: false,
|
|
563
|
-
});
|
|
564
|
-
return { name: mutationName, ast };
|
|
565
|
-
};
|
|
566
|
-
exports.createOne = createOne;
|
|
567
|
-
const patchOne = ({ operationName, mutation, selection, }, typeNameOverrides, typeIndex) => {
|
|
568
|
-
const mutationName = inflection_1.default.camelize([inflection_1.default.underscore(operationName), 'mutation'].join('_'), true);
|
|
569
|
-
if (!mutation.properties?.input?.properties) {
|
|
570
|
-
console.log('no input field for mutation for ' + mutationName);
|
|
571
|
-
return;
|
|
572
|
-
}
|
|
573
|
-
const modelName = inflection_1.default.camelize([singularModel(mutation.model)].join('_'), true);
|
|
574
|
-
// @ts-ignore
|
|
575
|
-
const allAttrs = objectToArray(mutation.properties.input.properties['patch']?.properties || {});
|
|
576
|
-
const patchAttrs = allAttrs.filter(
|
|
577
|
-
// @ts-ignore
|
|
578
|
-
(prop) => !NON_MUTABLE_PROPS.includes(prop.name));
|
|
579
|
-
const patchByAttrs = objectToArray(mutation.properties.input.properties).filter((n) => n.name !== 'patch');
|
|
580
|
-
const patchers = patchByAttrs.map((p) => p.name);
|
|
581
|
-
const useCollapsedOpt = selection?.mutationInputMode === 'patchCollapsed';
|
|
582
|
-
const ModelPascal = inflection_1.default.camelize(singularModel(mutation.model), false);
|
|
583
|
-
const patchTypeName = `${ModelPascal}Patch`;
|
|
584
|
-
const inputTypeName = resolveTypeName('input', mutation.properties?.input?.type || mutation.properties?.input, typeNameOverrides);
|
|
585
|
-
let unresolved = 0;
|
|
586
|
-
const patchAttrVarDefs = useCollapsedOpt
|
|
587
|
-
? [
|
|
588
|
-
t.variableDefinition({
|
|
589
|
-
variable: t.variable({ name: 'patch' }),
|
|
590
|
-
type: t.nonNullType({ type: t.namedType({ type: patchTypeName }) }),
|
|
591
|
-
}),
|
|
592
|
-
]
|
|
593
|
-
: patchAttrs
|
|
594
|
-
.filter((field) => !patchers.includes(field.name))
|
|
595
|
-
.map(({ name, type, isArray }) => {
|
|
596
|
-
let gqlType = null;
|
|
597
|
-
if (typeIndex) {
|
|
598
|
-
const pType = typeIndex.byName[patchTypeName];
|
|
599
|
-
const f = pType && pType.inputFields && pType.inputFields.find((x) => x.name === name);
|
|
600
|
-
if (f && f.type)
|
|
601
|
-
gqlType = refToTypeNode(f.type, typeNameOverrides);
|
|
602
|
-
}
|
|
603
|
-
if (!gqlType) {
|
|
604
|
-
const typeName = resolveTypeName(name, type, typeNameOverrides);
|
|
605
|
-
gqlType = t.namedType({ type: typeName });
|
|
606
|
-
if (isArray) {
|
|
607
|
-
gqlType = t.listType({ type: gqlType });
|
|
608
|
-
}
|
|
609
|
-
if (patchers.includes(name)) {
|
|
610
|
-
gqlType = t.nonNullType({ type: gqlType });
|
|
611
|
-
}
|
|
612
|
-
}
|
|
613
|
-
const nn = extractNamedTypeName(gqlType);
|
|
614
|
-
if (nn === 'JSON')
|
|
615
|
-
unresolved++;
|
|
616
|
-
return t.variableDefinition({ variable: t.variable({ name }), type: gqlType });
|
|
617
|
-
});
|
|
618
|
-
const patchByVarDefs = patchByAttrs.map(({ name, type, isNotNull, isArray, isArrayNotNull }) => {
|
|
619
|
-
let gqlType = null;
|
|
620
|
-
if (typeIndex && inputTypeName) {
|
|
621
|
-
const ref = typeIndex.getInputFieldType(inputTypeName, name);
|
|
622
|
-
const tn = refToTypeNode(ref, typeNameOverrides);
|
|
623
|
-
if (tn)
|
|
624
|
-
gqlType = tn;
|
|
625
|
-
}
|
|
626
|
-
if (!gqlType) {
|
|
627
|
-
const typeName = resolveTypeName(name, type, typeNameOverrides);
|
|
628
|
-
gqlType = t.namedType({ type: typeName });
|
|
629
|
-
if (isNotNull) {
|
|
630
|
-
gqlType = t.nonNullType({ type: gqlType });
|
|
631
|
-
}
|
|
632
|
-
if (isArray) {
|
|
633
|
-
gqlType = t.listType({ type: gqlType });
|
|
634
|
-
if (isArrayNotNull) {
|
|
635
|
-
gqlType = t.nonNullType({ type: gqlType });
|
|
636
|
-
}
|
|
637
|
-
}
|
|
638
|
-
}
|
|
639
|
-
const nn = extractNamedTypeName(gqlType);
|
|
640
|
-
if (nn === 'JSON')
|
|
641
|
-
unresolved++;
|
|
642
|
-
return t.variableDefinition({ variable: t.variable({ name }), type: gqlType });
|
|
643
|
-
});
|
|
644
|
-
const mustUseRaw = unresolved > 0;
|
|
645
|
-
const selectArgs = mustUseRaw
|
|
646
|
-
? [t.argument({ name: 'input', value: t.variable({ name: 'input' }) })]
|
|
647
|
-
: [
|
|
648
|
-
t.argument({
|
|
649
|
-
name: 'input',
|
|
650
|
-
value: t.objectValue({
|
|
651
|
-
fields: [
|
|
652
|
-
...patchByAttrs.map((field) => t.objectField({ name: field.name, value: t.variable({ name: field.name }) })),
|
|
653
|
-
t.objectField({
|
|
654
|
-
name: 'patch',
|
|
655
|
-
value: useCollapsedOpt ? t.variable({ name: 'patch' }) : t.objectValue({
|
|
656
|
-
fields: patchAttrs
|
|
657
|
-
.filter((field) => !patchers.includes(field.name))
|
|
658
|
-
.map((field) => t.objectField({ name: field.name, value: t.variable({ name: field.name }) })),
|
|
659
|
-
}),
|
|
660
|
-
}),
|
|
661
|
-
],
|
|
662
|
-
}),
|
|
663
|
-
}),
|
|
664
|
-
];
|
|
665
|
-
let idExistsPatch = true;
|
|
666
|
-
if (typeIndex) {
|
|
667
|
-
const typ = typeIndex.byName?.[mutation.model];
|
|
668
|
-
const fields = (typ && Array.isArray(typ.fields)) ? typ.fields : [];
|
|
669
|
-
idExistsPatch = fields.some((f) => f && f.name === 'id');
|
|
670
|
-
}
|
|
671
|
-
const shouldDropIdPatch = /Extension$/i.test(modelName) || !idExistsPatch;
|
|
672
|
-
const idSelection = shouldDropIdPatch ? [] : ['id'];
|
|
673
|
-
const nestedPatch = (idSelection.length > 0)
|
|
674
|
-
? [t.field({
|
|
675
|
-
name: modelName,
|
|
676
|
-
selectionSet: t.selectionSet({ selections: idSelection.map((f) => t.field({ name: f })) }),
|
|
677
|
-
})]
|
|
678
|
-
: [];
|
|
679
|
-
const ast = (0, exports.createGqlMutation)({
|
|
680
|
-
operationName,
|
|
681
|
-
mutationName,
|
|
682
|
-
selectArgs,
|
|
683
|
-
selections: [...nestedPatch, t.field({ name: 'clientMutationId' })],
|
|
684
|
-
variableDefinitions: mustUseRaw
|
|
685
|
-
? [t.variableDefinition({ variable: t.variable({ name: 'input' }), type: t.nonNullType({ type: t.namedType({ type: inputTypeName }) }) })]
|
|
686
|
-
: [...patchByVarDefs, ...patchAttrVarDefs],
|
|
687
|
-
useModel: false,
|
|
688
|
-
});
|
|
689
|
-
return { name: mutationName, ast };
|
|
690
|
-
};
|
|
691
|
-
exports.patchOne = patchOne;
|
|
692
|
-
const deleteOne = ({ operationName, mutation, }, typeNameOverrides, typeIndex) => {
|
|
693
|
-
const mutationName = inflection_1.default.camelize([inflection_1.default.underscore(operationName), 'mutation'].join('_'), true);
|
|
694
|
-
if (!mutation.properties?.input?.properties) {
|
|
695
|
-
console.log('no input field for mutation for ' + mutationName);
|
|
696
|
-
return;
|
|
697
|
-
}
|
|
698
|
-
const modelName = inflection_1.default.camelize([singularModel(mutation.model)].join('_'), true);
|
|
699
|
-
// @ts-ignore
|
|
700
|
-
const deleteAttrs = objectToArray(mutation.properties.input.properties);
|
|
701
|
-
const inputTypeName = resolveTypeName('input', mutation.properties?.input?.type || mutation.properties?.input, typeNameOverrides);
|
|
702
|
-
let unresolved = 0;
|
|
703
|
-
const variableDefinitions = deleteAttrs.map(({ name, type, isNotNull, isArray }) => {
|
|
704
|
-
let gqlType = null;
|
|
705
|
-
if (typeIndex && inputTypeName) {
|
|
706
|
-
const ref = typeIndex.getInputFieldType(inputTypeName, name);
|
|
707
|
-
const tn = refToTypeNode(ref, typeNameOverrides);
|
|
708
|
-
if (tn)
|
|
709
|
-
gqlType = tn;
|
|
710
|
-
}
|
|
711
|
-
if (!gqlType) {
|
|
712
|
-
const typeName = resolveTypeName(name, type, typeNameOverrides);
|
|
713
|
-
gqlType = t.namedType({ type: typeName });
|
|
714
|
-
if (isNotNull) {
|
|
715
|
-
gqlType = t.nonNullType({ type: gqlType });
|
|
716
|
-
}
|
|
717
|
-
if (isArray) {
|
|
718
|
-
gqlType = t.listType({ type: gqlType });
|
|
719
|
-
gqlType = t.nonNullType({ type: gqlType });
|
|
720
|
-
}
|
|
721
|
-
}
|
|
722
|
-
const nn = extractNamedTypeName(gqlType);
|
|
723
|
-
if (nn === 'JSON')
|
|
724
|
-
unresolved++;
|
|
725
|
-
return t.variableDefinition({ variable: t.variable({ name }), type: gqlType });
|
|
726
|
-
});
|
|
727
|
-
const mustUseRaw = unresolved > 0;
|
|
728
|
-
const selectArgs = mustUseRaw
|
|
729
|
-
? [t.argument({ name: 'input', value: t.variable({ name: 'input' }) })]
|
|
730
|
-
: [
|
|
731
|
-
t.argument({
|
|
732
|
-
name: 'input',
|
|
733
|
-
value: t.objectValue({
|
|
734
|
-
fields: deleteAttrs.map((f) => t.objectField({ name: f.name, value: t.variable({ name: f.name }) })),
|
|
735
|
-
}),
|
|
736
|
-
}),
|
|
737
|
-
];
|
|
738
|
-
const selections = [t.field({ name: 'clientMutationId' })];
|
|
739
|
-
const ast = (0, exports.createGqlMutation)({
|
|
740
|
-
operationName,
|
|
741
|
-
mutationName,
|
|
742
|
-
selectArgs,
|
|
743
|
-
selections,
|
|
744
|
-
variableDefinitions: mustUseRaw
|
|
745
|
-
? [t.variableDefinition({ variable: t.variable({ name: 'input' }), type: t.nonNullType({ type: t.namedType({ type: inputTypeName }) }) })]
|
|
746
|
-
: variableDefinitions,
|
|
747
|
-
modelName,
|
|
748
|
-
useModel: false,
|
|
749
|
-
});
|
|
750
|
-
return { name: mutationName, ast };
|
|
751
|
-
};
|
|
752
|
-
exports.deleteOne = deleteOne;
|
|
753
|
-
const createMutation = ({ operationName, mutation, selection, }, typeNameOverrides, typeIndex) => {
|
|
754
|
-
const mutationName = inflection_1.default.camelize([inflection_1.default.underscore(operationName), 'mutation'].join('_'), true);
|
|
755
|
-
if (!mutation.properties?.input?.properties) {
|
|
756
|
-
console.log('no input field for mutation for ' + mutationName);
|
|
757
|
-
return;
|
|
758
|
-
}
|
|
759
|
-
// @ts-ignore
|
|
760
|
-
const otherAttrs = objectToArray(mutation.properties.input.properties);
|
|
761
|
-
const useRaw = selection?.mutationInputMode === 'raw';
|
|
762
|
-
const inputTypeName = resolveTypeName('input', mutation.properties?.input?.type || mutation.properties?.input, typeNameOverrides);
|
|
763
|
-
let unresolved = 0;
|
|
764
|
-
const builtVarDefs = otherAttrs.map(({ name, type, isArray, isArrayNotNull }) => {
|
|
765
|
-
let gqlType = null;
|
|
766
|
-
if (typeIndex && inputTypeName) {
|
|
767
|
-
const ref = typeIndex.getInputFieldType(inputTypeName, name);
|
|
768
|
-
const tn = refToTypeNode(ref, typeNameOverrides);
|
|
769
|
-
if (tn)
|
|
770
|
-
gqlType = tn;
|
|
771
|
-
}
|
|
772
|
-
if (!gqlType) {
|
|
773
|
-
const typeName = resolveTypeName(name, type, typeNameOverrides);
|
|
774
|
-
gqlType = t.namedType({ type: typeName });
|
|
775
|
-
gqlType = t.nonNullType({ type: gqlType });
|
|
776
|
-
if (isArray) {
|
|
777
|
-
gqlType = t.listType({ type: gqlType });
|
|
778
|
-
if (isArrayNotNull) {
|
|
779
|
-
gqlType = t.nonNullType({ type: gqlType });
|
|
780
|
-
}
|
|
781
|
-
}
|
|
782
|
-
if (gqlType.type && gqlType.type.type && gqlType.type.type.name === 'JSON') {
|
|
783
|
-
unresolved++;
|
|
784
|
-
}
|
|
785
|
-
}
|
|
786
|
-
return t.variableDefinition({ variable: t.variable({ name }), type: gqlType });
|
|
787
|
-
});
|
|
788
|
-
const mustUseRaw = useRaw || otherAttrs.length === 0 || unresolved > 0;
|
|
789
|
-
const variableDefinitions = mustUseRaw
|
|
790
|
-
? [t.variableDefinition({ variable: t.variable({ name: 'input' }), type: t.nonNullType({ type: t.namedType({ type: inputTypeName }) }) })]
|
|
791
|
-
: builtVarDefs;
|
|
792
|
-
const selectArgs = [
|
|
793
|
-
t.argument({
|
|
794
|
-
name: 'input',
|
|
795
|
-
value: mustUseRaw
|
|
796
|
-
? t.variable({ name: 'input' })
|
|
797
|
-
: t.objectValue({
|
|
798
|
-
fields: otherAttrs.map((f) => t.objectField({ name: f.name, value: t.variable({ name: f.name }) })),
|
|
799
|
-
}),
|
|
800
|
-
}),
|
|
801
|
-
];
|
|
802
|
-
const scalarOutputs = (mutation.outputs || [])
|
|
803
|
-
.filter((field) => field.type.kind === 'SCALAR')
|
|
804
|
-
.map((f) => f.name);
|
|
805
|
-
let objectOutputName = (mutation.outputs || [])
|
|
806
|
-
.find((field) => field.type.kind === 'OBJECT')?.name;
|
|
807
|
-
if (!objectOutputName) {
|
|
808
|
-
const payloadTypeName = mutation?.output?.name;
|
|
809
|
-
if (typeIndex && payloadTypeName) {
|
|
810
|
-
const payloadType = typeIndex.byName?.[payloadTypeName];
|
|
811
|
-
const fields = (payloadType && Array.isArray(payloadType.fields)) ? payloadType.fields : [];
|
|
812
|
-
const match = fields
|
|
813
|
-
.filter((f) => f && f.name !== 'clientMutationId')
|
|
814
|
-
.filter((f) => (refToNamedTypeName(f.type) || f.type?.name) !== 'Query')
|
|
815
|
-
.find((f) => (refToNamedTypeName(f.type) || f.type?.name) === mutation?.model);
|
|
816
|
-
if (match)
|
|
817
|
-
objectOutputName = match.name;
|
|
818
|
-
}
|
|
819
|
-
}
|
|
820
|
-
const selections = [];
|
|
821
|
-
if (objectOutputName) {
|
|
822
|
-
const modelTypeName = mutation?.model;
|
|
823
|
-
const modelType = typeIndex && modelTypeName ? typeIndex.byName?.[modelTypeName] : null;
|
|
824
|
-
const fieldNames = (modelType && Array.isArray(modelType.fields))
|
|
825
|
-
? modelType.fields
|
|
826
|
-
.filter((f) => {
|
|
827
|
-
let r = f.type;
|
|
828
|
-
while (r && (r.kind === 'NON_NULL' || r.kind === 'LIST'))
|
|
829
|
-
r = r.ofType;
|
|
830
|
-
const kind = r?.kind;
|
|
831
|
-
return kind === 'SCALAR' || kind === 'ENUM';
|
|
832
|
-
})
|
|
833
|
-
.map((f) => f.name)
|
|
834
|
-
: [];
|
|
835
|
-
selections.push(t.field({
|
|
836
|
-
name: objectOutputName,
|
|
837
|
-
selectionSet: t.selectionSet({ selections: fieldNames.map((n) => t.field({ name: n })) }),
|
|
838
|
-
}));
|
|
839
|
-
}
|
|
840
|
-
if (scalarOutputs.length > 0) {
|
|
841
|
-
selections.push(...scalarOutputs.map((o) => t.field({ name: o })));
|
|
842
|
-
}
|
|
843
|
-
else {
|
|
844
|
-
selections.push(t.field({ name: 'clientMutationId' }));
|
|
845
|
-
}
|
|
846
|
-
const ast = (0, exports.createGqlMutation)({
|
|
847
|
-
operationName,
|
|
848
|
-
mutationName,
|
|
849
|
-
selectArgs,
|
|
850
|
-
selections,
|
|
851
|
-
variableDefinitions,
|
|
852
|
-
});
|
|
853
|
-
return { name: mutationName, ast };
|
|
854
|
-
};
|
|
855
|
-
exports.createMutation = createMutation;
|
|
856
|
-
const generate = (gql, selection, typeNameOverrides, typeIndex) => {
|
|
857
|
-
return Object.keys(gql).reduce((m, operationName) => {
|
|
858
|
-
const defn = gql[operationName];
|
|
859
|
-
let name;
|
|
860
|
-
let ast;
|
|
861
|
-
if (defn.qtype === 'mutation') {
|
|
862
|
-
if (defn.mutationType === 'create') {
|
|
863
|
-
({ name, ast } = (0, exports.createOne)({ operationName, mutation: defn, selection }, typeNameOverrides, typeIndex) ?? {});
|
|
864
|
-
}
|
|
865
|
-
else if (defn.mutationType === 'patch') {
|
|
866
|
-
({ name, ast } = (0, exports.patchOne)({ operationName, mutation: defn, selection }, typeNameOverrides, typeIndex) ?? {});
|
|
867
|
-
}
|
|
868
|
-
else if (defn.mutationType === 'delete') {
|
|
869
|
-
({ name, ast } = (0, exports.deleteOne)({ operationName, mutation: defn }, typeNameOverrides, typeIndex) ?? {});
|
|
870
|
-
}
|
|
871
|
-
else {
|
|
872
|
-
({ name, ast } = (0, exports.createMutation)({ operationName, mutation: defn, selection }, typeNameOverrides, typeIndex) ?? {});
|
|
873
|
-
}
|
|
874
|
-
}
|
|
875
|
-
else if (defn.qtype === 'getMany') {
|
|
876
|
-
[
|
|
877
|
-
exports.getMany,
|
|
878
|
-
exports.getManyPaginatedEdges,
|
|
879
|
-
exports.getOrderByEnums,
|
|
880
|
-
exports.getFragment
|
|
881
|
-
].forEach(fn => {
|
|
882
|
-
const result = fn({ operationName, query: defn });
|
|
883
|
-
if (result?.name && result?.ast) {
|
|
884
|
-
m[result.name] = result;
|
|
885
|
-
}
|
|
886
|
-
});
|
|
887
|
-
}
|
|
888
|
-
else if (defn.qtype === 'getOne') {
|
|
889
|
-
// @ts-ignore
|
|
890
|
-
({ name, ast } = (0, exports.getOne)({ operationName, query: defn }, typeNameOverrides) ?? {});
|
|
891
|
-
}
|
|
892
|
-
else {
|
|
893
|
-
console.warn('Unknown qtype for key: ' + operationName);
|
|
894
|
-
}
|
|
895
|
-
if (name && ast) {
|
|
896
|
-
m[name] = { name, ast };
|
|
897
|
-
}
|
|
898
|
-
return m;
|
|
899
|
-
}, {});
|
|
900
|
-
};
|
|
901
|
-
exports.generate = generate;
|
|
902
|
-
const generateGranular = (gql, model, fields) => {
|
|
903
|
-
return Object.keys(gql).reduce((m, operationName) => {
|
|
904
|
-
const defn = gql[operationName];
|
|
905
|
-
const matchModel = defn.model;
|
|
906
|
-
let name;
|
|
907
|
-
let ast;
|
|
908
|
-
if (defn.qtype === 'getMany') {
|
|
909
|
-
const many = (0, exports.getMany)({ operationName, query: defn, fields });
|
|
910
|
-
if (many?.name && many?.ast && model === matchModel) {
|
|
911
|
-
m[many.name] = many;
|
|
912
|
-
}
|
|
913
|
-
const paginatedEdges = (0, exports.getManyPaginatedEdges)({
|
|
914
|
-
operationName,
|
|
915
|
-
query: defn,
|
|
916
|
-
fields,
|
|
917
|
-
});
|
|
918
|
-
if (paginatedEdges?.name && paginatedEdges?.ast && model === matchModel) {
|
|
919
|
-
m[paginatedEdges.name] = paginatedEdges;
|
|
920
|
-
}
|
|
921
|
-
const paginatedNodes = (0, exports.getManyPaginatedNodes)({
|
|
922
|
-
operationName,
|
|
923
|
-
query: defn,
|
|
924
|
-
fields,
|
|
925
|
-
});
|
|
926
|
-
if (paginatedNodes?.name && paginatedNodes?.ast && model === matchModel) {
|
|
927
|
-
m[paginatedNodes.name] = paginatedNodes;
|
|
928
|
-
}
|
|
929
|
-
}
|
|
930
|
-
else if (defn.qtype === 'getOne') {
|
|
931
|
-
const one = (0, exports.getOne)({ operationName, query: defn, fields });
|
|
932
|
-
if (one?.name && one?.ast && model === matchModel) {
|
|
933
|
-
m[one.name] = one;
|
|
934
|
-
}
|
|
935
|
-
}
|
|
936
|
-
return m;
|
|
937
|
-
}, {});
|
|
938
|
-
};
|
|
939
|
-
exports.generateGranular = generateGranular;
|
|
940
|
-
function getSelections(query, fields = []) {
|
|
941
|
-
const useAll = fields.length === 0;
|
|
942
|
-
const shouldDropId = typeof query.model === 'string' && /Extension$/i.test(query.model);
|
|
943
|
-
const mapItem = (item) => {
|
|
944
|
-
if (typeof item === 'string') {
|
|
945
|
-
if (shouldDropId && item === 'id')
|
|
946
|
-
return null;
|
|
947
|
-
if (!useAll && !fields.includes(item))
|
|
948
|
-
return null;
|
|
949
|
-
return t.field({ name: item });
|
|
950
|
-
}
|
|
951
|
-
if (typeof item === 'object' &&
|
|
952
|
-
item !== null &&
|
|
953
|
-
'name' in item &&
|
|
954
|
-
'selection' in item &&
|
|
955
|
-
Array.isArray(item.selection)) {
|
|
956
|
-
if (!useAll && !fields.includes(item.name))
|
|
957
|
-
return null;
|
|
958
|
-
const isMany = item.qtype === 'getMany';
|
|
959
|
-
if (isMany) {
|
|
960
|
-
return t.field({
|
|
961
|
-
name: item.name,
|
|
962
|
-
args: [t.argument({ name: 'first', value: t.intValue({ value: '3' }) })],
|
|
963
|
-
selectionSet: t.selectionSet({
|
|
964
|
-
selections: [
|
|
965
|
-
t.field({
|
|
966
|
-
name: 'edges',
|
|
967
|
-
selectionSet: t.selectionSet({
|
|
968
|
-
selections: [
|
|
969
|
-
t.field({ name: 'cursor' }),
|
|
970
|
-
t.field({
|
|
971
|
-
name: 'node',
|
|
972
|
-
selectionSet: t.selectionSet({ selections: item.selection.map((s) => mapItem(s)).filter(Boolean) }),
|
|
973
|
-
}),
|
|
974
|
-
],
|
|
975
|
-
}),
|
|
976
|
-
}),
|
|
977
|
-
],
|
|
978
|
-
}),
|
|
979
|
-
});
|
|
980
|
-
}
|
|
981
|
-
return t.field({
|
|
982
|
-
name: item.name,
|
|
983
|
-
selectionSet: t.selectionSet({ selections: item.selection.map((s) => mapItem(s)).filter(Boolean) }),
|
|
984
|
-
});
|
|
985
|
-
}
|
|
986
|
-
return null;
|
|
987
|
-
};
|
|
988
|
-
return query.selection
|
|
989
|
-
.filter((s) => !(shouldDropId && s === 'id'))
|
|
990
|
-
.map((field) => mapItem(field))
|
|
991
|
-
.filter((i) => Boolean(i));
|
|
992
|
-
}
|