@graphql-mesh/transform-rename 1.0.0-alpha-20220804093904-8e2e41f7f → 1.0.0-alpha-20230420220344-25b6b92bf

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.
@@ -0,0 +1,124 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const graphql_1 = require("graphql");
4
+ const utils_1 = require("@graphql-tools/utils");
5
+ const shared_js_1 = require("./shared.js");
6
+ // Resolver composer mapping renamed field and arguments
7
+ const defaultResolverComposer = (resolveFn = graphql_1.defaultFieldResolver, originalFieldName, argsMap) => (root, args, context, info) => resolveFn(root,
8
+ // map renamed arguments to their original value
9
+ argsMap
10
+ ? Object.keys(args).reduce((acc, key) => ({ ...acc, [argsMap[key] || key]: args[key] }), {})
11
+ : args, context,
12
+ // map renamed field name to its original value
13
+ originalFieldName ? { ...info, fieldName: originalFieldName } : info);
14
+ class BareRename {
15
+ constructor({ config }) {
16
+ this.noWrap = true;
17
+ this.typesMap = new Map();
18
+ this.fieldsMap = new Map();
19
+ this.argsMap = new Map();
20
+ for (const rename of config.renames) {
21
+ const { from: { type: fromTypeName, field: fromFieldName, argument: fromArgName }, to: { type: toTypeName, field: toFieldName, argument: toArgName }, useRegExpForTypes, useRegExpForFields, } = rename;
22
+ const regExpFlags = rename.regExpFlags || undefined;
23
+ if (fromTypeName &&
24
+ !fromFieldName &&
25
+ toTypeName &&
26
+ !toFieldName &&
27
+ fromTypeName !== toTypeName) {
28
+ this.typesMap.set(useRegExpForTypes ? new RegExp(fromTypeName, regExpFlags) : fromTypeName, toTypeName);
29
+ }
30
+ if (fromTypeName &&
31
+ fromFieldName &&
32
+ toTypeName &&
33
+ toFieldName &&
34
+ fromFieldName !== toFieldName) {
35
+ const fromName = useRegExpForFields
36
+ ? new RegExp(fromFieldName, regExpFlags)
37
+ : fromFieldName;
38
+ const typeMap = this.fieldsMap.get(fromTypeName) || new Map();
39
+ this.fieldsMap.set(fromTypeName, typeMap.set(fromName, toFieldName));
40
+ }
41
+ if (fromTypeName &&
42
+ fromFieldName &&
43
+ fromArgName &&
44
+ toTypeName &&
45
+ toFieldName &&
46
+ toArgName &&
47
+ fromArgName !== toArgName) {
48
+ const fromName = useRegExpForFields ? new RegExp(fromArgName, regExpFlags) : fromArgName;
49
+ const key = `${fromTypeName}.${fromFieldName}`;
50
+ const typeMap = this.argsMap.get(key) || new Map();
51
+ this.argsMap.set(key, typeMap.set(fromName, toArgName));
52
+ }
53
+ }
54
+ }
55
+ matchInMap(map, toMatch) {
56
+ const mapKeyIsString = map.has(toMatch);
57
+ const mapKey = mapKeyIsString
58
+ ? toMatch
59
+ : [...map.keys()].find(key => typeof key !== 'string' && key.test(toMatch));
60
+ if (!mapKey)
61
+ return null;
62
+ const newName = mapKeyIsString ? map.get(mapKey) : toMatch.replace(mapKey, map.get(mapKey));
63
+ // avoid re-iterating over strings that have already been renamed
64
+ if (mapKeyIsString)
65
+ map.delete(mapKey);
66
+ return newName;
67
+ }
68
+ renameType(type) {
69
+ const newTypeName = shared_js_1.ignoreList.includes(type.toString())
70
+ ? null
71
+ : this.matchInMap(this.typesMap, type.toString());
72
+ return newTypeName ? (0, utils_1.renameType)(type, newTypeName) : undefined;
73
+ }
74
+ transformSchema(schema) {
75
+ return (0, utils_1.mapSchema)(schema, {
76
+ ...(this.typesMap.size && {
77
+ [utils_1.MapperKind.TYPE]: type => this.renameType(type),
78
+ [utils_1.MapperKind.ABSTRACT_TYPE]: type => {
79
+ const currentName = type.toString();
80
+ const newName = shared_js_1.ignoreList.includes(currentName)
81
+ ? null
82
+ : this.matchInMap(this.typesMap, currentName);
83
+ const existingResolver = type.resolveType;
84
+ type.resolveType = async (data, context, info, abstractType) => {
85
+ const originalResolvedTypename = await existingResolver(data, context, info, abstractType);
86
+ const newTypename = shared_js_1.ignoreList.includes(originalResolvedTypename)
87
+ ? null
88
+ : this.matchInMap(this.typesMap, originalResolvedTypename);
89
+ return newTypename || originalResolvedTypename;
90
+ };
91
+ if (newName && newName !== currentName) {
92
+ return (0, utils_1.renameType)(type, newName);
93
+ }
94
+ return undefined;
95
+ },
96
+ [utils_1.MapperKind.ROOT_OBJECT]: type => this.renameType(type),
97
+ }),
98
+ ...((this.fieldsMap.size || this.argsMap.size) && {
99
+ [utils_1.MapperKind.COMPOSITE_FIELD]: (fieldConfig, fieldName, typeName) => {
100
+ const typeRules = this.fieldsMap.get(typeName);
101
+ const fieldRules = this.argsMap.get(`${typeName}.${fieldName}`);
102
+ const newFieldName = typeRules && this.matchInMap(typeRules, fieldName);
103
+ const argsMap = fieldRules &&
104
+ Array.from(fieldRules.entries()).reduce((acc, [orName, newName]) => ({ ...acc, [newName]: orName }), {});
105
+ if (!newFieldName && !fieldRules)
106
+ return undefined;
107
+ // Rename rules for type might have been emptied by matchInMap, in which case we can cleanup
108
+ if (!(typeRules === null || typeRules === void 0 ? void 0 : typeRules.size))
109
+ this.fieldsMap.delete(typeName);
110
+ if (fieldRules && fieldConfig.args) {
111
+ fieldConfig.args = Object.entries(fieldConfig.args).reduce((args, [argName, argConfig]) => ({
112
+ ...args,
113
+ [this.matchInMap(fieldRules, argName) || argName]: argConfig,
114
+ }), {});
115
+ }
116
+ // Wrap resolve fn to handle mapping renamed field name and/or renamed arguments
117
+ fieldConfig.resolve = defaultResolverComposer(fieldConfig.resolve, fieldName, argsMap);
118
+ return [newFieldName || fieldName, fieldConfig];
119
+ },
120
+ }),
121
+ });
122
+ }
123
+ }
124
+ exports.default = BareRename;
package/cjs/index.js ADDED
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ const bareRename_js_1 = tslib_1.__importDefault(require("./bareRename.js"));
5
+ const wrapRename_js_1 = tslib_1.__importDefault(require("./wrapRename.js"));
6
+ exports.default = (function RenameTransform(options) {
7
+ if (Array.isArray(options.config)) {
8
+ return new wrapRename_js_1.default({
9
+ config: {
10
+ mode: 'wrap',
11
+ renames: options.config,
12
+ },
13
+ });
14
+ }
15
+ return options.config.mode === 'bare' ? new bareRename_js_1.default(options) : new wrapRename_js_1.default(options);
16
+ });
@@ -0,0 +1 @@
1
+ {"type":"commonjs"}
package/cjs/shared.js ADDED
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ignoreList = void 0;
4
+ const graphql_scalars_1 = require("graphql-scalars");
5
+ exports.ignoreList = [
6
+ 'Int',
7
+ 'Float',
8
+ 'String',
9
+ 'Boolean',
10
+ 'ID',
11
+ 'date',
12
+ 'hostname',
13
+ 'regex',
14
+ 'json-pointer',
15
+ 'relative-json-pointer',
16
+ 'uri-reference',
17
+ 'uri-template',
18
+ 'ObjMap',
19
+ 'HttpMethod',
20
+ ...Object.keys(graphql_scalars_1.resolvers),
21
+ ];
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const utils_1 = require("@graphql-mesh/utils");
4
+ const wrap_1 = require("@graphql-tools/wrap");
5
+ const shared_js_1 = require("./shared.js");
6
+ class WrapRename {
7
+ constructor({ config }) {
8
+ this.transforms = [];
9
+ for (const change of config.renames) {
10
+ const { from: { type: fromTypeName, field: fromFieldName, argument: fromArgumentName }, to: { type: toTypeName, field: toFieldName, argument: toArgumentName }, useRegExpForTypes, useRegExpForFields, useRegExpForArguments, } = change;
11
+ const regExpFlags = change.regExpFlags || undefined;
12
+ if (fromTypeName !== toTypeName) {
13
+ let replaceTypeNameFn;
14
+ if (useRegExpForTypes) {
15
+ const typeNameRegExp = new RegExp(fromTypeName, regExpFlags);
16
+ replaceTypeNameFn = (t) => t.replace(typeNameRegExp, toTypeName);
17
+ }
18
+ else {
19
+ replaceTypeNameFn = t => (t === fromTypeName ? toTypeName : t);
20
+ }
21
+ this.transforms.push(new wrap_1.RenameTypes(typeName => {
22
+ if (shared_js_1.ignoreList.includes(typeName)) {
23
+ return typeName;
24
+ }
25
+ return replaceTypeNameFn(typeName);
26
+ }));
27
+ }
28
+ if (fromFieldName && toFieldName && fromFieldName !== toFieldName) {
29
+ let replaceFieldNameFn;
30
+ if (useRegExpForFields) {
31
+ const fieldNameRegExp = new RegExp(fromFieldName, regExpFlags);
32
+ replaceFieldNameFn = (typeName, fieldName) => typeName === toTypeName ? fieldName.replace(fieldNameRegExp, toFieldName) : fieldName;
33
+ }
34
+ else {
35
+ replaceFieldNameFn = (typeName, fieldName) => typeName === toTypeName && fieldName === fromFieldName ? toFieldName : fieldName;
36
+ }
37
+ this.transforms.push(new wrap_1.RenameObjectFields(replaceFieldNameFn));
38
+ this.transforms.push(new wrap_1.RenameInputObjectFields(replaceFieldNameFn));
39
+ }
40
+ if (fromTypeName &&
41
+ (fromTypeName === toTypeName || useRegExpForTypes) &&
42
+ toFieldName &&
43
+ (fromFieldName === toFieldName || useRegExpForFields) &&
44
+ fromArgumentName &&
45
+ fromArgumentName !== toArgumentName) {
46
+ let replaceArgNameFn;
47
+ const fieldNameMatch = (fieldName) => fieldName ===
48
+ (useRegExpForFields
49
+ ? fieldName.replace(new RegExp(fromFieldName, regExpFlags), toFieldName)
50
+ : toFieldName);
51
+ const typeNameMatch = (typeName) => typeName ===
52
+ (useRegExpForTypes
53
+ ? typeName.replace(new RegExp(fromTypeName, regExpFlags), toTypeName)
54
+ : toTypeName);
55
+ if (useRegExpForArguments) {
56
+ const argNameRegExp = new RegExp(fromArgumentName, regExpFlags);
57
+ replaceArgNameFn = (typeName, fieldName, argName) => typeNameMatch(typeName) && fieldNameMatch(fieldName)
58
+ ? argName.replace(argNameRegExp, toArgumentName)
59
+ : argName;
60
+ }
61
+ else {
62
+ replaceArgNameFn = (typeName, fieldName, argName) => typeNameMatch(typeName) && fieldNameMatch(fieldName) && argName === fromArgumentName
63
+ ? toArgumentName
64
+ : argName;
65
+ }
66
+ this.transforms.push(new wrap_1.RenameObjectFieldArguments(replaceArgNameFn));
67
+ }
68
+ }
69
+ }
70
+ transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
71
+ return (0, utils_1.applySchemaTransforms)(originalWrappingSchema, subschemaConfig, transformedSchema, this.transforms);
72
+ }
73
+ transformRequest(originalRequest, delegationContext, transformationContext) {
74
+ return (0, utils_1.applyRequestTransforms)(originalRequest, delegationContext, transformationContext, this.transforms);
75
+ }
76
+ transformResult(originalResult, delegationContext, transformationContext) {
77
+ return (0, utils_1.applyResultTransforms)(originalResult, delegationContext, transformationContext, this.transforms);
78
+ }
79
+ }
80
+ exports.default = WrapRename;
@@ -0,0 +1,121 @@
1
+ import { defaultFieldResolver, } from 'graphql';
2
+ import { MapperKind, mapSchema, renameType } from '@graphql-tools/utils';
3
+ import { ignoreList } from './shared.js';
4
+ // Resolver composer mapping renamed field and arguments
5
+ const defaultResolverComposer = (resolveFn = defaultFieldResolver, originalFieldName, argsMap) => (root, args, context, info) => resolveFn(root,
6
+ // map renamed arguments to their original value
7
+ argsMap
8
+ ? Object.keys(args).reduce((acc, key) => ({ ...acc, [argsMap[key] || key]: args[key] }), {})
9
+ : args, context,
10
+ // map renamed field name to its original value
11
+ originalFieldName ? { ...info, fieldName: originalFieldName } : info);
12
+ export default class BareRename {
13
+ constructor({ config }) {
14
+ this.noWrap = true;
15
+ this.typesMap = new Map();
16
+ this.fieldsMap = new Map();
17
+ this.argsMap = new Map();
18
+ for (const rename of config.renames) {
19
+ const { from: { type: fromTypeName, field: fromFieldName, argument: fromArgName }, to: { type: toTypeName, field: toFieldName, argument: toArgName }, useRegExpForTypes, useRegExpForFields, } = rename;
20
+ const regExpFlags = rename.regExpFlags || undefined;
21
+ if (fromTypeName &&
22
+ !fromFieldName &&
23
+ toTypeName &&
24
+ !toFieldName &&
25
+ fromTypeName !== toTypeName) {
26
+ this.typesMap.set(useRegExpForTypes ? new RegExp(fromTypeName, regExpFlags) : fromTypeName, toTypeName);
27
+ }
28
+ if (fromTypeName &&
29
+ fromFieldName &&
30
+ toTypeName &&
31
+ toFieldName &&
32
+ fromFieldName !== toFieldName) {
33
+ const fromName = useRegExpForFields
34
+ ? new RegExp(fromFieldName, regExpFlags)
35
+ : fromFieldName;
36
+ const typeMap = this.fieldsMap.get(fromTypeName) || new Map();
37
+ this.fieldsMap.set(fromTypeName, typeMap.set(fromName, toFieldName));
38
+ }
39
+ if (fromTypeName &&
40
+ fromFieldName &&
41
+ fromArgName &&
42
+ toTypeName &&
43
+ toFieldName &&
44
+ toArgName &&
45
+ fromArgName !== toArgName) {
46
+ const fromName = useRegExpForFields ? new RegExp(fromArgName, regExpFlags) : fromArgName;
47
+ const key = `${fromTypeName}.${fromFieldName}`;
48
+ const typeMap = this.argsMap.get(key) || new Map();
49
+ this.argsMap.set(key, typeMap.set(fromName, toArgName));
50
+ }
51
+ }
52
+ }
53
+ matchInMap(map, toMatch) {
54
+ const mapKeyIsString = map.has(toMatch);
55
+ const mapKey = mapKeyIsString
56
+ ? toMatch
57
+ : [...map.keys()].find(key => typeof key !== 'string' && key.test(toMatch));
58
+ if (!mapKey)
59
+ return null;
60
+ const newName = mapKeyIsString ? map.get(mapKey) : toMatch.replace(mapKey, map.get(mapKey));
61
+ // avoid re-iterating over strings that have already been renamed
62
+ if (mapKeyIsString)
63
+ map.delete(mapKey);
64
+ return newName;
65
+ }
66
+ renameType(type) {
67
+ const newTypeName = ignoreList.includes(type.toString())
68
+ ? null
69
+ : this.matchInMap(this.typesMap, type.toString());
70
+ return newTypeName ? renameType(type, newTypeName) : undefined;
71
+ }
72
+ transformSchema(schema) {
73
+ return mapSchema(schema, {
74
+ ...(this.typesMap.size && {
75
+ [MapperKind.TYPE]: type => this.renameType(type),
76
+ [MapperKind.ABSTRACT_TYPE]: type => {
77
+ const currentName = type.toString();
78
+ const newName = ignoreList.includes(currentName)
79
+ ? null
80
+ : this.matchInMap(this.typesMap, currentName);
81
+ const existingResolver = type.resolveType;
82
+ type.resolveType = async (data, context, info, abstractType) => {
83
+ const originalResolvedTypename = await existingResolver(data, context, info, abstractType);
84
+ const newTypename = ignoreList.includes(originalResolvedTypename)
85
+ ? null
86
+ : this.matchInMap(this.typesMap, originalResolvedTypename);
87
+ return newTypename || originalResolvedTypename;
88
+ };
89
+ if (newName && newName !== currentName) {
90
+ return renameType(type, newName);
91
+ }
92
+ return undefined;
93
+ },
94
+ [MapperKind.ROOT_OBJECT]: type => this.renameType(type),
95
+ }),
96
+ ...((this.fieldsMap.size || this.argsMap.size) && {
97
+ [MapperKind.COMPOSITE_FIELD]: (fieldConfig, fieldName, typeName) => {
98
+ const typeRules = this.fieldsMap.get(typeName);
99
+ const fieldRules = this.argsMap.get(`${typeName}.${fieldName}`);
100
+ const newFieldName = typeRules && this.matchInMap(typeRules, fieldName);
101
+ const argsMap = fieldRules &&
102
+ Array.from(fieldRules.entries()).reduce((acc, [orName, newName]) => ({ ...acc, [newName]: orName }), {});
103
+ if (!newFieldName && !fieldRules)
104
+ return undefined;
105
+ // Rename rules for type might have been emptied by matchInMap, in which case we can cleanup
106
+ if (!(typeRules === null || typeRules === void 0 ? void 0 : typeRules.size))
107
+ this.fieldsMap.delete(typeName);
108
+ if (fieldRules && fieldConfig.args) {
109
+ fieldConfig.args = Object.entries(fieldConfig.args).reduce((args, [argName, argConfig]) => ({
110
+ ...args,
111
+ [this.matchInMap(fieldRules, argName) || argName]: argConfig,
112
+ }), {});
113
+ }
114
+ // Wrap resolve fn to handle mapping renamed field name and/or renamed arguments
115
+ fieldConfig.resolve = defaultResolverComposer(fieldConfig.resolve, fieldName, argsMap);
116
+ return [newFieldName || fieldName, fieldConfig];
117
+ },
118
+ }),
119
+ });
120
+ }
121
+ }
package/esm/index.js ADDED
@@ -0,0 +1,13 @@
1
+ import BareRename from './bareRename.js';
2
+ import WrapRename from './wrapRename.js';
3
+ export default (function RenameTransform(options) {
4
+ if (Array.isArray(options.config)) {
5
+ return new WrapRename({
6
+ config: {
7
+ mode: 'wrap',
8
+ renames: options.config,
9
+ },
10
+ });
11
+ }
12
+ return options.config.mode === 'bare' ? new BareRename(options) : new WrapRename(options);
13
+ });
package/esm/shared.js ADDED
@@ -0,0 +1,18 @@
1
+ import { resolvers as scalarsResolversMap } from 'graphql-scalars';
2
+ export const ignoreList = [
3
+ 'Int',
4
+ 'Float',
5
+ 'String',
6
+ 'Boolean',
7
+ 'ID',
8
+ 'date',
9
+ 'hostname',
10
+ 'regex',
11
+ 'json-pointer',
12
+ 'relative-json-pointer',
13
+ 'uri-reference',
14
+ 'uri-template',
15
+ 'ObjMap',
16
+ 'HttpMethod',
17
+ ...Object.keys(scalarsResolversMap),
18
+ ];
@@ -0,0 +1,77 @@
1
+ import { applyRequestTransforms, applyResultTransforms, applySchemaTransforms, } from '@graphql-mesh/utils';
2
+ import { RenameInputObjectFields, RenameObjectFieldArguments, RenameObjectFields, RenameTypes, } from '@graphql-tools/wrap';
3
+ import { ignoreList } from './shared.js';
4
+ export default class WrapRename {
5
+ constructor({ config }) {
6
+ this.transforms = [];
7
+ for (const change of config.renames) {
8
+ const { from: { type: fromTypeName, field: fromFieldName, argument: fromArgumentName }, to: { type: toTypeName, field: toFieldName, argument: toArgumentName }, useRegExpForTypes, useRegExpForFields, useRegExpForArguments, } = change;
9
+ const regExpFlags = change.regExpFlags || undefined;
10
+ if (fromTypeName !== toTypeName) {
11
+ let replaceTypeNameFn;
12
+ if (useRegExpForTypes) {
13
+ const typeNameRegExp = new RegExp(fromTypeName, regExpFlags);
14
+ replaceTypeNameFn = (t) => t.replace(typeNameRegExp, toTypeName);
15
+ }
16
+ else {
17
+ replaceTypeNameFn = t => (t === fromTypeName ? toTypeName : t);
18
+ }
19
+ this.transforms.push(new RenameTypes(typeName => {
20
+ if (ignoreList.includes(typeName)) {
21
+ return typeName;
22
+ }
23
+ return replaceTypeNameFn(typeName);
24
+ }));
25
+ }
26
+ if (fromFieldName && toFieldName && fromFieldName !== toFieldName) {
27
+ let replaceFieldNameFn;
28
+ if (useRegExpForFields) {
29
+ const fieldNameRegExp = new RegExp(fromFieldName, regExpFlags);
30
+ replaceFieldNameFn = (typeName, fieldName) => typeName === toTypeName ? fieldName.replace(fieldNameRegExp, toFieldName) : fieldName;
31
+ }
32
+ else {
33
+ replaceFieldNameFn = (typeName, fieldName) => typeName === toTypeName && fieldName === fromFieldName ? toFieldName : fieldName;
34
+ }
35
+ this.transforms.push(new RenameObjectFields(replaceFieldNameFn));
36
+ this.transforms.push(new RenameInputObjectFields(replaceFieldNameFn));
37
+ }
38
+ if (fromTypeName &&
39
+ (fromTypeName === toTypeName || useRegExpForTypes) &&
40
+ toFieldName &&
41
+ (fromFieldName === toFieldName || useRegExpForFields) &&
42
+ fromArgumentName &&
43
+ fromArgumentName !== toArgumentName) {
44
+ let replaceArgNameFn;
45
+ const fieldNameMatch = (fieldName) => fieldName ===
46
+ (useRegExpForFields
47
+ ? fieldName.replace(new RegExp(fromFieldName, regExpFlags), toFieldName)
48
+ : toFieldName);
49
+ const typeNameMatch = (typeName) => typeName ===
50
+ (useRegExpForTypes
51
+ ? typeName.replace(new RegExp(fromTypeName, regExpFlags), toTypeName)
52
+ : toTypeName);
53
+ if (useRegExpForArguments) {
54
+ const argNameRegExp = new RegExp(fromArgumentName, regExpFlags);
55
+ replaceArgNameFn = (typeName, fieldName, argName) => typeNameMatch(typeName) && fieldNameMatch(fieldName)
56
+ ? argName.replace(argNameRegExp, toArgumentName)
57
+ : argName;
58
+ }
59
+ else {
60
+ replaceArgNameFn = (typeName, fieldName, argName) => typeNameMatch(typeName) && fieldNameMatch(fieldName) && argName === fromArgumentName
61
+ ? toArgumentName
62
+ : argName;
63
+ }
64
+ this.transforms.push(new RenameObjectFieldArguments(replaceArgNameFn));
65
+ }
66
+ }
67
+ }
68
+ transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
69
+ return applySchemaTransforms(originalWrappingSchema, subschemaConfig, transformedSchema, this.transforms);
70
+ }
71
+ transformRequest(originalRequest, delegationContext, transformationContext) {
72
+ return applyRequestTransforms(originalRequest, delegationContext, transformationContext, this.transforms);
73
+ }
74
+ transformResult(originalResult, delegationContext, transformationContext) {
75
+ return applyResultTransforms(originalResult, delegationContext, transformationContext, this.transforms);
76
+ }
77
+ }
package/package.json CHANGED
@@ -1,17 +1,18 @@
1
1
  {
2
2
  "name": "@graphql-mesh/transform-rename",
3
- "version": "1.0.0-alpha-20220804093904-8e2e41f7f",
3
+ "version": "1.0.0-alpha-20230420220344-25b6b92bf",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
- "@graphql-mesh/types": "0.79.0-alpha-20220804093904-8e2e41f7f",
7
- "@graphql-mesh/utils": "1.0.0-alpha-20220804093904-8e2e41f7f",
8
- "graphql": "*"
6
+ "@graphql-mesh/types": "1.0.0-alpha-20230420220344-25b6b92bf",
7
+ "@graphql-mesh/utils": "1.0.0-alpha-20230420220344-25b6b92bf",
8
+ "@graphql-tools/utils": "^9.2.1",
9
+ "graphql": "*",
10
+ "tslib": "^2.4.0"
9
11
  },
