@graphcommerce/graphql-codegen-near-operation-file 2.102.2 → 2.103.2
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/dist/fragment-resolver.js +136 -0
- package/dist/index.js +118 -0
- package/dist/injectable.js +124 -0
- package/dist/resolve-document-imports.js +134 -0
- package/dist/utils.js +51 -0
- package/package.json +8 -6
- package/src/fragment-resolver.ts +191 -0
- package/src/index.ts +266 -0
- package/src/injectable.graphqls +21 -0
- package/src/injectable.ts +126 -0
- package/src/resolve-document-imports.ts +173 -0
- package/src/test/InjectableFragment.graphql +3 -0
- package/src/test/InjectingFragment.graphql +3 -0
- package/src/test/QueryWithInjectable.graphql +5 -0
- package/src/utils.ts +61 -0
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildFragmentRegistry = void 0;
|
|
4
|
+
var visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common");
|
|
5
|
+
var graphql_1 = require("graphql");
|
|
6
|
+
var utils_1 = require("./utils");
|
|
7
|
+
/**
|
|
8
|
+
* Used by `buildFragmentResolver` to build a mapping of fragmentNames to paths, importNames, and
|
|
9
|
+
* other useful info
|
|
10
|
+
*/
|
|
11
|
+
function buildFragmentRegistry(_a, _b, schemaObject) {
|
|
12
|
+
var generateFilePath = _a.generateFilePath;
|
|
13
|
+
var documents = _b.documents, config = _b.config;
|
|
14
|
+
var baseVisitor = new visitor_plugin_common_1.BaseVisitor(config, {
|
|
15
|
+
scalars: (0, visitor_plugin_common_1.buildScalarsFromConfig)(schemaObject, config),
|
|
16
|
+
dedupeOperationSuffix: (0, visitor_plugin_common_1.getConfigValue)(config.dedupeOperationSuffix, false),
|
|
17
|
+
omitOperationSuffix: (0, visitor_plugin_common_1.getConfigValue)(config.omitOperationSuffix, false),
|
|
18
|
+
fragmentVariablePrefix: (0, visitor_plugin_common_1.getConfigValue)(config.fragmentVariablePrefix, ''),
|
|
19
|
+
fragmentVariableSuffix: (0, visitor_plugin_common_1.getConfigValue)(config.fragmentVariableSuffix, 'FragmentDoc'),
|
|
20
|
+
});
|
|
21
|
+
var getFragmentImports = function (possbileTypes, name) {
|
|
22
|
+
var fragmentImports = [];
|
|
23
|
+
fragmentImports.push({ name: baseVisitor.getFragmentVariableName(name), kind: 'document' });
|
|
24
|
+
var fragmentSuffix = baseVisitor.getFragmentSuffix(name);
|
|
25
|
+
if (possbileTypes.length === 1) {
|
|
26
|
+
fragmentImports.push({
|
|
27
|
+
name: baseVisitor.convertName(name, {
|
|
28
|
+
useTypesPrefix: true,
|
|
29
|
+
suffix: fragmentSuffix,
|
|
30
|
+
}),
|
|
31
|
+
kind: 'type',
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
else if (possbileTypes.length !== 0) {
|
|
35
|
+
possbileTypes.forEach(function (typeName) {
|
|
36
|
+
fragmentImports.push({
|
|
37
|
+
name: baseVisitor.convertName(name, {
|
|
38
|
+
useTypesPrefix: true,
|
|
39
|
+
suffix: "_" + typeName + "_" + fragmentSuffix,
|
|
40
|
+
}),
|
|
41
|
+
kind: 'type',
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
return fragmentImports;
|
|
46
|
+
};
|
|
47
|
+
var duplicateFragmentNames = [];
|
|
48
|
+
var registry = documents.reduce(function (prev, documentRecord) {
|
|
49
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
50
|
+
var fragments = documentRecord.document.definitions.filter(function (d) { return d.kind === graphql_1.Kind.FRAGMENT_DEFINITION; });
|
|
51
|
+
if (fragments.length > 0) {
|
|
52
|
+
for (var _i = 0, fragments_1 = fragments; _i < fragments_1.length; _i++) {
|
|
53
|
+
var fragment = fragments_1[_i];
|
|
54
|
+
var schemaType = schemaObject.getType(fragment.typeCondition.name.value);
|
|
55
|
+
if (!schemaType) {
|
|
56
|
+
throw new Error("Fragment \"" + fragment.name.value + "\" is set on non-existing type \"" + fragment.typeCondition.name.value + "\"!");
|
|
57
|
+
}
|
|
58
|
+
var possibleTypes = (0, visitor_plugin_common_1.getPossibleTypes)(schemaObject, schemaType);
|
|
59
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
60
|
+
var filePath = generateFilePath(documentRecord.location);
|
|
61
|
+
var imports = getFragmentImports(possibleTypes.map(function (t) { return t.name; }), fragment.name.value);
|
|
62
|
+
if (prev[fragment.name.value] &&
|
|
63
|
+
(0, graphql_1.print)(fragment) !== (0, graphql_1.print)(prev[fragment.name.value].node)) {
|
|
64
|
+
duplicateFragmentNames.push(fragment.name.value);
|
|
65
|
+
}
|
|
66
|
+
prev[fragment.name.value] = {
|
|
67
|
+
filePath: filePath,
|
|
68
|
+
imports: imports,
|
|
69
|
+
onType: fragment.typeCondition.name.value,
|
|
70
|
+
node: fragment,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return prev;
|
|
75
|
+
}, {});
|
|
76
|
+
if (duplicateFragmentNames.length) {
|
|
77
|
+
throw new Error("Multiple fragments with the name(s) \"" + duplicateFragmentNames.join(', ') + "\" were found.");
|
|
78
|
+
}
|
|
79
|
+
return registry;
|
|
80
|
+
}
|
|
81
|
+
exports.buildFragmentRegistry = buildFragmentRegistry;
|
|
82
|
+
/** Builds a fragment "resolver" that collects `externalFragments` definitions and `fragmentImportStatements` */
|
|
83
|
+
function buildFragmentResolver(collectorOptions, presetOptions, schemaObject) {
|
|
84
|
+
var fragmentRegistry = buildFragmentRegistry(collectorOptions, presetOptions, schemaObject);
|
|
85
|
+
var baseOutputDir = presetOptions.baseOutputDir;
|
|
86
|
+
var baseDir = collectorOptions.baseDir, typesImport = collectorOptions.typesImport;
|
|
87
|
+
function resolveFragments(generatedFilePath, documentFileContent) {
|
|
88
|
+
var _a;
|
|
89
|
+
var fragmentsInUse = (0, utils_1.extractExternalFragmentsInUse)(documentFileContent, fragmentRegistry);
|
|
90
|
+
var externalFragments = [];
|
|
91
|
+
// fragment files to import names
|
|
92
|
+
var fragmentFileImports = {};
|
|
93
|
+
for (var _i = 0, _b = Object.keys(fragmentsInUse); _i < _b.length; _i++) {
|
|
94
|
+
var fragmentName = _b[_i];
|
|
95
|
+
var level = fragmentsInUse[fragmentName];
|
|
96
|
+
var fragmentDetails = fragmentRegistry[fragmentName];
|
|
97
|
+
if (fragmentDetails) {
|
|
98
|
+
// add top level references to the import object
|
|
99
|
+
// we don't checkf or global namespace because the calling config can do so
|
|
100
|
+
if (level === 0) {
|
|
101
|
+
if (fragmentFileImports[fragmentDetails.filePath] === undefined) {
|
|
102
|
+
fragmentFileImports[fragmentDetails.filePath] = fragmentDetails.imports;
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
(_a = fragmentFileImports[fragmentDetails.filePath]).push.apply(_a, fragmentDetails.imports);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
externalFragments.push({
|
|
109
|
+
level: level,
|
|
110
|
+
isExternal: true,
|
|
111
|
+
name: fragmentName,
|
|
112
|
+
onType: fragmentDetails.onType,
|
|
113
|
+
node: fragmentDetails.node,
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return {
|
|
118
|
+
externalFragments: externalFragments,
|
|
119
|
+
fragmentImports: Object.entries(fragmentFileImports).map(function (_a) {
|
|
120
|
+
var fragmentsFilePath = _a[0], identifiers = _a[1];
|
|
121
|
+
return ({
|
|
122
|
+
baseDir: baseDir,
|
|
123
|
+
baseOutputDir: baseOutputDir,
|
|
124
|
+
outputPath: generatedFilePath,
|
|
125
|
+
importSource: {
|
|
126
|
+
path: fragmentsFilePath,
|
|
127
|
+
identifiers: identifiers,
|
|
128
|
+
},
|
|
129
|
+
typesImport: typesImport,
|
|
130
|
+
});
|
|
131
|
+
}),
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
return resolveFragments;
|
|
135
|
+
}
|
|
136
|
+
exports.default = buildFragmentResolver;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
14
|
+
var t = {};
|
|
15
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
16
|
+
t[p] = s[p];
|
|
17
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
18
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
19
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
20
|
+
t[p[i]] = s[p[i]];
|
|
21
|
+
}
|
|
22
|
+
return t;
|
|
23
|
+
};
|
|
24
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
25
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
26
|
+
if (ar || !(i in from)) {
|
|
27
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
28
|
+
ar[i] = from[i];
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
32
|
+
};
|
|
33
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
34
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
35
|
+
};
|
|
36
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
37
|
+
exports.preset = exports.resolveDocumentImports = void 0;
|
|
38
|
+
var path_1 = require("path");
|
|
39
|
+
var add_1 = __importDefault(require("@graphql-codegen/add"));
|
|
40
|
+
var graphql_1 = require("graphql");
|
|
41
|
+
var injectable_1 = require("./injectable");
|
|
42
|
+
var resolve_document_imports_1 = require("./resolve-document-imports");
|
|
43
|
+
Object.defineProperty(exports, "resolveDocumentImports", { enumerable: true, get: function () { return resolve_document_imports_1.resolveDocumentImports; } });
|
|
44
|
+
var utils_1 = require("./utils");
|
|
45
|
+
function isFragment(documentFile) {
|
|
46
|
+
var name = false;
|
|
47
|
+
(0, graphql_1.visit)(documentFile.document, {
|
|
48
|
+
enter: {
|
|
49
|
+
FragmentDefinition: function () {
|
|
50
|
+
name = true;
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
return name;
|
|
55
|
+
}
|
|
56
|
+
function isDocument(documentFiles) {
|
|
57
|
+
return !documentFiles.every(isFragment);
|
|
58
|
+
}
|
|
59
|
+
exports.preset = {
|
|
60
|
+
buildGeneratesSection: function (options) {
|
|
61
|
+
var _a, _b, _c, _d, _e, _f;
|
|
62
|
+
if (options.presetConfig.injectables) {
|
|
63
|
+
options.documents = (0, injectable_1.injectInjectables)(options.documents);
|
|
64
|
+
}
|
|
65
|
+
var schemaObject = (_a = options.schemaAst) !== null && _a !== void 0 ? _a : (0, graphql_1.buildASTSchema)(options.schema, options.config);
|
|
66
|
+
var baseDir = (_b = options.presetConfig.cwd) !== null && _b !== void 0 ? _b : process.cwd();
|
|
67
|
+
var extension = (_c = options.presetConfig.extension) !== null && _c !== void 0 ? _c : '.generated.ts';
|
|
68
|
+
var folder = (_d = options.presetConfig.folder) !== null && _d !== void 0 ? _d : '';
|
|
69
|
+
var importTypesNamespace = (_e = options.presetConfig.importTypesNamespace) !== null && _e !== void 0 ? _e : 'Types';
|
|
70
|
+
var importAllFragmentsFrom = options.presetConfig.importAllFragmentsFrom;
|
|
71
|
+
var baseTypesPath = options.presetConfig.baseTypesPath;
|
|
72
|
+
if (!baseTypesPath) {
|
|
73
|
+
throw new Error("Preset \"near-operation-file\" requires you to specify \"baseTypesPath\" configuration and point it to your base types file (generated by \"typescript\" plugin)!");
|
|
74
|
+
}
|
|
75
|
+
var shouldAbsolute = !baseTypesPath.startsWith('~');
|
|
76
|
+
var pluginMap = __assign(__assign({}, options.pluginMap), { add: add_1.default });
|
|
77
|
+
var sources = (0, resolve_document_imports_1.resolveDocumentImports)(options, schemaObject, {
|
|
78
|
+
baseDir: baseDir,
|
|
79
|
+
generateFilePath: function (location) {
|
|
80
|
+
var newFilePath = (0, utils_1.defineFilepathSubfolder)(location, folder);
|
|
81
|
+
return (0, utils_1.appendExtensionToFilePath)(newFilePath, extension);
|
|
82
|
+
},
|
|
83
|
+
schemaTypesSource: {
|
|
84
|
+
path: shouldAbsolute ? (0, path_1.join)(options.baseOutputDir, baseTypesPath) : baseTypesPath,
|
|
85
|
+
namespace: importTypesNamespace,
|
|
86
|
+
},
|
|
87
|
+
typesImport: (_f = options.config.useTypeImports) !== null && _f !== void 0 ? _f : false,
|
|
88
|
+
});
|
|
89
|
+
return sources.map(function (_a) {
|
|
90
|
+
var importStatements = _a.importStatements, externalFragments = _a.externalFragments, fragmentImports = _a.fragmentImports, documents = _a.documents, source = __rest(_a, ["importStatements", "externalFragments", "fragmentImports", "documents"]);
|
|
91
|
+
var fragmentImportsArr = fragmentImports;
|
|
92
|
+
if (importAllFragmentsFrom) {
|
|
93
|
+
fragmentImportsArr = fragmentImports.map(function (t) {
|
|
94
|
+
var newImportSource = typeof importAllFragmentsFrom === 'string'
|
|
95
|
+
? __assign(__assign({}, t.importSource), { path: importAllFragmentsFrom }) : importAllFragmentsFrom(t.importSource, source.filename);
|
|
96
|
+
return __assign(__assign({}, t), { importSource: newImportSource || t.importSource });
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
var isDoc = isDocument(documents);
|
|
100
|
+
var isRelayOptimizer = !!Object.keys(pluginMap).find(function (plugin) {
|
|
101
|
+
return plugin.includes('relay-optimizer-plugin');
|
|
102
|
+
});
|
|
103
|
+
var plugins = __spreadArray(__spreadArray([], (options.config.globalNamespace
|
|
104
|
+
? []
|
|
105
|
+
: importStatements.map(function (importStatement) { return ({ add: { content: importStatement } }); })), true), options.plugins.filter(function (pluginOptions) {
|
|
106
|
+
return !isRelayOptimizer ||
|
|
107
|
+
isDoc ||
|
|
108
|
+
!Object.keys(pluginOptions).includes('typed-document-node');
|
|
109
|
+
}), true);
|
|
110
|
+
var config = __assign(__assign({}, options.config), {
|
|
111
|
+
// This is set here in order to make sure the fragment spreads sub types
|
|
112
|
+
// are exported from operations file
|
|
113
|
+
exportFragmentSpreadSubTypes: true, namespacedImportName: importTypesNamespace, externalFragments: externalFragments, fragmentImports: fragmentImportsArr });
|
|
114
|
+
return __assign(__assign({}, source), { documents: documents, plugins: plugins, pluginMap: pluginMap, config: config, schema: options.schema, schemaAst: schemaObject, skipDocumentsValidation: true });
|
|
115
|
+
});
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
exports.default = exports.preset;
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
3
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
4
|
+
if (ar || !(i in from)) {
|
|
5
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
6
|
+
ar[i] = from[i];
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.injectInjectables = void 0;
|
|
13
|
+
var graphql_1 = require("graphql");
|
|
14
|
+
function isFragment(document) {
|
|
15
|
+
var is = false;
|
|
16
|
+
(0, graphql_1.visit)(document, {
|
|
17
|
+
FragmentDefinition: function () {
|
|
18
|
+
is = true;
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
return is;
|
|
22
|
+
}
|
|
23
|
+
function hasInjectableDirective(document) {
|
|
24
|
+
var is = false;
|
|
25
|
+
(0, graphql_1.visit)(document, {
|
|
26
|
+
Directive: function (node) {
|
|
27
|
+
if (!is && node.name.value === 'injectable')
|
|
28
|
+
is = true;
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
return is && isFragment;
|
|
32
|
+
}
|
|
33
|
+
function hasInjectDirective(document) {
|
|
34
|
+
var is = false;
|
|
35
|
+
(0, graphql_1.visit)(document, {
|
|
36
|
+
Directive: function (node) {
|
|
37
|
+
if (!is && node.name.value === 'inject')
|
|
38
|
+
is = true;
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
return is && isFragment;
|
|
42
|
+
}
|
|
43
|
+
function throwInjectError(conf, message) {
|
|
44
|
+
var _a, _b, _c;
|
|
45
|
+
var val = (_a = conf.into) === null || _a === void 0 ? void 0 : _a.map(function (v) { return "\"" + v + "\""; });
|
|
46
|
+
throw Error(message + "\n fragment " + ((_b = conf.fragment) === null || _b === void 0 ? void 0 : _b.name.value) + " on " + ((_c = conf.fragment) === null || _c === void 0 ? void 0 : _c.typeCondition.name.value) + " @inject(into: [" + val + "]) { ... }");
|
|
47
|
+
}
|
|
48
|
+
function assertValidInject(injectVal) {
|
|
49
|
+
var into = injectVal.into, fragment = injectVal.fragment;
|
|
50
|
+
if (!fragment || (into === null || into === void 0 ? void 0 : into.length) === 0)
|
|
51
|
+
throwInjectError(injectVal, 'Invalid inject');
|
|
52
|
+
}
|
|
53
|
+
function getInjectConf(document) {
|
|
54
|
+
if (!hasInjectDirective(document))
|
|
55
|
+
throw Error('');
|
|
56
|
+
var conf = { into: [] };
|
|
57
|
+
(0, graphql_1.visit)(document, {
|
|
58
|
+
Directive: function (node) {
|
|
59
|
+
if (node.name.value !== 'inject')
|
|
60
|
+
return false;
|
|
61
|
+
(0, graphql_1.visit)(node, {
|
|
62
|
+
Argument: function (arg) {
|
|
63
|
+
if (arg.name.value !== 'into')
|
|
64
|
+
return false;
|
|
65
|
+
(0, graphql_1.visit)(arg, {
|
|
66
|
+
ListValue: function (list) {
|
|
67
|
+
list.values.forEach(function (value) {
|
|
68
|
+
(0, graphql_1.visit)(value, {
|
|
69
|
+
StringValue: function (string) {
|
|
70
|
+
var _a;
|
|
71
|
+
(_a = conf.into) === null || _a === void 0 ? void 0 : _a.push(string.value);
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
return undefined;
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
return null;
|
|
81
|
+
},
|
|
82
|
+
FragmentDefinition: function (node) {
|
|
83
|
+
conf.fragment = node;
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
assertValidInject(conf);
|
|
87
|
+
return conf;
|
|
88
|
+
}
|
|
89
|
+
function injectInjectable(injectables, injector) {
|
|
90
|
+
var injectVal = getInjectConf(injector);
|
|
91
|
+
var into = injectVal.into, fragment = injectVal.fragment;
|
|
92
|
+
into.forEach(function (target) {
|
|
93
|
+
var found = false;
|
|
94
|
+
injectables.forEach(function (injectable) {
|
|
95
|
+
(0, graphql_1.visit)(injectable, {
|
|
96
|
+
FragmentDefinition: function (frag) {
|
|
97
|
+
if (frag.name.value === target) {
|
|
98
|
+
found = true;
|
|
99
|
+
var spread = {
|
|
100
|
+
kind: 'FragmentSpread',
|
|
101
|
+
name: { kind: 'Name', value: fragment.name.value },
|
|
102
|
+
};
|
|
103
|
+
frag.selectionSet.selections = __spreadArray(__spreadArray([], frag.selectionSet.selections, true), [spread], false);
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
if (!found)
|
|
109
|
+
throwInjectError(injectVal, "fragment " + target + " @injectable { ... } can not be found or isn't injectable");
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
function injectInjectables(documentFiles) {
|
|
113
|
+
var documents = documentFiles
|
|
114
|
+
.map(function (_a) {
|
|
115
|
+
var document = _a.document;
|
|
116
|
+
return document;
|
|
117
|
+
})
|
|
118
|
+
.filter(function (doc) { return doc; });
|
|
119
|
+
var injectables = documents.filter(function (d) { return isFragment(d) && hasInjectableDirective(d); });
|
|
120
|
+
var injectors = documents.filter(function (d) { return isFragment(d) && hasInjectDirective(d); });
|
|
121
|
+
injectors.forEach(function (d) { return injectInjectable(injectables, d); });
|
|
122
|
+
return documentFiles;
|
|
123
|
+
}
|
|
124
|
+
exports.injectInjectables = injectInjectables;
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
14
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
+
if (mod && mod.__esModule) return mod;
|
|
16
|
+
var result = {};
|
|
17
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
+
__setModuleDefault(result, mod);
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
21
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
22
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
23
|
+
if (ar || !(i in from)) {
|
|
24
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
25
|
+
ar[i] = from[i];
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
29
|
+
};
|
|
30
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
31
|
+
exports.resolveDocumentImports = void 0;
|
|
32
|
+
/* eslint-disable import/no-cycle */
|
|
33
|
+
var path_1 = require("path");
|
|
34
|
+
var plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
|
|
35
|
+
var visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common");
|
|
36
|
+
var graphql_1 = require("graphql");
|
|
37
|
+
var fragment_resolver_1 = __importStar(require("./fragment-resolver"));
|
|
38
|
+
var utils_1 = require("./utils");
|
|
39
|
+
function getFragmentName(documentFile) {
|
|
40
|
+
var name;
|
|
41
|
+
(0, graphql_1.visit)(documentFile.document, {
|
|
42
|
+
enter: {
|
|
43
|
+
FragmentDefinition: function (node) {
|
|
44
|
+
name = node.name.value;
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
return name;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Transform the preset's provided documents into single-file generator sources, while resolving
|
|
52
|
+
* fragment and user-defined imports
|
|
53
|
+
*
|
|
54
|
+
* Resolves user provided imports and fragment imports using the `DocumentImportResolverOptions`.
|
|
55
|
+
* Does not define specific plugins, but rather returns a string[] of `importStatements` for the
|
|
56
|
+
* calling plugin to make use of
|
|
57
|
+
*/
|
|
58
|
+
function resolveDocumentImports(presetOptions, schemaObject, importResolverOptions) {
|
|
59
|
+
var baseOutputDir = presetOptions.baseOutputDir, documents = presetOptions.documents, pluginMap = presetOptions.pluginMap;
|
|
60
|
+
var generateFilePath = importResolverOptions.generateFilePath, schemaTypesSource = importResolverOptions.schemaTypesSource, baseDir = importResolverOptions.baseDir, typesImport = importResolverOptions.typesImport;
|
|
61
|
+
var resolveFragments = (0, fragment_resolver_1.default)(importResolverOptions, presetOptions, schemaObject);
|
|
62
|
+
var fragmentRegistry = (0, fragment_resolver_1.buildFragmentRegistry)(importResolverOptions, presetOptions, schemaObject);
|
|
63
|
+
var isRelayOptimizer = !!Object.keys(pluginMap).find(function (plugin) {
|
|
64
|
+
return plugin.includes('relay-optimizer-plugin');
|
|
65
|
+
});
|
|
66
|
+
var resDocuments = documents.map(function (documentFile) {
|
|
67
|
+
try {
|
|
68
|
+
var isFragment = typeof getFragmentName(documentFile) !== 'undefined';
|
|
69
|
+
if (!isFragment && isRelayOptimizer) {
|
|
70
|
+
var generatedFilePath_1 = generateFilePath(documentFile.location);
|
|
71
|
+
var externalFragments_1 = (0, utils_1.extractExternalFragmentsInUse)(documentFile.document, fragmentRegistry);
|
|
72
|
+
// Sort the entries in the right order so fragments are defined when using
|
|
73
|
+
externalFragments_1 = Object.fromEntries(Object.entries(externalFragments_1).sort(function (_a, _b) {
|
|
74
|
+
var levelA = _a[1];
|
|
75
|
+
var levelB = _b[1];
|
|
76
|
+
return levelB - levelA;
|
|
77
|
+
}));
|
|
78
|
+
var fragments = documents.filter(function (d) { var _a; return typeof externalFragments_1[(_a = getFragmentName(d)) !== null && _a !== void 0 ? _a : ''] !== 'undefined'; });
|
|
79
|
+
var importStatements_1 = [];
|
|
80
|
+
if ((0, plugin_helpers_1.isUsingTypes)(documentFile.document, [], schemaObject)) {
|
|
81
|
+
var schemaTypesImportStatement = (0, visitor_plugin_common_1.generateImportStatement)({
|
|
82
|
+
baseDir: baseDir,
|
|
83
|
+
importSource: (0, visitor_plugin_common_1.resolveImportSource)(schemaTypesSource),
|
|
84
|
+
baseOutputDir: baseOutputDir,
|
|
85
|
+
outputPath: generatedFilePath_1,
|
|
86
|
+
typesImport: typesImport,
|
|
87
|
+
});
|
|
88
|
+
importStatements_1.unshift(schemaTypesImportStatement);
|
|
89
|
+
}
|
|
90
|
+
// const newDocument = [...fragments.map((f) => f.rawSDL), documentFile.rawSDL].join('\n')
|
|
91
|
+
return {
|
|
92
|
+
filename: generatedFilePath_1,
|
|
93
|
+
documents: __spreadArray(__spreadArray([], fragments, true), [documentFile], false),
|
|
94
|
+
importStatements: importStatements_1,
|
|
95
|
+
fragmentImports: [],
|
|
96
|
+
externalFragments: [],
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
var generatedFilePath = generateFilePath(documentFile.location);
|
|
100
|
+
var importStatements = [];
|
|
101
|
+
var _a = resolveFragments(generatedFilePath, documentFile.document), externalFragments = _a.externalFragments, fragmentImports = _a.fragmentImports;
|
|
102
|
+
if (isRelayOptimizer ||
|
|
103
|
+
(0, plugin_helpers_1.isUsingTypes)(documentFile.document, externalFragments.map(function (m) { return m.name; }), schemaObject)) {
|
|
104
|
+
var schemaTypesImportStatement = (0, visitor_plugin_common_1.generateImportStatement)({
|
|
105
|
+
baseDir: baseDir,
|
|
106
|
+
importSource: (0, visitor_plugin_common_1.resolveImportSource)(schemaTypesSource),
|
|
107
|
+
baseOutputDir: baseOutputDir,
|
|
108
|
+
outputPath: generatedFilePath,
|
|
109
|
+
typesImport: typesImport,
|
|
110
|
+
});
|
|
111
|
+
importStatements.unshift(schemaTypesImportStatement);
|
|
112
|
+
}
|
|
113
|
+
return {
|
|
114
|
+
filename: generatedFilePath,
|
|
115
|
+
documents: [documentFile],
|
|
116
|
+
importStatements: importStatements,
|
|
117
|
+
fragmentImports: fragmentImports,
|
|
118
|
+
externalFragments: externalFragments,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
catch (e) {
|
|
122
|
+
if (e instanceof Error) {
|
|
123
|
+
throw new plugin_helpers_1.DetailedError("Unable to validate GraphQL document!", "File " + documentFile.location + " caused error: " + (e.message || e.toString()), documentFile.location);
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
throw e;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
return resDocuments.filter(function (result) {
|
|
131
|
+
return result.filename.startsWith((0, path_1.resolve)(baseDir, baseOutputDir));
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
exports.resolveDocumentImports = resolveDocumentImports;
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.extractExternalFragmentsInUse = exports.appendExtensionToFilePath = exports.defineFilepathSubfolder = void 0;
|
|
7
|
+
/* eslint-disable import/no-cycle */
|
|
8
|
+
var path_1 = require("path");
|
|
9
|
+
var graphql_1 = require("graphql");
|
|
10
|
+
var parse_filepath_1 = __importDefault(require("parse-filepath"));
|
|
11
|
+
function defineFilepathSubfolder(baseFilePath, folder) {
|
|
12
|
+
var parsedPath = (0, parse_filepath_1.default)(baseFilePath);
|
|
13
|
+
return (0, path_1.join)(parsedPath.dir, folder, parsedPath.base).replace(/\\/g, '/');
|
|
14
|
+
}
|
|
15
|
+
exports.defineFilepathSubfolder = defineFilepathSubfolder;
|
|
16
|
+
function appendExtensionToFilePath(baseFilePath, extension) {
|
|
17
|
+
var parsedPath = (0, parse_filepath_1.default)(baseFilePath);
|
|
18
|
+
return (0, path_1.join)(parsedPath.dir, parsedPath.name + extension).replace(/\\/g, '/');
|
|
19
|
+
}
|
|
20
|
+
exports.appendExtensionToFilePath = appendExtensionToFilePath;
|
|
21
|
+
function extractExternalFragmentsInUse(documentNode, fragmentNameToFile, result, level) {
|
|
22
|
+
if (result === void 0) { result = {}; }
|
|
23
|
+
if (level === void 0) { level = 0; }
|
|
24
|
+
var ignoreList = new Set();
|
|
25
|
+
// First, take all fragments definition from the current file, and mark them as ignored
|
|
26
|
+
(0, graphql_1.visit)(documentNode, {
|
|
27
|
+
enter: {
|
|
28
|
+
FragmentDefinition: function (node) {
|
|
29
|
+
ignoreList.add(node.name.value);
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
// Then, look for all used fragments in this document
|
|
34
|
+
(0, graphql_1.visit)(documentNode, {
|
|
35
|
+
enter: {
|
|
36
|
+
FragmentSpread: function (node) {
|
|
37
|
+
if (!ignoreList.has(node.name.value)) {
|
|
38
|
+
if (result[node.name.value] === undefined ||
|
|
39
|
+
(result[node.name.value] !== undefined && level < result[node.name.value])) {
|
|
40
|
+
result[node.name.value] = level;
|
|
41
|
+
if (fragmentNameToFile[node.name.value]) {
|
|
42
|
+
extractExternalFragmentsInUse(fragmentNameToFile[node.name.value].node, fragmentNameToFile, result, level + 1);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
return result;
|
|
50
|
+
}
|
|
51
|
+
exports.extractExternalFragmentsInUse = extractExternalFragmentsInUse;
|
package/package.json
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphcommerce/graphql-codegen-near-operation-file",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.103.2",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"engines": {
|
|
7
7
|
"node": "14.x"
|
|
8
8
|
},
|
|
9
9
|
"files": [
|
|
10
|
-
"dist"
|
|
10
|
+
"dist",
|
|
11
|
+
"src"
|
|
11
12
|
],
|
|
12
13
|
"scripts": {
|
|
13
14
|
"dev": "tsc --preserveWatchOutput --watch --sourceMap --outDir dist",
|
|
14
|
-
"build": "tsc --target es5 --outDir dist"
|
|
15
|
+
"build": "tsc --target es5 --outDir dist",
|
|
16
|
+
"prepack": "yarn build"
|
|
15
17
|
},
|
|
16
18
|
"prettier": "@graphcommerce/prettier-config-pwa",
|
|
17
19
|
"browserslist": [
|
|
@@ -25,9 +27,9 @@
|
|
|
25
27
|
},
|
|
26
28
|
"devDependencies": {
|
|
27
29
|
"@graphcommerce/browserslist-config-pwa": "^3.0.1",
|
|
28
|
-
"@graphcommerce/eslint-config-pwa": "^3.0.
|
|
30
|
+
"@graphcommerce/eslint-config-pwa": "^3.0.4",
|
|
29
31
|
"@graphcommerce/prettier-config-pwa": "^3.0.2",
|
|
30
|
-
"@graphcommerce/typescript-config-pwa": "^3.0
|
|
32
|
+
"@graphcommerce/typescript-config-pwa": "^3.1.0",
|
|
31
33
|
"@playwright/test": "^1.15.0"
|
|
32
34
|
},
|
|
33
35
|
"dependencies": {
|
|
@@ -38,5 +40,5 @@
|
|
|
38
40
|
"graphql": "^15.6.0",
|
|
39
41
|
"parse-filepath": "^1.0.2"
|
|
40
42
|
},
|
|
41
|
-
"gitHead": "
|
|
43
|
+
"gitHead": "cf2b61fa96aca043ff841b018334ca8c28e5e6cf"
|
|
42
44
|
}
|