@graphql-mesh/transform-transfer-schema 0.1.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/cjs/index.js ADDED
@@ -0,0 +1,123 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const graphql_1 = require("graphql");
4
+ const micromatch_1 = require("micromatch");
5
+ const utils_1 = require("@graphql-tools/utils");
6
+ class TransferSchemaTransform {
7
+ constructor(options) {
8
+ this.noWrap = true;
9
+ const { config } = options;
10
+ this.additionalFieldsMap = new Map();
11
+ this.additionalArgsMap = new Map();
12
+ this.additionalFieldsConfig = new Map();
13
+ this.additionalArgsConfig = new Map();
14
+ this.removalFieldsMap = new Map();
15
+ this.removalArgsMap = new Map();
16
+ for (const transfer of config.transfers) {
17
+ const [fromType, fromFieldNameOrGlob, fromArgsGlob] = transfer.from.split('.');
18
+ const rawGlob = fromArgsGlob || fromFieldNameOrGlob;
19
+ const fixedGlob = rawGlob.includes('{') && !rawGlob.includes(',')
20
+ ? rawGlob.replace('{', '').replace('}', '')
21
+ : rawGlob;
22
+ const polishedGlob = fixedGlob.split(', ').join(',').trim();
23
+ const mapName = fromArgsGlob ? 'ArgsMap' : 'FieldsMap';
24
+ const mapKey = fromArgsGlob ? `${fromType}.${fromFieldNameOrGlob}` : fromType;
25
+ if (transfer.action === 'move') {
26
+ const currentRemovalRules = this[`removal${mapName}`].get(mapKey) || new Set();
27
+ currentRemovalRules.add(polishedGlob);
28
+ this[`removal${mapName}`].set(mapKey, currentRemovalRules);
29
+ }
30
+ const currentAdditionalRules = this[`additional${mapName}`].get(mapKey) || new Set();
31
+ currentAdditionalRules.add(`${transfer.to}.${polishedGlob}`);
32
+ this[`additional${mapName}`].set(mapKey, currentAdditionalRules);
33
+ }
34
+ }
35
+ handleAdditions(rulesArray, value, config) {
36
+ for (const rule of rulesArray) {
37
+ const [toType, toField, pattern] = rule.split('.');
38
+ const usePattern = pattern || toField;
39
+ const mapIdentifier = pattern ? 'ArgsConfig' : 'FieldsConfig';
40
+ const mapKey = pattern ? `${toType}.${toField}` : toType;
41
+ const isMatch = (0, micromatch_1.matcher)(usePattern);
42
+ if (isMatch(value)) {
43
+ const currentAdditionalConfigs = this[`additional${mapIdentifier}`].get(mapKey) || {};
44
+ this[`additional${mapIdentifier}`].set(mapKey, {
45
+ ...currentAdditionalConfigs,
46
+ [value]: config,
47
+ });
48
+ break;
49
+ }
50
+ }
51
+ }
52
+ matchInSet(rulesSet, value) {
53
+ for (const rule of rulesSet) {
54
+ const isMatch = (0, micromatch_1.matcher)(rule);
55
+ if (isMatch(value))
56
+ return true;
57
+ }
58
+ return undefined;
59
+ }
60
+ transformSchema(schema) {
61
+ // initial mapSchema is necessary to store fields/args configs that will be attached to Types/fields in second map
62
+ const schemaWithRemovals = (0, utils_1.mapSchema)(schema, {
63
+ [utils_1.MapperKind.COMPOSITE_FIELD]: (fieldConfig, fieldName, typeName) => {
64
+ const additionalFieldRules = this.additionalFieldsMap.get(typeName);
65
+ const additionalArgRules = this.additionalArgsMap.get(`${typeName}.${fieldName}`);
66
+ const removalFieldRules = this.removalFieldsMap.get(typeName);
67
+ const removalArgRules = this.removalArgsMap.get(`${typeName}.${fieldName}`);
68
+ const hasAdditionalFieldRules = Boolean(additionalFieldRules);
69
+ const hasAdditionalArgRules = Boolean(additionalArgRules);
70
+ const hasRemovalFieldRules = Boolean(removalFieldRules && removalFieldRules.size);
71
+ const hasRemovalArgRules = Boolean(removalArgRules && removalArgRules.size);
72
+ // handle field addition
73
+ if (hasAdditionalFieldRules) {
74
+ this.handleAdditions(additionalFieldRules, fieldName, fieldConfig);
75
+ }
76
+ // handle args addition
77
+ if (hasAdditionalArgRules) {
78
+ for (const [argName, argConfig] of Object.entries(fieldConfig.args)) {
79
+ this.handleAdditions(additionalArgRules, argName, argConfig);
80
+ }
81
+ }
82
+ // handle field removal
83
+ if (hasRemovalFieldRules && this.matchInSet(removalFieldRules, fieldName))
84
+ return null;
85
+ // handle args removal
86
+ if (hasRemovalArgRules) {
87
+ const newArgs = Object.entries(fieldConfig.args).reduce((args, [argName, argConfig]) => {
88
+ return this.matchInSet(removalArgRules, argName)
89
+ ? args
90
+ : { ...args, [argName]: argConfig };
91
+ }, {});
92
+ return { ...fieldConfig, args: newArgs };
93
+ }
94
+ return undefined;
95
+ },
96
+ });
97
+ const schemaWithAdditions = (0, utils_1.mapSchema)(schemaWithRemovals, {
98
+ ...(this.additionalFieldsConfig.size && {
99
+ [utils_1.MapperKind.OBJECT_TYPE]: type => {
100
+ const additionalFieldsConfigMap = this.additionalFieldsConfig.get(type.toString());
101
+ if (!additionalFieldsConfigMap)
102
+ return undefined;
103
+ const newConfig = { ...type.toConfig() };
104
+ newConfig.fields = { ...newConfig.fields, ...additionalFieldsConfigMap };
105
+ return new graphql_1.GraphQLObjectType(newConfig);
106
+ },
107
+ }),
108
+ ...(this.additionalArgsConfig.size && {
109
+ [utils_1.MapperKind.COMPOSITE_FIELD]: (fieldConfig, fieldName, typeName) => {
110
+ const additionalArgsConfigMap = this.additionalArgsConfig.get(`${typeName}.${fieldName}`);
111
+ if (!additionalArgsConfigMap)
112
+ return undefined;
113
+ return {
114
+ ...fieldConfig,
115
+ args: { ...(fieldConfig.args || {}), ...additionalArgsConfigMap },
116
+ };
117
+ },
118
+ }),
119
+ });
120
+ return (0, utils_1.pruneSchema)(schemaWithAdditions);
121
+ }
122
+ }
123
+ exports.default = TransferSchemaTransform;
@@ -0,0 +1 @@
1
+ {"type":"commonjs"}
package/esm/index.js ADDED
@@ -0,0 +1,120 @@
1
+ import { GraphQLObjectType, } from 'graphql';
2
+ import { matcher } from 'micromatch';
3
+ import { MapperKind, mapSchema, pruneSchema } from '@graphql-tools/utils';
4
+ export default class TransferSchemaTransform {
5
+ constructor(options) {
6
+ this.noWrap = true;
7
+ const { config } = options;
8
+ this.additionalFieldsMap = new Map();
9
+ this.additionalArgsMap = new Map();
10
+ this.additionalFieldsConfig = new Map();
11
+ this.additionalArgsConfig = new Map();
12
+ this.removalFieldsMap = new Map();
13
+ this.removalArgsMap = new Map();
14
+ for (const transfer of config.transfers) {
15
+ const [fromType, fromFieldNameOrGlob, fromArgsGlob] = transfer.from.split('.');
16
+ const rawGlob = fromArgsGlob || fromFieldNameOrGlob;
17
+ const fixedGlob = rawGlob.includes('{') && !rawGlob.includes(',')
18
+ ? rawGlob.replace('{', '').replace('}', '')
19
+ : rawGlob;
20
+ const polishedGlob = fixedGlob.split(', ').join(',').trim();
21
+ const mapName = fromArgsGlob ? 'ArgsMap' : 'FieldsMap';
22
+ const mapKey = fromArgsGlob ? `${fromType}.${fromFieldNameOrGlob}` : fromType;
23
+ if (transfer.action === 'move') {
24
+ const currentRemovalRules = this[`removal${mapName}`].get(mapKey) || new Set();
25
+ currentRemovalRules.add(polishedGlob);
26
+ this[`removal${mapName}`].set(mapKey, currentRemovalRules);
27
+ }
28
+ const currentAdditionalRules = this[`additional${mapName}`].get(mapKey) || new Set();
29
+ currentAdditionalRules.add(`${transfer.to}.${polishedGlob}`);
30
+ this[`additional${mapName}`].set(mapKey, currentAdditionalRules);
31
+ }
32
+ }
33
+ handleAdditions(rulesArray, value, config) {
34
+ for (const rule of rulesArray) {
35
+ const [toType, toField, pattern] = rule.split('.');
36
+ const usePattern = pattern || toField;
37
+ const mapIdentifier = pattern ? 'ArgsConfig' : 'FieldsConfig';
38
+ const mapKey = pattern ? `${toType}.${toField}` : toType;
39
+ const isMatch = matcher(usePattern);
40
+ if (isMatch(value)) {
41
+ const currentAdditionalConfigs = this[`additional${mapIdentifier}`].get(mapKey) || {};
42
+ this[`additional${mapIdentifier}`].set(mapKey, {
43
+ ...currentAdditionalConfigs,
44
+ [value]: config,
45
+ });
46
+ break;
47
+ }
48
+ }
49
+ }
50
+ matchInSet(rulesSet, value) {
51
+ for (const rule of rulesSet) {
52
+ const isMatch = matcher(rule);
53
+ if (isMatch(value))
54
+ return true;
55
+ }
56
+ return undefined;
57
+ }
58
+ transformSchema(schema) {
59
+ // initial mapSchema is necessary to store fields/args configs that will be attached to Types/fields in second map
60
+ const schemaWithRemovals = mapSchema(schema, {
61
+ [MapperKind.COMPOSITE_FIELD]: (fieldConfig, fieldName, typeName) => {
62
+ const additionalFieldRules = this.additionalFieldsMap.get(typeName);
63
+ const additionalArgRules = this.additionalArgsMap.get(`${typeName}.${fieldName}`);
64
+ const removalFieldRules = this.removalFieldsMap.get(typeName);
65
+ const removalArgRules = this.removalArgsMap.get(`${typeName}.${fieldName}`);
66
+ const hasAdditionalFieldRules = Boolean(additionalFieldRules);
67
+ const hasAdditionalArgRules = Boolean(additionalArgRules);
68
+ const hasRemovalFieldRules = Boolean(removalFieldRules && removalFieldRules.size);
69
+ const hasRemovalArgRules = Boolean(removalArgRules && removalArgRules.size);
70
+ // handle field addition
71
+ if (hasAdditionalFieldRules) {
72
+ this.handleAdditions(additionalFieldRules, fieldName, fieldConfig);
73
+ }
74
+ // handle args addition
75
+ if (hasAdditionalArgRules) {
76
+ for (const [argName, argConfig] of Object.entries(fieldConfig.args)) {
77
+ this.handleAdditions(additionalArgRules, argName, argConfig);
78
+ }
79
+ }
80
+ // handle field removal
81
+ if (hasRemovalFieldRules && this.matchInSet(removalFieldRules, fieldName))
82
+ return null;
83
+ // handle args removal
84
+ if (hasRemovalArgRules) {
85
+ const newArgs = Object.entries(fieldConfig.args).reduce((args, [argName, argConfig]) => {
86
+ return this.matchInSet(removalArgRules, argName)
87
+ ? args
88
+ : { ...args, [argName]: argConfig };
89
+ }, {});
90
+ return { ...fieldConfig, args: newArgs };
91
+ }
92
+ return undefined;
93
+ },
94
+ });
95
+ const schemaWithAdditions = mapSchema(schemaWithRemovals, {
96
+ ...(this.additionalFieldsConfig.size && {
97
+ [MapperKind.OBJECT_TYPE]: type => {
98
+ const additionalFieldsConfigMap = this.additionalFieldsConfig.get(type.toString());
99
+ if (!additionalFieldsConfigMap)
100
+ return undefined;
101
+ const newConfig = { ...type.toConfig() };
102
+ newConfig.fields = { ...newConfig.fields, ...additionalFieldsConfigMap };
103
+ return new GraphQLObjectType(newConfig);
104
+ },
105
+ }),
106
+ ...(this.additionalArgsConfig.size && {
107
+ [MapperKind.COMPOSITE_FIELD]: (fieldConfig, fieldName, typeName) => {
108
+ const additionalArgsConfigMap = this.additionalArgsConfig.get(`${typeName}.${fieldName}`);
109
+ if (!additionalArgsConfigMap)
110
+ return undefined;
111
+ return {
112
+ ...fieldConfig,
113
+ args: { ...(fieldConfig.args || {}), ...additionalArgsConfigMap },
114
+ };
115
+ },
116
+ }),
117
+ });
118
+ return pruneSchema(schemaWithAdditions);
119
+ }
120
+ }
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@graphql-mesh/transform-transfer-schema",
3
+ "version": "0.1.2",
4
+ "sideEffects": false,
5
+ "peerDependencies": {
6
+ "@graphql-mesh/types": "^0.91.12",
7
+ "@graphql-mesh/utils": "^0.43.20",
8
+ "@graphql-tools/utils": "^9.2.1",
9
+ "graphql": "*"
10
+ },
11
+ "dependencies": {
12
+ "micromatch": "4.0.4"
13
+ },
14
+ "license": "MIT",
15
+ "main": "cjs/index.js",
16
+ "module": "esm/index.js",
17
+ "typings": "typings/index.d.ts",
18
+ "typescript": {
19
+ "definition": "typings/index.d.ts"
20
+ },
21
+ "exports": {
22
+ ".": {
23
+ "require": {
24
+ "types": "./typings/index.d.cts",
25
+ "default": "./cjs/index.js"
26
+ },
27
+ "import": {
28
+ "types": "./typings/index.d.ts",
29
+ "default": "./esm/index.js"
30
+ },
31
+ "default": {
32
+ "types": "./typings/index.d.ts",
33
+ "default": "./esm/index.js"
34
+ }
35
+ },
36
+ "./package.json": "./package.json"
37
+ }
38
+ }
@@ -0,0 +1,15 @@
1
+ import { GraphQLSchema } from 'graphql';
2
+ import { MeshTransform, MeshTransformOptions, YamlConfig } from '@graphql-mesh/types';
3
+ export default class TransferSchemaTransform implements MeshTransform {
4
+ noWrap: boolean;
5
+ private additionalFieldsMap;
6
+ private additionalArgsMap;
7
+ private additionalFieldsConfig;
8
+ private additionalArgsConfig;
9
+ private removalFieldsMap;
10
+ private removalArgsMap;
11
+ constructor(options: MeshTransformOptions<YamlConfig.TransferSchemaTransformConfig>);
12
+ handleAdditions(rulesArray: Set<string>, value: string, config: any): void;
13
+ matchInSet(rulesSet: Set<string>, value: string): true | undefined;
14
+ transformSchema(schema: GraphQLSchema): GraphQLSchema;
15
+ }
@@ -0,0 +1,15 @@
1
+ import { GraphQLSchema } from 'graphql';
2
+ import { MeshTransform, MeshTransformOptions, YamlConfig } from '@graphql-mesh/types';
3
+ export default class TransferSchemaTransform implements MeshTransform {
4
+ noWrap: boolean;
5
+ private additionalFieldsMap;
6
+ private additionalArgsMap;
7
+ private additionalFieldsConfig;
8
+ private additionalArgsConfig;
9
+ private removalFieldsMap;
10
+ private removalArgsMap;
11
+ constructor(options: MeshTransformOptions<YamlConfig.TransferSchemaTransformConfig>);
12
+ handleAdditions(rulesArray: Set<string>, value: string, config: any): void;
13
+ matchInSet(rulesSet: Set<string>, value: string): true | undefined;
14
+ transformSchema(schema: GraphQLSchema): GraphQLSchema;
15
+ }