10
12
  "dependencies": {
11
- "@graphql-tools/delegate": "8.8.1",
12
- "@graphql-tools/utils": "8.9.0",
13
- "@graphql-tools/wrap": "8.5.1",
14
- "tslib": "^2.4.0"
13
+ "@graphql-tools/delegate": "9.0.32",
14
+ "@graphql-tools/wrap": "9.4.2",
15
+ "graphql-scalars": "^1.20.4"
15
16
  },
16
17
  "repository": {
17
18
  "type": "git",
@@ -19,21 +20,28 @@
19
20
  "directory": "packages/transforms/rename"
20
21
  },
21
22
  "license": "MIT",
22
- "main": "index.js",
23
- "module": "index.mjs",
24
- "typings": "index.d.ts",
23
+ "main": "cjs/index.js",
24
+ "module": "esm/index.js",
25
+ "typings": "typings/index.d.ts",
25
26
  "typescript": {
26
- "definition": "index.d.ts"
27
+ "definition": "typings/index.d.ts"
27
28
  },
29
+ "type": "module",
28
30
  "exports": {
29
31
  ".": {
30
- "require": "./index.js",
31
- "import": "./index.mjs"
32
- },
33
- "./*": {
34
- "require": "./*.js",
35
- "import": "./*.mjs"
32
+ "require": {
33
+ "types": "./typings/index.d.cts",
34
+ "default": "./cjs/index.js"
35
+ },
36
+ "import": {
37
+ "types": "./typings/index.d.ts",
38
+ "default": "./esm/index.js"
39
+ },
40
+ "default": {
41
+ "types": "./typings/index.d.ts",
42
+ "default": "./esm/index.js"
43
+ }
36
44
  },
37
45
  "./package.json": "./package.json"
38
46
  }
