@graphql-tools/stitching-directives 3.1.13-alpha-20241113142246-527bbf8c4e20af3d172439ab4746fe676b3a61c5 → 3.1.14-alpha-c1f969d9556b274c8e1313a3d2a4742f231bc03a
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +929 -0
- package/dist/index.cjs +1196 -0
- package/dist/index.d.cts +58 -0
- package/dist/index.d.ts +58 -0
- package/dist/index.js +1193 -0
- package/package.json +32 -42
- package/cjs/defaultStitchingDirectiveOptions.js +0 -10
- package/cjs/extractVariables.js +0 -52
- package/cjs/federationToStitchingSDL.js +0 -116
- package/cjs/getSourcePaths.js +0 -25
- package/cjs/index.js +0 -6
- package/cjs/package.json +0 -1
- package/cjs/parseMergeArgsExpr.js +0 -77
- package/cjs/pathsFromSelectionSet.js +0 -29
- package/cjs/preparseMergeArgsExpr.js +0 -31
- package/cjs/properties.js +0 -70
- package/cjs/stitchingDirectives.js +0 -78
- package/cjs/stitchingDirectivesTransformer.js +0 -490
- package/cjs/stitchingDirectivesValidator.js +0 -119
- package/cjs/types.js +0 -0
- package/esm/defaultStitchingDirectiveOptions.js +0 -7
- package/esm/extractVariables.js +0 -48
- package/esm/federationToStitchingSDL.js +0 -112
- package/esm/getSourcePaths.js +0 -21
- package/esm/index.js +0 -3
- package/esm/parseMergeArgsExpr.js +0 -73
- package/esm/pathsFromSelectionSet.js +0 -25
- package/esm/preparseMergeArgsExpr.js +0 -27
- package/esm/properties.js +0 -63
- package/esm/stitchingDirectives.js +0 -74
- package/esm/stitchingDirectivesTransformer.js +0 -486
- package/esm/stitchingDirectivesValidator.js +0 -115
- package/esm/types.js +0 -0
- package/typings/defaultStitchingDirectiveOptions.d.cts +0 -2
- package/typings/defaultStitchingDirectiveOptions.d.ts +0 -2
- package/typings/extractVariables.d.cts +0 -7
- package/typings/extractVariables.d.ts +0 -7
- package/typings/federationToStitchingSDL.d.cts +0 -2
- package/typings/federationToStitchingSDL.d.ts +0 -2
- package/typings/getSourcePaths.d.cts +0 -3
- package/typings/getSourcePaths.d.ts +0 -3
- package/typings/index.d.cts +0 -3
- package/typings/index.d.ts +0 -3
- package/typings/parseMergeArgsExpr.d.cts +0 -3
- package/typings/parseMergeArgsExpr.d.ts +0 -3
- package/typings/pathsFromSelectionSet.d.cts +0 -2
- package/typings/pathsFromSelectionSet.d.ts +0 -2
- package/typings/preparseMergeArgsExpr.d.cts +0 -6
- package/typings/preparseMergeArgsExpr.d.ts +0 -6
- package/typings/properties.d.cts +0 -5
- package/typings/properties.d.ts +0 -5
- package/typings/stitchingDirectives.d.cts +0 -19
- package/typings/stitchingDirectives.d.ts +0 -19
- package/typings/stitchingDirectivesTransformer.d.cts +0 -3
- package/typings/stitchingDirectivesTransformer.d.ts +0 -3
- package/typings/stitchingDirectivesValidator.d.cts +0 -3
- package/typings/stitchingDirectivesValidator.d.ts +0 -3
- package/typings/types.d.cts +0 -35
- package/typings/types.d.ts +0 -35
package/package.json
CHANGED
@@ -1,61 +1,51 @@
|
|
1
1
|
{
|
2
2
|
"name": "@graphql-tools/stitching-directives",
|
3
|
-
"version": "3.1.
|
3
|
+
"version": "3.1.14-alpha-c1f969d9556b274c8e1313a3d2a4742f231bc03a",
|
4
|
+
"type": "module",
|
4
5
|
"description": "A set of utils for faster development of GraphQL tools",
|
5
|
-
"sideEffects": false,
|
6
|
-
"peerDependencies": {
|
7
|
-
"graphql": "^14.0.0 || ^15.0.0 || ^16.0.0 || ^17.0.0"
|
8
|
-
},
|
9
|
-
"dependencies": {
|
10
|
-
"@graphql-tools/delegate": "10.1.2-alpha-20241113142246-527bbf8c4e20af3d172439ab4746fe676b3a61c5",
|
11
|
-
"@graphql-tools/utils": "10.5.6-alpha-20241113142246-527bbf8c4e20af3d172439ab4746fe676b3a61c5",
|
12
|
-
"tslib": "^2.4.0"
|
13
|
-
},
|
14
6
|
"repository": {
|
15
7
|
"type": "git",
|
16
|
-
"url": "
|
8
|
+
"url": "graphql-hive/gateway",
|
17
9
|
"directory": "packages/stitching-directives"
|
18
10
|
},
|
19
11
|
"license": "MIT",
|
20
12
|
"engines": {
|
21
|
-
"node": ">=
|
13
|
+
"node": ">=18.0.0"
|
22
14
|
},
|
23
|
-
"main": "
|
24
|
-
"module": "esm/index.js",
|
25
|
-
"typings": "typings/index.d.ts",
|
26
|
-
"typescript": {
|
27
|
-
"definition": "typings/index.d.ts"
|
28
|
-
},
|
29
|
-
"type": "module",
|
15
|
+
"main": "./dist/index.js",
|
30
16
|
"exports": {
|
31
17
|
".": {
|
32
18
|
"require": {
|
33
|
-
"types": "./
|
34
|
-
"default": "./
|
19
|
+
"types": "./dist/index.d.cts",
|
20
|
+
"default": "./dist/index.cjs"
|
35
21
|
},
|
36
22
|
"import": {
|
37
|
-
"types": "./
|
38
|
-
"default": "./
|
39
|
-
},
|
40
|
-
"default": {
|
41
|
-
"types": "./typings/index.d.ts",
|
42
|
-
"default": "./esm/index.js"
|
43
|
-
}
|
44
|
-
},
|
45
|
-
"./*": {
|
46
|
-
"require": {
|
47
|
-
"types": "./typings/*.d.cts",
|
48
|
-
"default": "./cjs/*.js"
|
49
|
-
},
|
50
|
-
"import": {
|
51
|
-
"types": "./typings/*.d.ts",
|
52
|
-
"default": "./esm/*.js"
|
53
|
-
},
|
54
|
-
"default": {
|
55
|
-
"types": "./typings/*.d.ts",
|
56
|
-
"default": "./esm/*.js"
|
23
|
+
"types": "./dist/index.d.ts",
|
24
|
+
"default": "./dist/index.js"
|
57
25
|
}
|
58
26
|
},
|
59
27
|
"./package.json": "./package.json"
|
60
|
-
}
|
28
|
+
},
|
29
|
+
"types": "./dist/index.d.ts",
|
30
|
+
"files": [
|
31
|
+
"dist"
|
32
|
+
],
|
33
|
+
"scripts": {
|
34
|
+
"build": "pkgroll --clean-dist",
|
35
|
+
"prepack": "yarn build"
|
36
|
+
},
|
37
|
+
"peerDependencies": {
|
38
|
+
"graphql": "^14.0.0 || ^15.0.0 || ^16.0.0 || ^17.0.0"
|
39
|
+
},
|
40
|
+
"dependencies": {
|
41
|
+
"@graphql-tools/delegate": "^10.1.3-alpha-c1f969d9556b274c8e1313a3d2a4742f231bc03a",
|
42
|
+
"@graphql-tools/utils": "^10.5.6",
|
43
|
+
"tslib": "^2.4.0"
|
44
|
+
},
|
45
|
+
"devDependencies": {
|
46
|
+
"@graphql-tools/schema": "^10.0.8",
|
47
|
+
"graphql": "^16.9.0",
|
48
|
+
"pkgroll": "2.5.1"
|
49
|
+
},
|
50
|
+
"sideEffects": false
|
61
51
|
}
|
@@ -1,10 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.defaultStitchingDirectiveOptions = void 0;
|
4
|
-
exports.defaultStitchingDirectiveOptions = {
|
5
|
-
keyDirectiveName: 'key',
|
6
|
-
computedDirectiveName: 'computed',
|
7
|
-
canonicalDirectiveName: 'canonical',
|
8
|
-
mergeDirectiveName: 'merge',
|
9
|
-
pathToDirectivesInExtensions: ['directives'],
|
10
|
-
};
|
package/cjs/extractVariables.js
DELETED
@@ -1,52 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.extractVariables = void 0;
|
4
|
-
const graphql_1 = require("graphql");
|
5
|
-
function extractVariables(inputValue) {
|
6
|
-
const path = [];
|
7
|
-
const variablePaths = Object.create(null);
|
8
|
-
const keyPathVisitor = {
|
9
|
-
enter: (_node, key) => {
|
10
|
-
if (typeof key === 'number') {
|
11
|
-
path.push(key);
|
12
|
-
}
|
13
|
-
},
|
14
|
-
leave: (_node, key) => {
|
15
|
-
if (typeof key === 'number') {
|
16
|
-
path.pop();
|
17
|
-
}
|
18
|
-
},
|
19
|
-
};
|
20
|
-
const fieldPathVisitor = {
|
21
|
-
enter: (node) => {
|
22
|
-
path.push(node.name.value);
|
23
|
-
},
|
24
|
-
leave: () => {
|
25
|
-
path.pop();
|
26
|
-
},
|
27
|
-
};
|
28
|
-
const variableVisitor = {
|
29
|
-
enter: (node, key) => {
|
30
|
-
if (typeof key === 'number') {
|
31
|
-
variablePaths[node.name.value] = path.concat([key]);
|
32
|
-
}
|
33
|
-
else {
|
34
|
-
variablePaths[node.name.value] = path.slice();
|
35
|
-
}
|
36
|
-
return {
|
37
|
-
kind: graphql_1.Kind.NULL,
|
38
|
-
};
|
39
|
-
},
|
40
|
-
};
|
41
|
-
const newInputValue = (0, graphql_1.visit)(inputValue, {
|
42
|
-
[graphql_1.Kind.OBJECT]: keyPathVisitor,
|
43
|
-
[graphql_1.Kind.LIST]: keyPathVisitor,
|
44
|
-
[graphql_1.Kind.OBJECT_FIELD]: fieldPathVisitor,
|
45
|
-
[graphql_1.Kind.VARIABLE]: variableVisitor,
|
46
|
-
});
|
47
|
-
return {
|
48
|
-
inputValue: newInputValue,
|
49
|
-
variablePaths,
|
50
|
-
};
|
51
|
-
}
|
52
|
-
exports.extractVariables = extractVariables;
|
@@ -1,116 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
// Taken from https://github.com/gmac/federation-to-stitching-sdl/blob/main/index.js
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
4
|
-
exports.federationToStitchingSDL = void 0;
|
5
|
-
const graphql_1 = require("graphql");
|
6
|
-
const stitchingDirectives_js_1 = require("./stitchingDirectives.js");
|
7
|
-
const extensionKind = /Extension$/;
|
8
|
-
const entityKinds = [
|
9
|
-
graphql_1.Kind.OBJECT_TYPE_DEFINITION,
|
10
|
-
graphql_1.Kind.OBJECT_TYPE_EXTENSION,
|
11
|
-
graphql_1.Kind.INTERFACE_TYPE_DEFINITION,
|
12
|
-
graphql_1.Kind.INTERFACE_TYPE_EXTENSION,
|
13
|
-
];
|
14
|
-
function isEntityKind(def) {
|
15
|
-
return entityKinds.includes(def.kind);
|
16
|
-
}
|
17
|
-
function getQueryTypeDef(definitions) {
|
18
|
-
const schemaDef = definitions.find(def => def.kind === graphql_1.Kind.SCHEMA_DEFINITION);
|
19
|
-
const typeName = schemaDef
|
20
|
-
? schemaDef.operationTypes.find(({ operation }) => operation === 'query')?.type.name.value
|
21
|
-
: 'Query';
|
22
|
-
return definitions.find(def => def.kind === graphql_1.Kind.OBJECT_TYPE_DEFINITION && def.name.value === typeName);
|
23
|
-
}
|
24
|
-
// Federation services are actually fairly complex,
|
25
|
-
// as the `buildFederatedSchema` helper does a fair amount
|
26
|
-
// of hidden work to setup the Federation schema specification:
|
27
|
-
// https://www.apollographql.com/docs/federation/federation-spec/#federation-schema-specification
|
28
|
-
function federationToStitchingSDL(federationSDL, stitchingConfig = (0, stitchingDirectives_js_1.stitchingDirectives)()) {
|
29
|
-
const doc = (0, graphql_1.parse)(federationSDL);
|
30
|
-
const entityTypes = [];
|
31
|
-
const baseTypeNames = doc.definitions.reduce((memo, typeDef) => {
|
32
|
-
if (!extensionKind.test(typeDef.kind) && 'name' in typeDef && typeDef.name) {
|
33
|
-
memo[typeDef.name.value] = true;
|
34
|
-
}
|
35
|
-
return memo;
|
36
|
-
}, {});
|
37
|
-
doc.definitions.forEach(typeDef => {
|
38
|
-
// Un-extend all types (remove "extends" keywords)...
|
39
|
-
// extended types are invalid GraphQL without a local base type to extend from.
|
40
|
-
// Stitching merges flat types in lieu of hierarchical extensions.
|
41
|
-
if (extensionKind.test(typeDef.kind) &&
|
42
|
-
'name' in typeDef &&
|
43
|
-
typeDef.name &&
|
44
|
-
!baseTypeNames[typeDef.name.value]) {
|
45
|
-
typeDef.kind = typeDef.kind.replace(extensionKind, 'Definition');
|
46
|
-
}
|
47
|
-
if (!isEntityKind(typeDef))
|
48
|
-
return;
|
49
|
-
// Find object definitions with "@key" directives;
|
50
|
-
// these are federated entities that get turned into merged types.
|
51
|
-
const keyDirs = [];
|
52
|
-
const otherDirs = [];
|
53
|
-
typeDef.directives?.forEach(dir => {
|
54
|
-
if (dir.name.value === 'key') {
|
55
|
-
keyDirs.push(dir);
|
56
|
-
}
|
57
|
-
else {
|
58
|
-
otherDirs.push(dir);
|
59
|
-
}
|
60
|
-
});
|
61
|
-
if (!keyDirs.length)
|
62
|
-
return;
|
63
|
-
// Setup stitching MergedTypeConfig for all federated entities:
|
64
|
-
const selectionSet = `{ ${keyDirs.map((dir) => dir.arguments[0].value.value).join(' ')} }`;
|
65
|
-
const keyFields = (0, graphql_1.parse)(selectionSet).definitions[0].selectionSet.selections.map((sel) => sel.name.value);
|
66
|
-
const keyDir = keyDirs[0];
|
67
|
-
keyDir.name.value = stitchingConfig.keyDirective.name;
|
68
|
-
keyDir.arguments[0].name.value = 'selectionSet';
|
69
|
-
keyDir.arguments[0].value.value = selectionSet;
|
70
|
-
typeDef.directives = [keyDir, ...otherDirs];
|
71
|
-
// Remove non-key "@external" fields from the type...
|
72
|
-
// the stitching query planner expects services to only publish their own fields.
|
73
|
-
// This makes "@provides" moot because the query planner can automate the logic.
|
74
|
-
typeDef.fields = typeDef.fields?.filter(fieldDef => {
|
75
|
-
return (keyFields.includes(fieldDef.name.value) ||
|
76
|
-
!fieldDef.directives?.find(dir => dir.name.value === 'external'));
|
77
|
-
});
|
78
|
-
// Discard remaining "@external" directives and any "@provides" directives
|
79
|
-
typeDef.fields?.forEach((fieldDef) => {
|
80
|
-
fieldDef.directives = fieldDef.directives.filter((dir) => !/^(external|provides)$/.test(dir.name.value));
|
81
|
-
fieldDef.directives.forEach((dir) => {
|
82
|
-
if (dir.name.value === 'requires') {
|
83
|
-
dir.name.value = stitchingConfig.computedDirective.name;
|
84
|
-
dir.arguments[0].name.value = 'selectionSet';
|
85
|
-
dir.arguments[0].value.value = `{ ${dir.arguments[0].value.value} }`;
|
86
|
-
}
|
87
|
-
});
|
88
|
-
});
|
89
|
-
if (typeDef.kind === graphql_1.Kind.OBJECT_TYPE_DEFINITION ||
|
90
|
-
typeDef.kind === graphql_1.Kind.OBJECT_TYPE_EXTENSION) {
|
91
|
-
entityTypes.push(typeDef.name.value);
|
92
|
-
}
|
93
|
-
});
|
94
|
-
// Federation service SDLs are incomplete because they omit the federation spec itself...
|
95
|
-
// (https://www.apollographql.com/docs/federation/federation-spec/#federation-schema-specification)
|
96
|
-
// To make federation SDLs into valid and parsable GraphQL schemas,
|
97
|
-
// we must fill in the missing details from the specification.
|
98
|
-
if (entityTypes.length) {
|
99
|
-
const queryDef = getQueryTypeDef(doc.definitions);
|
100
|
-
const entitiesSchema = (0, graphql_1.parse)(/* GraphQL */ `
|
101
|
-
scalar _Any
|
102
|
-
union _Entity = ${entityTypes.filter((v, i, a) => a.indexOf(v) === i).join(' | ')}
|
103
|
-
type Query { _entities(representations: [_Any!]!): [_Entity]! @${stitchingConfig.mergeDirective.name} }
|
104
|
-
`).definitions;
|
105
|
-
doc.definitions.push(entitiesSchema[0]);
|
106
|
-
doc.definitions.push(entitiesSchema[1]);
|
107
|
-
if (queryDef) {
|
108
|
-
queryDef.fields.push(entitiesSchema[2].fields[0]);
|
109
|
-
}
|
110
|
-
else {
|
111
|
-
doc.definitions.push(entitiesSchema[2]);
|
112
|
-
}
|
113
|
-
}
|
114
|
-
return [stitchingConfig.stitchingDirectivesTypeDefs, (0, graphql_1.print)(doc)].join('\n');
|
115
|
-
}
|
116
|
-
exports.federationToStitchingSDL = federationToStitchingSDL;
|
package/cjs/getSourcePaths.js
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.getSourcePaths = void 0;
|
4
|
-
const graphql_1 = require("graphql");
|
5
|
-
const pathsFromSelectionSet_js_1 = require("./pathsFromSelectionSet.js");
|
6
|
-
function getSourcePaths(mappingInstructions, selectionSet) {
|
7
|
-
const sourcePaths = [];
|
8
|
-
for (const mappingInstruction of mappingInstructions) {
|
9
|
-
const { sourcePath } = mappingInstruction;
|
10
|
-
if (sourcePath.length) {
|
11
|
-
sourcePaths.push(sourcePath);
|
12
|
-
continue;
|
13
|
-
}
|
14
|
-
if (selectionSet == null) {
|
15
|
-
continue;
|
16
|
-
}
|
17
|
-
const paths = (0, pathsFromSelectionSet_js_1.pathsFromSelectionSet)(selectionSet);
|
18
|
-
for (const path of paths) {
|
19
|
-
sourcePaths.push(path);
|
20
|
-
}
|
21
|
-
sourcePaths.push([graphql_1.TypeNameMetaFieldDef.name]);
|
22
|
-
}
|
23
|
-
return sourcePaths;
|
24
|
-
}
|
25
|
-
exports.getSourcePaths = getSourcePaths;
|
package/cjs/index.js
DELETED
@@ -1,6 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
const tslib_1 = require("tslib");
|
4
|
-
tslib_1.__exportStar(require("./stitchingDirectives.js"), exports);
|
5
|
-
tslib_1.__exportStar(require("./types.js"), exports);
|
6
|
-
tslib_1.__exportStar(require("./federationToStitchingSDL.js"), exports);
|
package/cjs/package.json
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
{"type":"commonjs"}
|
@@ -1,77 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.parseMergeArgsExpr = void 0;
|
4
|
-
const graphql_1 = require("graphql");
|
5
|
-
const extractVariables_js_1 = require("./extractVariables.js");
|
6
|
-
const getSourcePaths_js_1 = require("./getSourcePaths.js");
|
7
|
-
const preparseMergeArgsExpr_js_1 = require("./preparseMergeArgsExpr.js");
|
8
|
-
const properties_js_1 = require("./properties.js");
|
9
|
-
function parseMergeArgsExpr(mergeArgsExpr, selectionSet) {
|
10
|
-
const { mergeArgsExpr: newMergeArgsExpr, expansionExpressions } = (0, preparseMergeArgsExpr_js_1.preparseMergeArgsExpr)(mergeArgsExpr);
|
11
|
-
const inputValue = (0, graphql_1.parseValue)(`{ ${newMergeArgsExpr} }`, { noLocation: true });
|
12
|
-
const { inputValue: newInputValue, variablePaths } = (0, extractVariables_js_1.extractVariables)(inputValue);
|
13
|
-
if (!Object.keys(expansionExpressions).length) {
|
14
|
-
if (!Object.keys(variablePaths).length) {
|
15
|
-
throw new Error('Merge arguments must declare a key.');
|
16
|
-
}
|
17
|
-
const mappingInstructions = getMappingInstructions(variablePaths);
|
18
|
-
const usedProperties = (0, properties_js_1.propertyTreeFromPaths)((0, getSourcePaths_js_1.getSourcePaths)(mappingInstructions, selectionSet));
|
19
|
-
return {
|
20
|
-
args: (0, graphql_1.valueFromASTUntyped)(newInputValue),
|
21
|
-
usedProperties,
|
22
|
-
mappingInstructions,
|
23
|
-
};
|
24
|
-
}
|
25
|
-
const expansionRegEx = new RegExp(`^${preparseMergeArgsExpr_js_1.EXPANSION_PREFIX}[0-9]+$`);
|
26
|
-
for (const variableName in variablePaths) {
|
27
|
-
if (!variableName.match(expansionRegEx)) {
|
28
|
-
throw new Error('Expansions cannot be mixed with single key declarations.');
|
29
|
-
}
|
30
|
-
}
|
31
|
-
const expansions = [];
|
32
|
-
const sourcePaths = [];
|
33
|
-
for (const variableName in expansionExpressions) {
|
34
|
-
const str = expansionExpressions[variableName];
|
35
|
-
const valuePath = variablePaths[variableName];
|
36
|
-
const { inputValue: expansionInputValue, variablePaths: expansionVariablePaths } = (0, extractVariables_js_1.extractVariables)((0, graphql_1.parseValue)(`${str}`, { noLocation: true }));
|
37
|
-
if (!Object.keys(expansionVariablePaths).length) {
|
38
|
-
throw new Error('Merge arguments must declare a key.');
|
39
|
-
}
|
40
|
-
const mappingInstructions = getMappingInstructions(expansionVariablePaths);
|
41
|
-
const value = (0, graphql_1.valueFromASTUntyped)(expansionInputValue);
|
42
|
-
sourcePaths.push(...(0, getSourcePaths_js_1.getSourcePaths)(mappingInstructions, selectionSet));
|
43
|
-
assertNotWithinList(valuePath);
|
44
|
-
expansions.push({
|
45
|
-
valuePath,
|
46
|
-
value,
|
47
|
-
mappingInstructions,
|
48
|
-
});
|
49
|
-
}
|
50
|
-
const usedProperties = (0, properties_js_1.propertyTreeFromPaths)(sourcePaths);
|
51
|
-
return {
|
52
|
-
args: (0, graphql_1.valueFromASTUntyped)(newInputValue),
|
53
|
-
usedProperties,
|
54
|
-
expansions,
|
55
|
-
};
|
56
|
-
}
|
57
|
-
exports.parseMergeArgsExpr = parseMergeArgsExpr;
|
58
|
-
function getMappingInstructions(variablePaths) {
|
59
|
-
const mappingInstructions = [];
|
60
|
-
for (const keyPath in variablePaths) {
|
61
|
-
const valuePath = variablePaths[keyPath];
|
62
|
-
const splitKeyPath = keyPath.split(preparseMergeArgsExpr_js_1.KEY_DELIMITER).slice(1);
|
63
|
-
assertNotWithinList(valuePath);
|
64
|
-
mappingInstructions.push({
|
65
|
-
destinationPath: valuePath,
|
66
|
-
sourcePath: splitKeyPath,
|
67
|
-
});
|
68
|
-
}
|
69
|
-
return mappingInstructions;
|
70
|
-
}
|
71
|
-
function assertNotWithinList(path) {
|
72
|
-
for (const pathSegment of path) {
|
73
|
-
if (typeof pathSegment === 'number') {
|
74
|
-
throw new Error('Insertions cannot be made into a list.');
|
75
|
-
}
|
76
|
-
}
|
77
|
-
}
|
@@ -1,29 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.pathsFromSelectionSet = void 0;
|
4
|
-
const graphql_1 = require("graphql");
|
5
|
-
function pathsFromSelectionSet(selectionSet, path = []) {
|
6
|
-
const paths = [];
|
7
|
-
for (const selection of selectionSet.selections) {
|
8
|
-
const additions = pathsFromSelection(selection, path) ?? [];
|
9
|
-
for (const addition of additions) {
|
10
|
-
paths.push(addition);
|
11
|
-
}
|
12
|
-
}
|
13
|
-
return paths;
|
14
|
-
}
|
15
|
-
exports.pathsFromSelectionSet = pathsFromSelectionSet;
|
16
|
-
function pathsFromSelection(selection, path) {
|
17
|
-
if (selection.kind === graphql_1.Kind.FIELD) {
|
18
|
-
const responseKey = selection.alias?.value ?? selection.name.value;
|
19
|
-
if (selection.selectionSet) {
|
20
|
-
return pathsFromSelectionSet(selection.selectionSet, path.concat([responseKey]));
|
21
|
-
}
|
22
|
-
else {
|
23
|
-
return [path.concat([responseKey])];
|
24
|
-
}
|
25
|
-
}
|
26
|
-
else if (selection.kind === graphql_1.Kind.INLINE_FRAGMENT) {
|
27
|
-
return pathsFromSelectionSet(selection.selectionSet, path);
|
28
|
-
}
|
29
|
-
}
|
@@ -1,31 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.preparseMergeArgsExpr = exports.EXPANSION_PREFIX = exports.KEY_DELIMITER = void 0;
|
4
|
-
exports.KEY_DELIMITER = '__dot__';
|
5
|
-
exports.EXPANSION_PREFIX = '__exp';
|
6
|
-
function preparseMergeArgsExpr(mergeArgsExpr) {
|
7
|
-
const variableRegex = /\$[_A-Za-z][_A-Za-z0-9.]*/g;
|
8
|
-
const dotRegex = /\./g;
|
9
|
-
mergeArgsExpr = mergeArgsExpr.replace(variableRegex, variable => variable.replace(dotRegex, exports.KEY_DELIMITER));
|
10
|
-
const segments = mergeArgsExpr.split('[[');
|
11
|
-
const expansionExpressions = Object.create(null);
|
12
|
-
if (segments.length === 1) {
|
13
|
-
return { mergeArgsExpr, expansionExpressions };
|
14
|
-
}
|
15
|
-
let finalSegments = [segments[0]];
|
16
|
-
for (let i = 1; i < segments.length; i++) {
|
17
|
-
const additionalSegments = segments[i].split(']]');
|
18
|
-
if (additionalSegments.length !== 2) {
|
19
|
-
throw new Error(`Each opening "[[" must be matched by a closing "]]" without nesting.`);
|
20
|
-
}
|
21
|
-
finalSegments = finalSegments.concat(additionalSegments);
|
22
|
-
}
|
23
|
-
let finalMergeArgsExpr = finalSegments[0];
|
24
|
-
for (let i = 1; i < finalSegments.length - 1; i += 2) {
|
25
|
-
const variableName = `${exports.EXPANSION_PREFIX}${(i - 1) / 2 + 1}`;
|
26
|
-
expansionExpressions[variableName] = finalSegments[i];
|
27
|
-
finalMergeArgsExpr += `\$${variableName}${finalSegments[i + 1]}`;
|
28
|
-
}
|
29
|
-
return { mergeArgsExpr: finalMergeArgsExpr, expansionExpressions };
|
30
|
-
}
|
31
|
-
exports.preparseMergeArgsExpr = preparseMergeArgsExpr;
|
package/cjs/properties.js
DELETED
@@ -1,70 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.propertyTreeFromPaths = exports.getProperties = exports.getProperty = exports.addProperty = void 0;
|
4
|
-
function addProperty(object, path, value) {
|
5
|
-
const initialSegment = path[0];
|
6
|
-
if (path.length === 1) {
|
7
|
-
object[initialSegment] = value;
|
8
|
-
return;
|
9
|
-
}
|
10
|
-
let field = object[initialSegment];
|
11
|
-
if (field != null) {
|
12
|
-
addProperty(field, path.slice(1), value);
|
13
|
-
return;
|
14
|
-
}
|
15
|
-
if (typeof path[1] === 'string') {
|
16
|
-
field = Object.create(null);
|
17
|
-
}
|
18
|
-
else {
|
19
|
-
field = [];
|
20
|
-
}
|
21
|
-
addProperty(field, path.slice(1), value);
|
22
|
-
object[initialSegment] = field;
|
23
|
-
}
|
24
|
-
exports.addProperty = addProperty;
|
25
|
-
function getProperty(object, path) {
|
26
|
-
if (!path.length || object == null) {
|
27
|
-
return object;
|
28
|
-
}
|
29
|
-
const newPath = path.slice();
|
30
|
-
const key = newPath.shift();
|
31
|
-
if (key == null) {
|
32
|
-
return;
|
33
|
-
}
|
34
|
-
const prop = object[key];
|
35
|
-
return getProperty(prop, newPath);
|
36
|
-
}
|
37
|
-
exports.getProperty = getProperty;
|
38
|
-
function getProperties(object, propertyTree) {
|
39
|
-
if (object == null) {
|
40
|
-
return object;
|
41
|
-
}
|
42
|
-
const newObject = Object.create(null);
|
43
|
-
for (const key in propertyTree) {
|
44
|
-
const subKey = propertyTree[key];
|
45
|
-
if (subKey == null) {
|
46
|
-
newObject[key] = object[key];
|
47
|
-
continue;
|
48
|
-
}
|
49
|
-
const prop = object[key];
|
50
|
-
newObject[key] = deepMap(prop, function deepMapFn(item) {
|
51
|
-
return getProperties(item, subKey);
|
52
|
-
});
|
53
|
-
}
|
54
|
-
return newObject;
|
55
|
-
}
|
56
|
-
exports.getProperties = getProperties;
|
57
|
-
function propertyTreeFromPaths(paths) {
|
58
|
-
const propertyTree = Object.create(null);
|
59
|
-
for (const path of paths) {
|
60
|
-
addProperty(propertyTree, path, null);
|
61
|
-
}
|
62
|
-
return propertyTree;
|
63
|
-
}
|
64
|
-
exports.propertyTreeFromPaths = propertyTreeFromPaths;
|
65
|
-
function deepMap(arrayOrItem, fn) {
|
66
|
-
if (Array.isArray(arrayOrItem)) {
|
67
|
-
return arrayOrItem.map(nestedArrayOrItem => deepMap(nestedArrayOrItem, fn));
|
68
|
-
}
|
69
|
-
return fn(arrayOrItem);
|
70
|
-
}
|
@@ -1,78 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.stitchingDirectives = void 0;
|
4
|
-
const graphql_1 = require("graphql");
|
5
|
-
const defaultStitchingDirectiveOptions_js_1 = require("./defaultStitchingDirectiveOptions.js");
|
6
|
-
const stitchingDirectivesTransformer_js_1 = require("./stitchingDirectivesTransformer.js");
|
7
|
-
const stitchingDirectivesValidator_js_1 = require("./stitchingDirectivesValidator.js");
|
8
|
-
function stitchingDirectives(options = {}) {
|
9
|
-
const finalOptions = {
|
10
|
-
...defaultStitchingDirectiveOptions_js_1.defaultStitchingDirectiveOptions,
|
11
|
-
...options,
|
12
|
-
};
|
13
|
-
const { keyDirectiveName, computedDirectiveName, mergeDirectiveName, canonicalDirectiveName } = finalOptions;
|
14
|
-
const keyDirectiveTypeDefs = /* GraphQL */ `directive @${keyDirectiveName}(selectionSet: String!) on OBJECT`;
|
15
|
-
const computedDirectiveTypeDefs = /* GraphQL */ `directive @${computedDirectiveName}(selectionSet: String!) on FIELD_DEFINITION`;
|
16
|
-
const mergeDirectiveTypeDefs = /* GraphQL */ `directive @${mergeDirectiveName}(argsExpr: String, keyArg: String, keyField: String, key: [String!], additionalArgs: String) on FIELD_DEFINITION`;
|
17
|
-
const canonicalDirectiveTypeDefs = /* GraphQL */ `directive @${canonicalDirectiveName} on OBJECT | INTERFACE | INPUT_OBJECT | UNION | ENUM | SCALAR | FIELD_DEFINITION | INPUT_FIELD_DEFINITION`;
|
18
|
-
const keyDirective = new graphql_1.GraphQLDirective({
|
19
|
-
name: keyDirectiveName,
|
20
|
-
locations: ['OBJECT'],
|
21
|
-
args: {
|
22
|
-
selectionSet: { type: new graphql_1.GraphQLNonNull(graphql_1.GraphQLString) },
|
23
|
-
},
|
24
|
-
});
|
25
|
-
const computedDirective = new graphql_1.GraphQLDirective({
|
26
|
-
name: computedDirectiveName,
|
27
|
-
locations: ['FIELD_DEFINITION'],
|
28
|
-
args: {
|
29
|
-
selectionSet: { type: new graphql_1.GraphQLNonNull(graphql_1.GraphQLString) },
|
30
|
-
},
|
31
|
-
});
|
32
|
-
const mergeDirective = new graphql_1.GraphQLDirective({
|
33
|
-
name: mergeDirectiveName,
|
34
|
-
locations: ['FIELD_DEFINITION'],
|
35
|
-
args: {
|
36
|
-
argsExpr: { type: graphql_1.GraphQLString },
|
37
|
-
keyArg: { type: graphql_1.GraphQLString },
|
38
|
-
keyField: { type: graphql_1.GraphQLString },
|
39
|
-
key: { type: new graphql_1.GraphQLList(new graphql_1.GraphQLNonNull(graphql_1.GraphQLString)) },
|
40
|
-
additionalArgs: { type: graphql_1.GraphQLString },
|
41
|
-
},
|
42
|
-
});
|
43
|
-
const canonicalDirective = new graphql_1.GraphQLDirective({
|
44
|
-
name: canonicalDirectiveName,
|
45
|
-
locations: [
|
46
|
-
'OBJECT',
|
47
|
-
'INTERFACE',
|
48
|
-
'INPUT_OBJECT',
|
49
|
-
'UNION',
|
50
|
-
'ENUM',
|
51
|
-
'SCALAR',
|
52
|
-
'FIELD_DEFINITION',
|
53
|
-
'INPUT_FIELD_DEFINITION',
|
54
|
-
],
|
55
|
-
});
|
56
|
-
const allStitchingDirectivesTypeDefs = [
|
57
|
-
keyDirectiveTypeDefs,
|
58
|
-
computedDirectiveTypeDefs,
|
59
|
-
mergeDirectiveTypeDefs,
|
60
|
-
canonicalDirectiveTypeDefs,
|
61
|
-
].join('\n');
|
62
|
-
return {
|
63
|
-
keyDirectiveTypeDefs,
|
64
|
-
computedDirectiveTypeDefs,
|
65
|
-
mergeDirectiveTypeDefs,
|
66
|
-
canonicalDirectiveTypeDefs,
|
67
|
-
stitchingDirectivesTypeDefs: allStitchingDirectivesTypeDefs, // for backwards compatibility
|
68
|
-
allStitchingDirectivesTypeDefs,
|
69
|
-
keyDirective,
|
70
|
-
computedDirective,
|
71
|
-
mergeDirective,
|
72
|
-
canonicalDirective,
|
73
|
-
allStitchingDirectives: [keyDirective, computedDirective, mergeDirective, canonicalDirective],
|
74
|
-
stitchingDirectivesValidator: (0, stitchingDirectivesValidator_js_1.stitchingDirectivesValidator)(finalOptions),
|
75
|
-
stitchingDirectivesTransformer: (0, stitchingDirectivesTransformer_js_1.stitchingDirectivesTransformer)(finalOptions),
|
76
|
-
};
|
77
|
-
}
|
78
|
-
exports.stitchingDirectives = stitchingDirectives;
|