UNPKG

3.22 kBJavaScriptView Raw
1"use strict";
2var __assign = (this && this.__assign) || Object.assign || function(t) {
3 for (var s, i = 1, n = arguments.length; i < n; i++) {
4 s = arguments[i];
5 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6 t[p] = s[p];
7 }
8 return t;
9};
10Object.defineProperty(exports, "__esModule", { value: true });
11var graphql_codegen_core_1 = require("graphql-codegen-core");
12var change_case_1 = require("change-case");
13exports.handleNameDuplications = function (name, existing) {
14 if (existing.find(function (model) { return model.modelType === name; })) {
15 return exports.handleNameDuplications('_' + name, existing);
16 }
17 return name;
18};
19function buildModelFromField(field, result) {
20 var modelName = exports.handleNameDuplications(change_case_1.pascalCase(field.name), result);
21 return {
22 schemaBaseType: field.type,
23 modelType: modelName,
24 fields: field.fields,
25 fragmentsSpread: field.fragmentsSpread,
26 inlineFragments: field.inlineFragments,
27 hasFields: field.hasFields,
28 hasFragmentsSpread: field.hasFragmentsSpread,
29 hasInlineFragments: field.hasInlineFragments
30 };
31}
32function buildModelFromInlineFragment(fragment, result) {
33 var modelName = exports.handleNameDuplications(change_case_1.pascalCase(fragment.onType) + 'InlineFragment', result);
34 return {
35 schemaBaseType: fragment.onType,
36 modelType: modelName,
37 fields: fragment.fields,
38 fragmentsSpread: fragment.fragmentsSpread,
39 inlineFragments: fragment.inlineFragments,
40 hasFields: fragment.hasFields,
41 hasFragmentsSpread: fragment.hasFragmentsSpread,
42 hasInlineFragments: fragment.hasInlineFragments
43 };
44}
45function flattenSelectionSet(selectionSet, result) {
46 if (result === void 0) { result = []; }
47 selectionSet.forEach(function (item) {
48 if (graphql_codegen_core_1.isFieldNode(item)) {
49 if (item.selectionSet.length > 0) {
50 var model = buildModelFromField(item, result);
51 item.type = model.modelType;
52 result.push(model);
53 flattenSelectionSet(item.selectionSet, result);
54 }
55 }
56 else if (graphql_codegen_core_1.isInlineFragmentNode(item)) {
57 var model = buildModelFromInlineFragment(item, result);
58 item.onType = model.modelType;
59 result.push(model);
60 flattenSelectionSet(item.selectionSet, result);
61 }
62 });
63 return result;
64}
65exports.flattenSelectionSet = flattenSelectionSet;
66function flattenTypes(document) {
67 return {
68 operations: document.operations.map(function (operation) {
69 return __assign({ isFlatten: true }, operation, { innerModels: flattenSelectionSet(operation.selectionSet) });
70 }),
71 fragments: document.fragments.map(function (fragment) {
72 return __assign({ isFlatten: true }, fragment, { innerModels: flattenSelectionSet(fragment.selectionSet) });
73 }),
74 hasOperations: document.hasOperations,
75 hasFragments: document.hasFragments
76 };
77}
78exports.flattenTypes = flattenTypes;
79//# sourceMappingURL=flatten-types.js.map
\No newline at end of file