39
- }
47
+ }
@@ -0,0 +1,16 @@
1
+ import { GraphQLSchema } from 'graphql';
2
+ import { MeshTransform, YamlConfig } from '@graphql-mesh/types';
3
+ type RenameMapObject = Map<string | RegExp, string>;
4
+ export default class BareRename implements MeshTransform {
5
+ noWrap: boolean;
6
+ typesMap: RenameMapObject;
7
+ fieldsMap: Map<string, RenameMapObject>;
8
+ argsMap: Map<string, RenameMapObject>;
9
+ constructor({ config }: {
10
+ config: YamlConfig.RenameTransform;
11
+ });
12
+ matchInMap(map: RenameMapObject, toMatch: string): string;
13
+ renameType(type: any): import("graphql").GraphQLObjectType<any, any>;
14
+ transformSchema(schema: GraphQLSchema): GraphQLSchema;
15
+ }
16
+ export {};
@@ -1,6 +1,6 @@
1
1
  import { GraphQLSchema } from 'graphql';
2
2
  import { MeshTransform, YamlConfig } from '@graphql-mesh/types';
3
- declare type RenameMapObject = Map<string | RegExp, string>;
3
+ type RenameMapObject = Map<string | RegExp, string>;
4
4
  export default class BareRename implements MeshTransform {
5
5
  noWrap: boolean;
6
6
  typesMap: RenameMapObject;
@@ -0,0 +1,10 @@
1
+ import { YamlConfig } from '@graphql-mesh/types';
2
+ import BareRename from './bareRename.cjs';
3
+ import WrapRename from './wrapRename.cjs';
4
+ interface RenameTransformConstructor {
5
+ new (options: {
6
+ config: YamlConfig.RenameTransform;
7
+ }): BareRename | WrapRename;
8
+ }
9
+ declare const _default: RenameTransformConstructor;
10
+ export default _default;
@@ -1,6 +1,6 @@
1
1
  import { YamlConfig } from '@graphql-mesh/types';
2
- import WrapRename from './wrapRename';
3
- import BareRename from './bareRename';
2
+ import BareRename from './bareRename.js';
3
+ import WrapRename from './wrapRename.js';
4
4
  interface RenameTransformConstructor {
5
5
  new (options: {
6
6
  config: YamlConfig.RenameTransform;
@@ -0,0 +1 @@
1
+ export declare const ignoreList: string[];
@@ -0,0 +1 @@
1
+ export declare const ignoreList: string[];
@@ -0,0 +1,13 @@
1
+ import { GraphQLSchema } from 'graphql';
2
+ import { YamlConfig } from '@graphql-mesh/types';
3
+ import { DelegationContext, SubschemaConfig, Transform } from '@graphql-tools/delegate';
4
+ import { ExecutionRequest, ExecutionResult } from '@graphql-tools/utils';
5
+ export default class WrapRename implements Transform {
6
+ private transforms;
7
+ constructor({ config }: {
8
+ config: YamlConfig.RenameTransform;
9
+ });
10
+ transformSchema(originalWrappingSchema: GraphQLSchema, subschemaConfig: SubschemaConfig, transformedSchema?: GraphQLSchema): GraphQLSchema;
11
+ transformRequest(originalRequest: ExecutionRequest, delegationContext: DelegationContext, transformationContext: Record<string, any>): ExecutionRequest<any, any, any, Record<string, any>, any>;
12
+ transformResult(originalResult: ExecutionResult, delegationContext: DelegationContext, transformationContext: any): ExecutionResult<any, any>;
13
+ }
@@ -1,13 +1,13 @@
1
1
  import { GraphQLSchema } from 'graphql';
2
2
  import { YamlConfig } from '@graphql-mesh/types';
3
- import { ExecutionResult, ExecutionRequest } from '@graphql-tools/utils';
4
- import { Transform, SubschemaConfig, DelegationContext } from '@graphql-tools/delegate';
3
+ import { DelegationContext, SubschemaConfig, Transform } from '@graphql-tools/delegate';
4
+ import { ExecutionRequest, ExecutionResult } from '@graphql-tools/utils';
5
5
  export default class WrapRename implements Transform {
6
6
  private transforms;
7
7
  constructor({ config }: {
8
8
  config: YamlConfig.RenameTransform;
9
9
  });
10
10
  transformSchema(originalWrappingSchema: GraphQLSchema, subschemaConfig: SubschemaConfig, transformedSchema?: GraphQLSchema): GraphQLSchema;
11
- transformRequest(originalRequest: ExecutionRequest, delegationContext: DelegationContext, transformationContext: Record<string, any>): ExecutionRequest<Record<string, any>, any, any, Record<string, any>>;
12
- transformResult(originalResult: ExecutionResult, delegationContext: DelegationContext, transformationContext: any): ExecutionResult<Record<string, any>>;
11
+ transformRequest(originalRequest: ExecutionRequest, delegationContext: DelegationContext, transformationContext: Record<string, any>): ExecutionRequest<any, any, any, Record<string, any>, any>;
12
+ transformResult(originalResult: ExecutionResult, delegationContext: DelegationContext, transformationContext: any): ExecutionResult<any, any>;
13
13
  }
package/index.js DELETED
@@ -1,243 +0,0 @@
1
- 'use strict';
2
-
3
- const wrap = require('@graphql-tools/wrap');
4
- const utils = require('@graphql-mesh/utils');
5
- const graphql = require('graphql');
6
- const utils$1 = require('@graphql-tools/utils');
7
-
8
- const ignoreList = [
9
- 'date',
10
- 'hostname',
11
- 'regex',
12
- 'json-pointer',
13
- 'relative-json-pointer',
14
- 'uri-reference',
15
- 'uri-template',
16
- 'Date',
17
- 'Time',
18
- 'DateTime',
19
- 'Timestamp',
20
- 'TimeZone',
21
- 'UtcOffset',
22
- 'Duration',
23
- 'ISO8601Duration',
24
- 'LocalDate',
25
- 'LocalTime',
26
- 'LocalEndTime',
27
- 'EmailAddress',
28
- 'NegativeFloat',
29
- 'NegativeInt',
30
- 'NonEmptyString',
31
- 'NonNegativeFloat',
32
- 'NonNegativeInt',
33
- 'NonPositiveFloat',
34
- 'NonPositiveInt',
35
- 'PhoneNumber',
36
- 'PositiveFloat',
37
- 'PositiveInt',
38
- 'PostalCode',
39
- 'UnsignedFloat',
40
- 'UnsignedInt',
41
- 'URL',
42
- 'BigInt',
43
- 'Byte',
44
- 'Long',
45
- 'SafeInt',
46
- 'UUID',
47
- 'GUID',
48
- 'Hexadecimal',
49
- 'HexColorCode',
50
- 'HSL',
51
- 'HSLA',
52
- 'IPv4',
53
- 'IPv6',
54
- 'ISBN',
55
- 'JWT',
56
- 'Latitude',
57
- 'Longitude',
58
- 'MAC',
59
- 'Port',
60
- 'RGB',
61
- 'RGBA',
62
- 'USCurrency',
63
- 'Currency',
64
- 'JSON',
65
- 'JSONObject',
66
- 'IBAN',
67
- 'ObjectID',
68
- 'Void',
69
- 'DID',
70
- 'CountryCode',
71
- 'Locale',
72
- 'RoutingNumber',
73
- 'AccountNumber',
74
- ];
75
- class WrapRename {
76
- constructor({ config }) {
77
- this.transforms = [];
78
- for (const change of config.renames) {
79
- const { from: { type: fromTypeName, field: fromFieldName, argument: fromArgumentName }, to: { type: toTypeName, field: toFieldName, argument: toArgumentName }, useRegExpForTypes, useRegExpForFields, useRegExpForArguments, } = change;
80
- const regExpFlags = change.regExpFlags || undefined;
81
- if (fromTypeName !== toTypeName) {
82
- let replaceTypeNameFn;
83
- if (useRegExpForTypes) {
84
- const typeNameRegExp = new RegExp(fromTypeName, regExpFlags);
85
- replaceTypeNameFn = (t) => t.replace(typeNameRegExp, toTypeName);
86
- }
87
- else {
88
- replaceTypeNameFn = t => (t === fromTypeName ? toTypeName : t);
89
- }
90
- this.transforms.push(new wrap.RenameTypes(typeName => {
91
- if (ignoreList.includes(typeName)) {
92
- return typeName;
93
- }
94
- return replaceTypeNameFn(typeName);
95
- }));
96
- }
97
- if (fromFieldName && toFieldName && fromFieldName !== toFieldName) {
98
- let replaceFieldNameFn;
99
- if (useRegExpForFields) {
100
- const fieldNameRegExp = new RegExp(fromFieldName, regExpFlags);
101
- replaceFieldNameFn = (typeName, fieldName) => typeName === toTypeName ? fieldName.replace(fieldNameRegExp, toFieldName) : fieldName;
102
- }
103
- else {
104
- replaceFieldNameFn = (typeName, fieldName) => typeName === toTypeName && fieldName === fromFieldName ? toFieldName : fieldName;
105
- }
106
- this.transforms.push(new wrap.RenameObjectFields(replaceFieldNameFn));
107
- this.transforms.push(new wrap.RenameInputObjectFields(replaceFieldNameFn));
108
- }
109
- if (fromTypeName &&
110
- (fromTypeName === toTypeName || useRegExpForTypes) &&
111
- toFieldName &&
112
- (fromFieldName === toFieldName || useRegExpForFields) &&
113
- fromArgumentName &&
114
- fromArgumentName !== toArgumentName) {
115
- let replaceArgNameFn;
116
- const fieldNameMatch = (fieldName) => fieldName ===
117
- (useRegExpForFields ? fieldName.replace(new RegExp(fromFieldName, regExpFlags), toFieldName) : toFieldName);
118
- const typeNameMatch = (typeName) => typeName ===
119
- (useRegExpForTypes ? typeName.replace(new RegExp(fromTypeName, regExpFlags), toTypeName) : toTypeName);
120
- if (useRegExpForArguments) {
121
- const argNameRegExp = new RegExp(fromArgumentName, regExpFlags);
122
- replaceArgNameFn = (typeName, fieldName, argName) => typeNameMatch(typeName) && fieldNameMatch(fieldName)
123
- ? argName.replace(argNameRegExp, toArgumentName)
124
- : argName;
125
- }
126
- else {
127
- replaceArgNameFn = (typeName, fieldName, argName) => typeNameMatch(typeName) && fieldNameMatch(fieldName) && argName === fromArgumentName
128
- ? toArgumentName
129
- : argName;
130
- }
131
- this.transforms.push(new wrap.RenameObjectFieldArguments(replaceArgNameFn));
132
- }
133
- }
134
- }
135
- transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
136
- return utils.applySchemaTransforms(originalWrappingSchema, subschemaConfig, transformedSchema, this.transforms);
137
- }
138
- transformRequest(originalRequest, delegationContext, transformationContext) {
139
- return utils.applyRequestTransforms(originalRequest, delegationContext, transformationContext, this.transforms);
140
- }
141
- transformResult(originalResult, delegationContext, transformationContext) {
142
- return utils.applyResultTransforms(originalResult, delegationContext, transformationContext, this.transforms);
143
- }
144
- }
145
-
146
- // Resolver composer mapping renamed field and arguments
147
- const defaultResolverComposer = (resolveFn = graphql.defaultFieldResolver, originalFieldName, argsMap) => (root, args, context, info) => resolveFn(root,
148
- // map renamed arguments to their original value
149
- argsMap
150
- ? Object.keys(args).reduce((acc, key) => ({ ...acc, [argsMap[key] || key]: args[key] }), {})
151
- : args, context,
152
- // map renamed field name to its original value
153
- originalFieldName ? { ...info, fieldName: originalFieldName } : info);
154
- class BareRename {
155
- constructor({ config }) {
156
- this.noWrap = true;
157
- this.typesMap = new Map();
158
- this.fieldsMap = new Map();
159
- this.argsMap = new Map();
160
- for (const rename of config.renames) {
161
- const { from: { type: fromTypeName, field: fromFieldName, argument: fromArgName }, to: { type: toTypeName, field: toFieldName, argument: toArgName }, useRegExpForTypes, useRegExpForFields, } = rename;
162
- const regExpFlags = rename.regExpFlags || undefined;
163
- if (fromTypeName && !fromFieldName && toTypeName && !toFieldName && fromTypeName !== toTypeName) {
164
- this.typesMap.set(useRegExpForTypes ? new RegExp(fromTypeName, regExpFlags) : fromTypeName, toTypeName);
165
- }
166
- if (fromTypeName && fromFieldName && toTypeName && toFieldName && fromFieldName !== toFieldName) {
167
- const fromName = useRegExpForFields ? new RegExp(fromFieldName, regExpFlags) : fromFieldName;
168
- const typeMap = this.fieldsMap.get(fromTypeName) || new Map();
169
- this.fieldsMap.set(fromTypeName, typeMap.set(fromName, toFieldName));
170
- }
171
- if (fromTypeName &&
172
- fromFieldName &&
173
- fromArgName &&
174
- toTypeName &&
175
- toFieldName &&
176
- toArgName &&
177
- fromArgName !== toArgName) {
178
- const fromName = useRegExpForFields ? new RegExp(fromArgName, regExpFlags) : fromArgName;
179
- const key = `${fromTypeName}.${fromFieldName}`;
180
- const typeMap = this.argsMap.get(key) || new Map();
181
- this.argsMap.set(key, typeMap.set(fromName, toArgName));
182
- }
183
- }
184
- }
185
- matchInMap(map, toMatch) {
186
- const mapKeyIsString = map.has(toMatch);
187
- const mapKey = mapKeyIsString ? toMatch : [...map.keys()].find(key => typeof key !== 'string' && key.test(toMatch));
188
- if (!mapKey)
189
- return null;
190
- const newName = mapKeyIsString ? map.get(mapKey) : toMatch.replace(mapKey, map.get(mapKey));
191
- // avoid re-iterating over strings that have already been renamed
192
- if (mapKeyIsString)
193
- map.delete(mapKey);
194
- return newName;
195
- }
196
- renameType(type) {
197
- const newTypeName = this.matchInMap(this.typesMap, type.toString());
198
- return newTypeName ? utils$1.renameType(type, newTypeName) : undefined;
199
- }
200
- transformSchema(schema) {
201
- return utils$1.mapSchema(schema, {
202
- ...(this.typesMap.size && { [utils$1.MapperKind.TYPE]: type => this.renameType(type) }),
203
- ...(this.typesMap.size && { [utils$1.MapperKind.ROOT_OBJECT]: type => this.renameType(type) }),
204
- ...((this.fieldsMap.size || this.argsMap.size) && {
205
- [utils$1.MapperKind.COMPOSITE_FIELD]: (fieldConfig, fieldName, typeName) => {
206
- const typeRules = this.fieldsMap.get(typeName);
207
- const fieldRules = this.argsMap.get(`${typeName}.${fieldName}`);
208
- const newFieldName = typeRules && this.matchInMap(typeRules, fieldName);
209
- const argsMap = fieldRules &&
210
- Array.from(fieldRules.entries()).reduce((acc, [orName, newName]) => ({ ...acc, [newName]: orName }), {});
211
- if (!newFieldName && !fieldRules)
212
- return undefined;
213
- // Rename rules for type might have been emptied by matchInMap, in which case we can cleanup
214
- if (!(typeRules === null || typeRules === void 0 ? void 0 : typeRules.size))
215
- this.fieldsMap.delete(typeName);
216
- if (fieldRules && fieldConfig.args) {
217
- fieldConfig.args = Object.entries(fieldConfig.args).reduce((args, [argName, argConfig]) => ({
218
- ...args,
219
- [this.matchInMap(fieldRules, argName) || argName]: argConfig,
220
- }), {});
221
- }
222
- // Wrap resolve fn to handle mapping renamed field name and/or renamed arguments
223
- fieldConfig.resolve = defaultResolverComposer(fieldConfig.resolve, fieldName, argsMap);
224
- return [newFieldName || fieldName, fieldConfig];
225
- },
226
- }),
227
- });
228
- }
229
- }
230
-
231
- const RenameTransform = (function RenameTransform(options) {
232
- if (Array.isArray(options.config)) {
233
- return new WrapRename({
234
- config: {
235
- mode: 'wrap',
236
- renames: options.config,
237
- },
238
- });
239
- }
240
- return options.config.mode === 'bare' ? new BareRename(options) : new WrapRename(options);
241
- });
242
-
243
- module.exports = RenameTransform;
package/index.mjs DELETED
@@ -1,241 +0,0 @@
1
- import { RenameTypes, RenameObjectFields, RenameInputObjectFields, RenameObjectFieldArguments } from '@graphql-tools/wrap';
2
- import { applySchemaTransforms, applyRequestTransforms, applyResultTransforms } from '@graphql-mesh/utils';
3
- import { defaultFieldResolver } from 'graphql';
4
- import { renameType, mapSchema, MapperKind } from '@graphql-tools/utils';
5
-
6
- const ignoreList = [
7
- 'date',
8
- 'hostname',
9
- 'regex',
10
- 'json-pointer',
11
- 'relative-json-pointer',
12
- 'uri-reference',
13
- 'uri-template',
14
- 'Date',
15
- 'Time',
16
- 'DateTime',
17
- 'Timestamp',
18
- 'TimeZone',
19
- 'UtcOffset',
20
- 'Duration',
21
- 'ISO8601Duration',
22
- 'LocalDate',
23
- 'LocalTime',
24
- 'LocalEndTime',
25
- 'EmailAddress',
26
- 'NegativeFloat',
27
- 'NegativeInt',
28
- 'NonEmptyString',
29
- 'NonNegativeFloat',
30
- 'NonNegativeInt',
31
- 'NonPositiveFloat',
32
- 'NonPositiveInt',
33
- 'PhoneNumber',
34
- 'PositiveFloat',
35
- 'PositiveInt',
36
- 'PostalCode',
37
- 'UnsignedFloat',
38
- 'UnsignedInt',
39
- 'URL',
40
- 'BigInt',
41
- 'Byte',
42
- 'Long',
43
- 'SafeInt',
44
- 'UUID',
45
- 'GUID',
46
- 'Hexadecimal',
47
- 'HexColorCode',
48
- 'HSL',
49
- 'HSLA',
50
- 'IPv4',
51
- 'IPv6',
52
- 'ISBN',
53
- 'JWT',
54
- 'Latitude',
55
- 'Longitude',
56
- 'MAC',
57
- 'Port',
58
- 'RGB',
59
- 'RGBA',
60
- 'USCurrency',
61
- 'Currency',
62
- 'JSON',
63
- 'JSONObject',
64
- 'IBAN',
65
- 'ObjectID',
66
- 'Void',
67
- 'DID',
68
- 'CountryCode',
69
- 'Locale',
70
- 'RoutingNumber',
71
- 'AccountNumber',
72
- ];
73
- class WrapRename {
74
- constructor({ config }) {
75
- this.transforms = [];
76
- for (const change of config.renames) {
77
- const { from: { type: fromTypeName, field: fromFieldName, argument: fromArgumentName }, to: { type: toTypeName, field: toFieldName, argument: toArgumentName }, useRegExpForTypes, useRegExpForFields, useRegExpForArguments, } = change;
78
- const regExpFlags = change.regExpFlags || undefined;
79
- if (fromTypeName !== toTypeName) {
80
- let replaceTypeNameFn;
81
- if (useRegExpForTypes) {
82
- const typeNameRegExp = new RegExp(fromTypeName, regExpFlags);
83
- replaceTypeNameFn = (t) => t.replace(typeNameRegExp, toTypeName);
84
- }
85
- else {
86
- replaceTypeNameFn = t => (t === fromTypeName ? toTypeName : t);
87
- }
88
- this.transforms.push(new RenameTypes(typeName => {
89
- if (ignoreList.includes(typeName)) {
90
- return typeName;
91
- }
92
- return replaceTypeNameFn(typeName);
93
- }));
94
- }
95
- if (fromFieldName && toFieldName && fromFieldName !== toFieldName) {
96
- let replaceFieldNameFn;
97
- if (useRegExpForFields) {
98
- const fieldNameRegExp = new RegExp(fromFieldName, regExpFlags);
99
- replaceFieldNameFn = (typeName, fieldName) => typeName === toTypeName ? fieldName.replace(fieldNameRegExp, toFieldName) : fieldName;
100
- }
101
- else {
102
- replaceFieldNameFn = (typeName, fieldName) => typeName === toTypeName && fieldName === fromFieldName ? toFieldName : fieldName;
103
- }
104
- this.transforms.push(new RenameObjectFields(replaceFieldNameFn));
105
- this.transforms.push(new RenameInputObjectFields(replaceFieldNameFn));
106
- }
107
- if (fromTypeName &&
108
- (fromTypeName === toTypeName || useRegExpForTypes) &&
109
- toFieldName &&
110
- (fromFieldName === toFieldName || useRegExpForFields) &&
111
- fromArgumentName &&
112
- fromArgumentName !== toArgumentName) {
113
- let replaceArgNameFn;
114
- const fieldNameMatch = (fieldName) => fieldName ===
115
- (useRegExpForFields ? fieldName.replace(new RegExp(fromFieldName, regExpFlags), toFieldName) : toFieldName);
116
- const typeNameMatch = (typeName) => typeName ===
117
- (useRegExpForTypes ? typeName.replace(new RegExp(fromTypeName, regExpFlags), toTypeName) : toTypeName);
118
- if (useRegExpForArguments) {
119
- const argNameRegExp = new RegExp(fromArgumentName, regExpFlags);
120
- replaceArgNameFn = (typeName, fieldName, argName) => typeNameMatch(typeName) && fieldNameMatch(fieldName)
121
- ? argName.replace(argNameRegExp, toArgumentName)
122
- : argName;
123
- }
124
- else {
125
- replaceArgNameFn = (typeName, fieldName, argName) => typeNameMatch(typeName) && fieldNameMatch(fieldName) && argName === fromArgumentName
126
- ? toArgumentName
127
- : argName;
128
- }
129
- this.transforms.push(new RenameObjectFieldArguments(replaceArgNameFn));
130
- }
131
- }
132
- }
133
- transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
134
- return applySchemaTransforms(originalWrappingSchema, subschemaConfig, transformedSchema, this.transforms);
135
- }
136
- transformRequest(originalRequest, delegationContext, transformationContext) {
137
- return applyRequestTransforms(originalRequest, delegationContext, transformationContext, this.transforms);
138
- }
139
- transformResult(originalResult, delegationContext, transformationContext) {
140
- return applyResultTransforms(originalResult, delegationContext, transformationContext, this.transforms);
141
- }
142
- }
143
-
144
- // Resolver composer mapping renamed field and arguments
145
- const defaultResolverComposer = (resolveFn = defaultFieldResolver, originalFieldName, argsMap) => (root, args, context, info) => resolveFn(root,
146
- // map renamed arguments to their original value
147
- argsMap
148
- ? Object.keys(args).reduce((acc, key) => ({ ...acc, [argsMap[key] || key]: args[key] }), {})
149
- : args, context,
150
- // map renamed field name to its original value
151
- originalFieldName ? { ...info, fieldName: originalFieldName } : info);
152
- class BareRename {
153
- constructor({ config }) {
154
- this.noWrap = true;
155
- this.typesMap = new Map();
156
- this.fieldsMap = new Map();
157
- this.argsMap = new Map();
158
- for (const rename of config.renames) {
159
- const { from: { type: fromTypeName, field: fromFieldName, argument: fromArgName }, to: { type: toTypeName, field: toFieldName, argument: toArgName }, useRegExpForTypes, useRegExpForFields, } = rename;
160
- const regExpFlags = rename.regExpFlags || undefined;
161
- if (fromTypeName && !fromFieldName && toTypeName && !toFieldName && fromTypeName !== toTypeName) {
162
- this.typesMap.set(useRegExpForTypes ? new RegExp(fromTypeName, regExpFlags) : fromTypeName, toTypeName);
163
- }
164
- if (fromTypeName && fromFieldName && toTypeName && toFieldName && fromFieldName !== toFieldName) {
165
- const fromName = useRegExpForFields ? new RegExp(fromFieldName, regExpFlags) : fromFieldName;
166
- const typeMap = this.fieldsMap.get(fromTypeName) || new Map();
167
- this.fieldsMap.set(fromTypeName, typeMap.set(fromName, toFieldName));
168
- }
169
- if (fromTypeName &&
170
- fromFieldName &&
171
- fromArgName &&
172
- toTypeName &&
173
- toFieldName &&
174
- toArgName &&
175
- fromArgName !== toArgName) {
176
- const fromName = useRegExpForFields ? new RegExp(fromArgName, regExpFlags) : fromArgName;
177
- const key = `${fromTypeName}.${fromFieldName}`;
178
- const typeMap = this.argsMap.get(key) || new Map();
179
- this.argsMap.set(key, typeMap.set(fromName, toArgName));
180
- }
181
- }
182
- }
183
- matchInMap(map, toMatch) {
184
- const mapKeyIsString = map.has(toMatch);
185
- const mapKey = mapKeyIsString ? toMatch : [...map.keys()].find(key => typeof key !== 'string' && key.test(toMatch));
186
- if (!mapKey)
187
- return null;
188
- const newName = mapKeyIsString ? map.get(mapKey) : toMatch.replace(mapKey, map.get(mapKey));
189
- // avoid re-iterating over strings that have already been renamed
190
- if (mapKeyIsString)
191
- map.delete(mapKey);
192
- return newName;
193
- }
194
- renameType(type) {
195
- const newTypeName = this.matchInMap(this.typesMap, type.toString());
196
- return newTypeName ? renameType(type, newTypeName) : undefined;
197
- }
198
- transformSchema(schema) {
199
- return mapSchema(schema, {
200
- ...(this.typesMap.size && { [MapperKind.TYPE]: type => this.renameType(type) }),
201
- ...(this.typesMap.size && { [MapperKind.ROOT_OBJECT]: type => this.renameType(type) }),
202
- ...((this.fieldsMap.size || this.argsMap.size) && {
203
- [MapperKind.COMPOSITE_FIELD]: (fieldConfig, fieldName, typeName) => {
204
- const typeRules = this.fieldsMap.get(typeName);
205
- const fieldRules = this.argsMap.get(`${typeName}.${fieldName}`);
206
- const newFieldName = typeRules && this.matchInMap(typeRules, fieldName);
207
- const argsMap = fieldRules &&
208
- Array.from(fieldRules.entries()).reduce((acc, [orName, newName]) => ({ ...acc, [newName]: orName }), {});
209
- if (!newFieldName && !fieldRules)
210
- return undefined;
211
- // Rename rules for type might have been emptied by matchInMap, in which case we can cleanup
212
- if (!(typeRules === null || typeRules === void 0 ? void 0 : typeRules.size))
213
- this.fieldsMap.delete(typeName);
214
- if (fieldRules && fieldConfig.args) {
215
- fieldConfig.args = Object.entries(fieldConfig.args).reduce((args, [argName, argConfig]) => ({
216
- ...args,
217
- [this.matchInMap(fieldRules, argName) || argName]: argConfig,
218
- }), {});
219
- }
220
- // Wrap resolve fn to handle mapping renamed field name and/or renamed arguments
221
- fieldConfig.resolve = defaultResolverComposer(fieldConfig.resolve, fieldName, argsMap);
222
- return [newFieldName || fieldName, fieldConfig];
223
- },
224
- }),
225
- });
226
- }
227
- }
228
-
229
- const RenameTransform = (function RenameTransform(options) {
230
- if (Array.isArray(options.config)) {
231
- return new WrapRename({
232
- config: {
233
- mode: 'wrap',
234
- renames: options.config,
235
- },
236
- });
237
- }
238
- return options.config.mode === 'bare' ? new BareRename(options) : new WrapRename(options);
239
- });
240
-
241
- export default RenameTransform;