@graphql-mesh/transform-naming-convention 1.0.0-alpha-3fc47d119.0 → 1.0.0-alpha-20230420181317-a95037648
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/bareNamingConvention.js +192 -0
- package/cjs/index.js +10 -0
- package/cjs/package.json +1 -0
- package/cjs/shared.js +41 -0
- package/cjs/wrapNamingConvention.js +50 -0
- package/esm/bareNamingConvention.js +189 -0
- package/esm/index.js +7 -0
- package/esm/shared.js +38 -0
- package/{index.mjs → esm/wrapNamingConvention.js} +11 -46
- package/package.json +27 -20
- package/typings/bareNamingConvention.d.cts +9 -0
- package/typings/bareNamingConvention.d.ts +9 -0
- package/typings/index.d.cts +8 -0
- package/typings/index.d.ts +8 -0
- package/typings/shared.d.cts +7 -0
- package/typings/shared.d.ts +7 -0
- package/{index.d.ts → typings/wrapNamingConvention.d.cts} +5 -5
- package/typings/wrapNamingConvention.d.ts +11 -0
- package/index.js +0 -84
|
@@ -0,0 +1,192 @@
|
|
|
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
|
+
const isObject = (input) => typeof input === 'object' && input !== null && !Array.isArray(input) && true;
|
|
7
|
+
const getUnderlyingType = (type) => type.ofType
|
|
8
|
+
? getUnderlyingType(type.ofType)
|
|
9
|
+
: type;
|
|
10
|
+
// Resolver composer mapping renamed field and arguments
|
|
11
|
+
const defaultResolverComposer = (resolveFn = graphql_1.defaultFieldResolver, originalFieldName, argsMap, resultMap) => (root, args, context, info) => {
|
|
12
|
+
const originalResult = resolveFn(root,
|
|
13
|
+
// map renamed arguments to their original value
|
|
14
|
+
argsMap ? argsFromArgMap(argsMap, args) : args, context,
|
|
15
|
+
// map renamed field name to its original value
|
|
16
|
+
originalFieldName ? { ...info, fieldName: originalFieldName } : info);
|
|
17
|
+
// map result values from original value to new renamed value
|
|
18
|
+
return resultMap
|
|
19
|
+
? Array.isArray(originalResult)
|
|
20
|
+
? originalResult.map(result => resultMap[result] || originalResult)
|
|
21
|
+
: resultMap[originalResult] || originalResult
|
|
22
|
+
: originalResult;
|
|
23
|
+
};
|
|
24
|
+
class NamingConventionTransform {
|
|
25
|
+
constructor(options) {
|
|
26
|
+
this.noWrap = true;
|
|
27
|
+
this.config = { ...options.config };
|
|
28
|
+
}
|
|
29
|
+
transformSchema(schema) {
|
|
30
|
+
return (0, utils_1.mapSchema)(schema, {
|
|
31
|
+
...(this.config.typeNames && {
|
|
32
|
+
[utils_1.MapperKind.TYPE]: type => {
|
|
33
|
+
const oldName = type.name;
|
|
34
|
+
const namingConventionFn = shared_js_1.NAMING_CONVENTIONS[this.config.typeNames];
|
|
35
|
+
const newName = shared_js_1.IGNORED_TYPE_NAMES.includes(oldName)
|
|
36
|
+
? oldName
|
|
37
|
+
: namingConventionFn(oldName);
|
|
38
|
+
if (newName !== undefined && newName !== oldName) {
|
|
39
|
+
return (0, utils_1.renameType)(type, newName);
|
|
40
|
+
}
|
|
41
|
+
return undefined;
|
|
42
|
+
},
|
|
43
|
+
[utils_1.MapperKind.ABSTRACT_TYPE]: type => {
|
|
44
|
+
const currentName = type.name;
|
|
45
|
+
const existingResolver = type.resolveType;
|
|
46
|
+
const namingConventionFn = shared_js_1.NAMING_CONVENTIONS[this.config.typeNames];
|
|
47
|
+
const newName = shared_js_1.IGNORED_TYPE_NAMES.includes(currentName)
|
|
48
|
+
? currentName
|
|
49
|
+
: namingConventionFn(currentName);
|
|
50
|
+
type.resolveType = async (data, context, info, abstractType) => {
|
|
51
|
+
const originalResolvedTypename = await existingResolver(data, context, info, abstractType);
|
|
52
|
+
return shared_js_1.IGNORED_TYPE_NAMES.includes(originalResolvedTypename)
|
|
53
|
+
? originalResolvedTypename
|
|
54
|
+
: namingConventionFn(originalResolvedTypename);
|
|
55
|
+
};
|
|
56
|
+
if (newName !== undefined && newName !== currentName) {
|
|
57
|
+
return (0, utils_1.renameType)(type, newName);
|
|
58
|
+
}
|
|
59
|
+
return undefined;
|
|
60
|
+
},
|
|
61
|
+
}),
|
|
62
|
+
...(this.config.enumValues && {
|
|
63
|
+
[utils_1.MapperKind.ENUM_VALUE]: (valueConfig, _typeName, _schema, externalValue) => {
|
|
64
|
+
const namingConventionFn = shared_js_1.NAMING_CONVENTIONS[this.config.enumValues];
|
|
65
|
+
const newEnumValue = namingConventionFn(externalValue);
|
|
66
|
+
if (newEnumValue === externalValue) {
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
return [
|
|
70
|
+
newEnumValue,
|
|
71
|
+
{
|
|
72
|
+
...valueConfig,
|
|
73
|
+
value: newEnumValue,
|
|
74
|
+
astNode: {
|
|
75
|
+
...valueConfig.astNode,
|
|
76
|
+
name: {
|
|
77
|
+
...valueConfig.astNode.name,
|
|
78
|
+
value: newEnumValue,
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
];
|
|
83
|
+
},
|
|
84
|
+
}),
|
|
85
|
+
...((this.config.fieldNames || this.config.fieldArgumentNames) && {
|
|
86
|
+
[utils_1.MapperKind.COMPOSITE_FIELD]: (fieldConfig, fieldName) => {
|
|
87
|
+
const enumNamingConventionFn = shared_js_1.NAMING_CONVENTIONS[this.config.enumValues];
|
|
88
|
+
const fieldNamingConventionFn = this.config.fieldNames && shared_js_1.NAMING_CONVENTIONS[this.config.fieldNames];
|
|
89
|
+
const argNamingConventionFn = this.config.fieldArgumentNames && shared_js_1.NAMING_CONVENTIONS[this.config.fieldArgumentNames];
|
|
90
|
+
const argsMap = fieldConfig.args && {};
|
|
91
|
+
const newFieldName = this.config.fieldNames &&
|
|
92
|
+
!shared_js_1.IGNORED_ROOT_FIELD_NAMES.includes(fieldName) &&
|
|
93
|
+
fieldNamingConventionFn(fieldName);
|
|
94
|
+
const fieldActualType = getUnderlyingType(fieldConfig.type);
|
|
95
|
+
const resultMap = this.config.enumValues &&
|
|
96
|
+
(0, graphql_1.isEnumType)(fieldActualType) &&
|
|
97
|
+
Object.keys(fieldActualType.toConfig().values).reduce((map, value) => {
|
|
98
|
+
if (Number.isFinite(value)) {
|
|
99
|
+
return map;
|
|
100
|
+
}
|
|
101
|
+
const newValue = enumNamingConventionFn(value);
|
|
102
|
+
return newValue === value
|
|
103
|
+
? map
|
|
104
|
+
: {
|
|
105
|
+
...map,
|
|
106
|
+
[value]: newValue,
|
|
107
|
+
};
|
|
108
|
+
}, {});
|
|
109
|
+
if (fieldConfig.args) {
|
|
110
|
+
fieldConfig.args = Object.entries(fieldConfig.args).reduce((args, [argName, argConfig]) => {
|
|
111
|
+
const newArgName = this.config.fieldArgumentNames && argNamingConventionFn(argName);
|
|
112
|
+
const useArgName = newArgName || argName;
|
|
113
|
+
const argIsInputObjectType = (0, graphql_1.isInputObjectType)(argConfig.type);
|
|
114
|
+
if (argIsInputObjectType) {
|
|
115
|
+
argsMap[useArgName] = {
|
|
116
|
+
originalName: argName,
|
|
117
|
+
fields: generateArgsMapForInput(argConfig.type, fieldNamingConventionFn),
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
else if (argName !== useArgName) {
|
|
121
|
+
argsMap[useArgName] = argName;
|
|
122
|
+
}
|
|
123
|
+
return {
|
|
124
|
+
...args,
|
|
125
|
+
[useArgName]: argConfig,
|
|
126
|
+
};
|
|
127
|
+
}, {});
|
|
128
|
+
}
|
|
129
|
+
// Wrap resolve fn to handle mapping renamed field and argument names as well as results (for enums)
|
|
130
|
+
fieldConfig.resolve = defaultResolverComposer(fieldConfig.resolve, fieldName, argsMap, resultMap);
|
|
131
|
+
return [newFieldName || fieldName, fieldConfig];
|
|
132
|
+
},
|
|
133
|
+
}),
|
|
134
|
+
...(this.config.fieldNames && {
|
|
135
|
+
[utils_1.MapperKind.INPUT_OBJECT_FIELD]: (inputFieldConfig, fieldName) => {
|
|
136
|
+
const namingConventionFn = this.config.fieldNames && shared_js_1.NAMING_CONVENTIONS[this.config.fieldNames];
|
|
137
|
+
const newName = namingConventionFn(fieldName);
|
|
138
|
+
if (newName === fieldName) {
|
|
139
|
+
return undefined;
|
|
140
|
+
}
|
|
141
|
+
return [newName, inputFieldConfig];
|
|
142
|
+
},
|
|
143
|
+
}),
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
exports.default = NamingConventionTransform;
|
|
148
|
+
function generateArgsMapForInput(input, fieldNamingConventionFn) {
|
|
149
|
+
const inputConfig = input.toConfig();
|
|
150
|
+
const inputFields = inputConfig.fields;
|
|
151
|
+
const argsMap = {};
|
|
152
|
+
Object.keys(inputFields).forEach(argName => {
|
|
153
|
+
if (typeof argName === 'number')
|
|
154
|
+
return;
|
|
155
|
+
const newArgName = fieldNamingConventionFn ? fieldNamingConventionFn(argName) : argName;
|
|
156
|
+
const argConfig = inputFields[argName];
|
|
157
|
+
// Unwind any list / nulls etc
|
|
158
|
+
const type = (0, graphql_1.getNamedType)(argConfig.type);
|
|
159
|
+
const argIsInputObjectType = (0, graphql_1.isInputObjectType)(type);
|
|
160
|
+
if (argIsInputObjectType) {
|
|
161
|
+
argsMap[newArgName] = {
|
|
162
|
+
originalName: argName,
|
|
163
|
+
fields: generateArgsMapForInput(type, fieldNamingConventionFn),
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
argsMap[newArgName] = argName;
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
return argsMap;
|
|
171
|
+
}
|
|
172
|
+
// Map back from new arg name to the original one
|
|
173
|
+
function argsFromArgMap(argMap, args) {
|
|
174
|
+
const originalArgs = {};
|
|
175
|
+
Object.keys(args).forEach(newArgName => {
|
|
176
|
+
if (typeof newArgName !== 'string')
|
|
177
|
+
return;
|
|
178
|
+
const argMapVal = argMap[newArgName];
|
|
179
|
+
const originalArgName = typeof argMapVal === 'string' ? argMapVal : argMapVal.originalName;
|
|
180
|
+
const val = args[newArgName];
|
|
181
|
+
if (Array.isArray(val) && typeof argMapVal !== 'string') {
|
|
182
|
+
originalArgs[originalArgName] = val.map(v => isObject(v) ? argsFromArgMap(argMapVal.fields, v) : v);
|
|
183
|
+
}
|
|
184
|
+
else if (isObject(val) && typeof argMapVal !== 'string') {
|
|
185
|
+
originalArgs[originalArgName] = argsFromArgMap(argMapVal.fields, val);
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
originalArgs[originalArgName] = val;
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
return originalArgs;
|
|
192
|
+
}
|
package/cjs/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
const bareNamingConvention_js_1 = tslib_1.__importDefault(require("./bareNamingConvention.js"));
|
|
5
|
+
const wrapNamingConvention_js_1 = tslib_1.__importDefault(require("./wrapNamingConvention.js"));
|
|
6
|
+
exports.default = (function NamingConventionTransform(options) {
|
|
7
|
+
return options.config.mode === 'bare'
|
|
8
|
+
? new bareNamingConvention_js_1.default(options)
|
|
9
|
+
: new wrapNamingConvention_js_1.default(options);
|
|
10
|
+
});
|
package/cjs/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
package/cjs/shared.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.IGNORED_TYPE_NAMES = exports.IGNORED_ROOT_FIELD_NAMES = exports.NAMING_CONVENTIONS = void 0;
|
|
4
|
+
const change_case_1 = require("change-case");
|
|
5
|
+
const graphql_scalars_1 = require("graphql-scalars");
|
|
6
|
+
const lower_case_1 = require("lower-case");
|
|
7
|
+
const upper_case_1 = require("upper-case");
|
|
8
|
+
exports.NAMING_CONVENTIONS = {
|
|
9
|
+
camelCase: change_case_1.camelCase,
|
|
10
|
+
capitalCase: change_case_1.capitalCase,
|
|
11
|
+
constantCase: change_case_1.constantCase,
|
|
12
|
+
dotCase: change_case_1.dotCase,
|
|
13
|
+
headerCase: change_case_1.headerCase,
|
|
14
|
+
noCase: change_case_1.noCase,
|
|
15
|
+
paramCase: change_case_1.paramCase,
|
|
16
|
+
pascalCase: change_case_1.pascalCase,
|
|
17
|
+
pathCase: change_case_1.pathCase,
|
|
18
|
+
sentenceCase: change_case_1.sentenceCase,
|
|
19
|
+
snakeCase: change_case_1.snakeCase,
|
|
20
|
+
upperCase: upper_case_1.upperCase,
|
|
21
|
+
lowerCase: lower_case_1.lowerCase,
|
|
22
|
+
};
|
|
23
|
+
// Ignore fields needed by Federation spec
|
|
24
|
+
exports.IGNORED_ROOT_FIELD_NAMES = ['_service', '_entities'];
|
|
25
|
+
exports.IGNORED_TYPE_NAMES = [
|
|
26
|
+
'Int',
|
|
27
|
+
'Float',
|
|
28
|
+
'String',
|
|
29
|
+
'Boolean',
|
|
30
|
+
'ID',
|
|
31
|
+
'date',
|
|
32
|
+
'hostname',
|
|
33
|
+
'regex',
|
|
34
|
+
'json-pointer',
|
|
35
|
+
'relative-json-pointer',
|
|
36
|
+
'uri-reference',
|
|
37
|
+
'uri-template',
|
|
38
|
+
'ObjMap',
|
|
39
|
+
'HttpMethod',
|
|
40
|
+
...Object.keys(graphql_scalars_1.resolvers),
|
|
41
|
+
];
|
|
@@ -0,0 +1,50 @@
|
|
|
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 NamingConventionTransform {
|
|
7
|
+
constructor(options) {
|
|
8
|
+
this.transforms = [];
|
|
9
|
+
if (options.config.typeNames) {
|
|
10
|
+
const namingConventionFn = shared_js_1.NAMING_CONVENTIONS[options.config.typeNames];
|
|
11
|
+
this.transforms.push(new wrap_1.RenameTypes(typeName => shared_js_1.IGNORED_TYPE_NAMES.includes(typeName)
|
|
12
|
+
? typeName
|
|
13
|
+
: namingConventionFn(typeName) || typeName));
|
|
14
|
+
}
|
|
15
|
+
if (options.config.fieldNames) {
|
|
16
|
+
const fieldNamingConventionFn = options.config.fieldNames
|
|
17
|
+
? shared_js_1.NAMING_CONVENTIONS[options.config.fieldNames]
|
|
18
|
+
: (s) => s;
|
|
19
|
+
this.transforms.push(new wrap_1.RenameInputObjectFields((_, fieldName) => fieldNamingConventionFn(fieldName) || fieldName), new wrap_1.TransformObjectFields((_, fieldName, fieldConfig) => [
|
|
20
|
+
shared_js_1.IGNORED_ROOT_FIELD_NAMES.includes(fieldName)
|
|
21
|
+
? fieldName
|
|
22
|
+
: fieldNamingConventionFn(fieldName) || fieldName,
|
|
23
|
+
fieldConfig,
|
|
24
|
+
]), new wrap_1.RenameInterfaceFields((_, fieldName) => fieldNamingConventionFn(fieldName) || fieldName));
|
|
25
|
+
}
|
|
26
|
+
if (options.config.fieldArgumentNames) {
|
|
27
|
+
const fieldArgNamingConventionFn = options.config.fieldArgumentNames
|
|
28
|
+
? shared_js_1.NAMING_CONVENTIONS[options.config.fieldArgumentNames]
|
|
29
|
+
: (s) => s;
|
|
30
|
+
this.transforms.push(new wrap_1.RenameObjectFieldArguments((_typeName, _fieldName, argName) => fieldArgNamingConventionFn(argName)));
|
|
31
|
+
}
|
|
32
|
+
if (options.config.enumValues) {
|
|
33
|
+
const namingConventionFn = shared_js_1.NAMING_CONVENTIONS[options.config.enumValues];
|
|
34
|
+
this.transforms.push(new wrap_1.TransformEnumValues((typeName, externalValue, enumValueConfig) => {
|
|
35
|
+
const newEnumValue = namingConventionFn(externalValue) || externalValue;
|
|
36
|
+
return [newEnumValue, enumValueConfig];
|
|
37
|
+
}));
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
|
41
|
+
return (0, utils_1.applySchemaTransforms)(originalWrappingSchema, subschemaConfig, transformedSchema, this.transforms);
|
|
42
|
+
}
|
|
43
|
+
transformRequest(originalRequest, delegationContext, transformationContext) {
|
|
44
|
+
return (0, utils_1.applyRequestTransforms)(originalRequest, delegationContext, transformationContext, this.transforms);
|
|
45
|
+
}
|
|
46
|
+
transformResult(originalResult, delegationContext, transformationContext) {
|
|
47
|
+
return (0, utils_1.applyResultTransforms)(originalResult, delegationContext, transformationContext, this.transforms);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
exports.default = NamingConventionTransform;
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { defaultFieldResolver, getNamedType, isEnumType, isInputObjectType, } from 'graphql';
|
|
2
|
+
import { MapperKind, mapSchema, renameType } from '@graphql-tools/utils';
|
|
3
|
+
import { IGNORED_ROOT_FIELD_NAMES, IGNORED_TYPE_NAMES, NAMING_CONVENTIONS } from './shared.js';
|
|
4
|
+
const isObject = (input) => typeof input === 'object' && input !== null && !Array.isArray(input) && true;
|
|
5
|
+
const getUnderlyingType = (type) => type.ofType
|
|
6
|
+
? getUnderlyingType(type.ofType)
|
|
7
|
+
: type;
|
|
8
|
+
// Resolver composer mapping renamed field and arguments
|
|
9
|
+
const defaultResolverComposer = (resolveFn = defaultFieldResolver, originalFieldName, argsMap, resultMap) => (root, args, context, info) => {
|
|
10
|
+
const originalResult = resolveFn(root,
|
|
11
|
+
// map renamed arguments to their original value
|
|
12
|
+
argsMap ? argsFromArgMap(argsMap, args) : args, context,
|
|
13
|
+
// map renamed field name to its original value
|
|
14
|
+
originalFieldName ? { ...info, fieldName: originalFieldName } : info);
|
|
15
|
+
// map result values from original value to new renamed value
|
|
16
|
+
return resultMap
|
|
17
|
+
? Array.isArray(originalResult)
|
|
18
|
+
? originalResult.map(result => resultMap[result] || originalResult)
|
|
19
|
+
: resultMap[originalResult] || originalResult
|
|
20
|
+
: originalResult;
|
|
21
|
+
};
|
|
22
|
+
export default class NamingConventionTransform {
|
|
23
|
+
constructor(options) {
|
|
24
|
+
this.noWrap = true;
|
|
25
|
+
this.config = { ...options.config };
|
|
26
|
+
}
|
|
27
|
+
transformSchema(schema) {
|
|
28
|
+
return mapSchema(schema, {
|
|
29
|
+
...(this.config.typeNames && {
|
|
30
|
+
[MapperKind.TYPE]: type => {
|
|
31
|
+
const oldName = type.name;
|
|
32
|
+
const namingConventionFn = NAMING_CONVENTIONS[this.config.typeNames];
|
|
33
|
+
const newName = IGNORED_TYPE_NAMES.includes(oldName)
|
|
34
|
+
? oldName
|
|
35
|
+
: namingConventionFn(oldName);
|
|
36
|
+
if (newName !== undefined && newName !== oldName) {
|
|
37
|
+
return renameType(type, newName);
|
|
38
|
+
}
|
|
39
|
+
return undefined;
|
|
40
|
+
},
|
|
41
|
+
[MapperKind.ABSTRACT_TYPE]: type => {
|
|
42
|
+
const currentName = type.name;
|
|
43
|
+
const existingResolver = type.resolveType;
|
|
44
|
+
const namingConventionFn = NAMING_CONVENTIONS[this.config.typeNames];
|
|
45
|
+
const newName = IGNORED_TYPE_NAMES.includes(currentName)
|
|
46
|
+
? currentName
|
|
47
|
+
: namingConventionFn(currentName);
|
|
48
|
+
type.resolveType = async (data, context, info, abstractType) => {
|
|
49
|
+
const originalResolvedTypename = await existingResolver(data, context, info, abstractType);
|
|
50
|
+
return IGNORED_TYPE_NAMES.includes(originalResolvedTypename)
|
|
51
|
+
? originalResolvedTypename
|
|
52
|
+
: namingConventionFn(originalResolvedTypename);
|
|
53
|
+
};
|
|
54
|
+
if (newName !== undefined && newName !== currentName) {
|
|
55
|
+
return renameType(type, newName);
|
|
56
|
+
}
|
|
57
|
+
return undefined;
|
|
58
|
+
},
|
|
59
|
+
}),
|
|
60
|
+
...(this.config.enumValues && {
|
|
61
|
+
[MapperKind.ENUM_VALUE]: (valueConfig, _typeName, _schema, externalValue) => {
|
|
62
|
+
const namingConventionFn = NAMING_CONVENTIONS[this.config.enumValues];
|
|
63
|
+
const newEnumValue = namingConventionFn(externalValue);
|
|
64
|
+
if (newEnumValue === externalValue) {
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
return [
|
|
68
|
+
newEnumValue,
|
|
69
|
+
{
|
|
70
|
+
...valueConfig,
|
|
71
|
+
value: newEnumValue,
|
|
72
|
+
astNode: {
|
|
73
|
+
...valueConfig.astNode,
|
|
74
|
+
name: {
|
|
75
|
+
...valueConfig.astNode.name,
|
|
76
|
+
value: newEnumValue,
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
];
|
|
81
|
+
},
|
|
82
|
+
}),
|
|
83
|
+
...((this.config.fieldNames || this.config.fieldArgumentNames) && {
|
|
84
|
+
[MapperKind.COMPOSITE_FIELD]: (fieldConfig, fieldName) => {
|
|
85
|
+
const enumNamingConventionFn = NAMING_CONVENTIONS[this.config.enumValues];
|
|
86
|
+
const fieldNamingConventionFn = this.config.fieldNames && NAMING_CONVENTIONS[this.config.fieldNames];
|
|
87
|
+
const argNamingConventionFn = this.config.fieldArgumentNames && NAMING_CONVENTIONS[this.config.fieldArgumentNames];
|
|
88
|
+
const argsMap = fieldConfig.args && {};
|
|
89
|
+
const newFieldName = this.config.fieldNames &&
|
|
90
|
+
!IGNORED_ROOT_FIELD_NAMES.includes(fieldName) &&
|
|
91
|
+
fieldNamingConventionFn(fieldName);
|
|
92
|
+
const fieldActualType = getUnderlyingType(fieldConfig.type);
|
|
93
|
+
const resultMap = this.config.enumValues &&
|
|
94
|
+
isEnumType(fieldActualType) &&
|
|
95
|
+
Object.keys(fieldActualType.toConfig().values).reduce((map, value) => {
|
|
96
|
+
if (Number.isFinite(value)) {
|
|
97
|
+
return map;
|
|
98
|
+
}
|
|
99
|
+
const newValue = enumNamingConventionFn(value);
|
|
100
|
+
return newValue === value
|
|
101
|
+
? map
|
|
102
|
+
: {
|
|
103
|
+
...map,
|
|
104
|
+
[value]: newValue,
|
|
105
|
+
};
|
|
106
|
+
}, {});
|
|
107
|
+
if (fieldConfig.args) {
|
|
108
|
+
fieldConfig.args = Object.entries(fieldConfig.args).reduce((args, [argName, argConfig]) => {
|
|
109
|
+
const newArgName = this.config.fieldArgumentNames && argNamingConventionFn(argName);
|
|
110
|
+
const useArgName = newArgName || argName;
|
|
111
|
+
const argIsInputObjectType = isInputObjectType(argConfig.type);
|
|
112
|
+
if (argIsInputObjectType) {
|
|
113
|
+
argsMap[useArgName] = {
|
|
114
|
+
originalName: argName,
|
|
115
|
+
fields: generateArgsMapForInput(argConfig.type, fieldNamingConventionFn),
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
else if (argName !== useArgName) {
|
|
119
|
+
argsMap[useArgName] = argName;
|
|
120
|
+
}
|
|
121
|
+
return {
|
|
122
|
+
...args,
|
|
123
|
+
[useArgName]: argConfig,
|
|
124
|
+
};
|
|
125
|
+
}, {});
|
|
126
|
+
}
|
|
127
|
+
// Wrap resolve fn to handle mapping renamed field and argument names as well as results (for enums)
|
|
128
|
+
fieldConfig.resolve = defaultResolverComposer(fieldConfig.resolve, fieldName, argsMap, resultMap);
|
|
129
|
+
return [newFieldName || fieldName, fieldConfig];
|
|
130
|
+
},
|
|
131
|
+
}),
|
|
132
|
+
...(this.config.fieldNames && {
|
|
133
|
+
[MapperKind.INPUT_OBJECT_FIELD]: (inputFieldConfig, fieldName) => {
|
|
134
|
+
const namingConventionFn = this.config.fieldNames && NAMING_CONVENTIONS[this.config.fieldNames];
|
|
135
|
+
const newName = namingConventionFn(fieldName);
|
|
136
|
+
if (newName === fieldName) {
|
|
137
|
+
return undefined;
|
|
138
|
+
}
|
|
139
|
+
return [newName, inputFieldConfig];
|
|
140
|
+
},
|
|
141
|
+
}),
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
function generateArgsMapForInput(input, fieldNamingConventionFn) {
|
|
146
|
+
const inputConfig = input.toConfig();
|
|
147
|
+
const inputFields = inputConfig.fields;
|
|
148
|
+
const argsMap = {};
|
|
149
|
+
Object.keys(inputFields).forEach(argName => {
|
|
150
|
+
if (typeof argName === 'number')
|
|
151
|
+
return;
|
|
152
|
+
const newArgName = fieldNamingConventionFn ? fieldNamingConventionFn(argName) : argName;
|
|
153
|
+
const argConfig = inputFields[argName];
|
|
154
|
+
// Unwind any list / nulls etc
|
|
155
|
+
const type = getNamedType(argConfig.type);
|
|
156
|
+
const argIsInputObjectType = isInputObjectType(type);
|
|
157
|
+
if (argIsInputObjectType) {
|
|
158
|
+
argsMap[newArgName] = {
|
|
159
|
+
originalName: argName,
|
|
160
|
+
fields: generateArgsMapForInput(type, fieldNamingConventionFn),
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
argsMap[newArgName] = argName;
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
return argsMap;
|
|
168
|
+
}
|
|
169
|
+
// Map back from new arg name to the original one
|
|
170
|
+
function argsFromArgMap(argMap, args) {
|
|
171
|
+
const originalArgs = {};
|
|
172
|
+
Object.keys(args).forEach(newArgName => {
|
|
173
|
+
if (typeof newArgName !== 'string')
|
|
174
|
+
return;
|
|
175
|
+
const argMapVal = argMap[newArgName];
|
|
176
|
+
const originalArgName = typeof argMapVal === 'string' ? argMapVal : argMapVal.originalName;
|
|
177
|
+
const val = args[newArgName];
|
|
178
|
+
if (Array.isArray(val) && typeof argMapVal !== 'string') {
|
|
179
|
+
originalArgs[originalArgName] = val.map(v => isObject(v) ? argsFromArgMap(argMapVal.fields, v) : v);
|
|
180
|
+
}
|
|
181
|
+
else if (isObject(val) && typeof argMapVal !== 'string') {
|
|
182
|
+
originalArgs[originalArgName] = argsFromArgMap(argMapVal.fields, val);
|
|
183
|
+
}
|
|
184
|
+
else {
|
|
185
|
+
originalArgs[originalArgName] = val;
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
return originalArgs;
|
|
189
|
+
}
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import BareNamingConvention from './bareNamingConvention.js';
|
|
2
|
+
import WrapNamingConvention from './wrapNamingConvention.js';
|
|
3
|
+
export default (function NamingConventionTransform(options) {
|
|
4
|
+
return options.config.mode === 'bare'
|
|
5
|
+
? new BareNamingConvention(options)
|
|
6
|
+
: new WrapNamingConvention(options);
|
|
7
|
+
});
|
package/esm/shared.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { camelCase, capitalCase, constantCase, dotCase, headerCase, noCase, paramCase, pascalCase, pathCase, sentenceCase, snakeCase, } from 'change-case';
|
|
2
|
+
import { resolvers as scalarsResolversMap } from 'graphql-scalars';
|
|
3
|
+
import { lowerCase } from 'lower-case';
|
|
4
|
+
import { upperCase } from 'upper-case';
|
|
5
|
+
export const NAMING_CONVENTIONS = {
|
|
6
|
+
camelCase,
|
|
7
|
+
capitalCase,
|
|
8
|
+
constantCase,
|
|
9
|
+
dotCase,
|
|
10
|
+
headerCase,
|
|
11
|
+
noCase,
|
|
12
|
+
paramCase,
|
|
13
|
+
pascalCase,
|
|
14
|
+
pathCase,
|
|
15
|
+
sentenceCase,
|
|
16
|
+
snakeCase,
|
|
17
|
+
upperCase,
|
|
18
|
+
lowerCase,
|
|
19
|
+
};
|
|
20
|
+
// Ignore fields needed by Federation spec
|
|
21
|
+
export const IGNORED_ROOT_FIELD_NAMES = ['_service', '_entities'];
|
|
22
|
+
export const IGNORED_TYPE_NAMES = [
|
|
23
|
+
'Int',
|
|
24
|
+
'Float',
|
|
25
|
+
'String',
|
|
26
|
+
'Boolean',
|
|
27
|
+
'ID',
|
|
28
|
+
'date',
|
|
29
|
+
'hostname',
|
|
30
|
+
'regex',
|
|
31
|
+
'json-pointer',
|
|
32
|
+
'relative-json-pointer',
|
|
33
|
+
'uri-reference',
|
|
34
|
+
'uri-template',
|
|
35
|
+
'ObjMap',
|
|
36
|
+
'HttpMethod',
|
|
37
|
+
...Object.keys(scalarsResolversMap),
|
|
38
|
+
];
|
|
@@ -1,50 +1,23 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
import { lowerCase } from 'lower-case';
|
|
6
|
-
import { resolvers } from 'graphql-scalars';
|
|
7
|
-
|
|
8
|
-
const NAMING_CONVENTIONS = {
|
|
9
|
-
camelCase,
|
|
10
|
-
capitalCase,
|
|
11
|
-
constantCase,
|
|
12
|
-
dotCase,
|
|
13
|
-
headerCase,
|
|
14
|
-
noCase,
|
|
15
|
-
paramCase,
|
|
16
|
-
pascalCase,
|
|
17
|
-
pathCase,
|
|
18
|
-
sentenceCase,
|
|
19
|
-
snakeCase,
|
|
20
|
-
upperCase,
|
|
21
|
-
lowerCase,
|
|
22
|
-
};
|
|
23
|
-
// Ignore fields needed by Federation spec
|
|
24
|
-
const IGNORED_ROOT_FIELD_NAMES = ['_service', '_entities'];
|
|
25
|
-
const IGNORED_TYPE_NAMES = [
|
|
26
|
-
'date',
|
|
27
|
-
'hostname',
|
|
28
|
-
'regex',
|
|
29
|
-
'json-pointer',
|
|
30
|
-
'relative-json-pointer',
|
|
31
|
-
'uri-reference',
|
|
32
|
-
'uri-template',
|
|
33
|
-
...Object.keys(resolvers),
|
|
34
|
-
];
|
|
35
|
-
class NamingConventionTransform {
|
|
1
|
+
import { applyRequestTransforms, applyResultTransforms, applySchemaTransforms, } from '@graphql-mesh/utils';
|
|
2
|
+
import { RenameInputObjectFields, RenameInterfaceFields, RenameObjectFieldArguments, RenameTypes, TransformEnumValues, TransformObjectFields, } from '@graphql-tools/wrap';
|
|
3
|
+
import { IGNORED_ROOT_FIELD_NAMES, IGNORED_TYPE_NAMES, NAMING_CONVENTIONS } from './shared.js';
|
|
4
|
+
export default class NamingConventionTransform {
|
|
36
5
|
constructor(options) {
|
|
37
6
|
this.transforms = [];
|
|
38
7
|
if (options.config.typeNames) {
|
|
39
8
|
const namingConventionFn = NAMING_CONVENTIONS[options.config.typeNames];
|
|
40
|
-
this.transforms.push(new RenameTypes(typeName => IGNORED_TYPE_NAMES.includes(typeName)
|
|
9
|
+
this.transforms.push(new RenameTypes(typeName => IGNORED_TYPE_NAMES.includes(typeName)
|
|
10
|
+
? typeName
|
|
11
|
+
: namingConventionFn(typeName) || typeName));
|
|
41
12
|
}
|
|
42
13
|
if (options.config.fieldNames) {
|
|
43
14
|
const fieldNamingConventionFn = options.config.fieldNames
|
|
44
15
|
? NAMING_CONVENTIONS[options.config.fieldNames]
|
|
45
16
|
: (s) => s;
|
|
46
17
|
this.transforms.push(new RenameInputObjectFields((_, fieldName) => fieldNamingConventionFn(fieldName) || fieldName), new TransformObjectFields((_, fieldName, fieldConfig) => [
|
|
47
|
-
IGNORED_ROOT_FIELD_NAMES.includes(fieldName)
|
|
18
|
+
IGNORED_ROOT_FIELD_NAMES.includes(fieldName)
|
|
19
|
+
? fieldName
|
|
20
|
+
: fieldNamingConventionFn(fieldName) || fieldName,
|
|
48
21
|
fieldConfig,
|
|
49
22
|
]), new RenameInterfaceFields((_, fieldName) => fieldNamingConventionFn(fieldName) || fieldName));
|
|
50
23
|
}
|
|
@@ -58,13 +31,7 @@ class NamingConventionTransform {
|
|
|
58
31
|
const namingConventionFn = NAMING_CONVENTIONS[options.config.enumValues];
|
|
59
32
|
this.transforms.push(new TransformEnumValues((typeName, externalValue, enumValueConfig) => {
|
|
60
33
|
const newEnumValue = namingConventionFn(externalValue) || externalValue;
|
|
61
|
-
return [
|
|
62
|
-
newEnumValue,
|
|
63
|
-
{
|
|
64
|
-
...enumValueConfig,
|
|
65
|
-
value: newEnumValue,
|
|
66
|
-
},
|
|
67
|
-
];
|
|
34
|
+
return [newEnumValue, enumValueConfig];
|
|
68
35
|
}));
|
|
69
36
|
}
|
|
70
37
|
}
|
|
@@ -78,5 +45,3 @@ class NamingConventionTransform {
|
|
|
78
45
|
return applyResultTransforms(originalResult, delegationContext, transformationContext, this.transforms);
|
|
79
46
|
}
|
|
80
47
|
}
|
|
81
|
-
|
|
82
|
-
export default NamingConventionTransform;
|
package/package.json
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphql-mesh/transform-naming-convention",
|
|
3
|
-
"version": "1.0.0-alpha-
|
|
3
|
+
"version": "1.0.0-alpha-20230420181317-a95037648",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"peerDependencies": {
|
|
6
|
-
"@graphql-mesh/types": "0.
|
|
7
|
-
"@graphql-mesh/utils": "1.0.0-alpha-
|
|
8
|
-
"graphql": "
|
|
6
|
+
"@graphql-mesh/types": "1.0.0-alpha-20230420181317-a95037648",
|
|
7
|
+
"@graphql-mesh/utils": "1.0.0-alpha-20230420181317-a95037648",
|
|
8
|
+
"@graphql-tools/utils": "^9.2.1",
|
|
9
|
+
"graphql": "*",
|
|
10
|
+
"tslib": "^2.5.0"
|
|
9
11
|
},
|
|
10
12
|
"dependencies": {
|
|
11
|
-
"@graphql-tools/delegate": "
|
|
12
|
-
"@graphql-tools/
|
|
13
|
-
"@graphql-tools/wrap": "8.5.0",
|
|
13
|
+
"@graphql-tools/delegate": "9.0.32",
|
|
14
|
+
"@graphql-tools/wrap": "9.4.2",
|
|
14
15
|
"change-case": "4.1.2",
|
|
15
|
-
"graphql-scalars": "1.
|
|
16
|
+
"graphql-scalars": "^1.20.4",
|
|
16
17
|
"lower-case": "2.0.2",
|
|
17
|
-
"tslib": "^2.4.0",
|
|
18
18
|
"upper-case": "2.0.2"
|
|
19
19
|
},
|
|
20
20
|
"repository": {
|
|
@@ -23,21 +23,28 @@
|
|
|
23
23
|
"directory": "packages/transforms/naming-convention"
|
|
24
24
|
},
|
|
25
25
|
"license": "MIT",
|
|
26
|
-
"main": "index.js",
|
|
27
|
-
"module": "index.
|
|
28
|
-
"typings": "index.d.ts",
|
|
26
|
+
"main": "cjs/index.js",
|
|
27
|
+
"module": "esm/index.js",
|
|
28
|
+
"typings": "typings/index.d.ts",
|
|
29
29
|
"typescript": {
|
|
30
|
-
"definition": "index.d.ts"
|
|
30
|
+
"definition": "typings/index.d.ts"
|
|
31
31
|
},
|
|
32
|
+
"type": "module",
|
|
32
33
|
"exports": {
|
|
33
34
|
".": {
|
|
34
|
-
"require":
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
"
|
|
39
|
-
|
|
35
|
+
"require": {
|
|
36
|
+
"types": "./typings/index.d.cts",
|
|
37
|
+
"default": "./cjs/index.js"
|
|
38
|
+
},
|
|
39
|
+
"import": {
|
|
40
|
+
"types": "./typings/index.d.ts",
|
|
41
|
+
"default": "./esm/index.js"
|
|
42
|
+
},
|
|
43
|
+
"default": {
|
|
44
|
+
"types": "./typings/index.d.ts",
|
|
45
|
+
"default": "./esm/index.js"
|
|
46
|
+
}
|
|
40
47
|
},
|
|
41
48
|
"./package.json": "./package.json"
|
|
42
49
|
}
|
|
43
|
-
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { GraphQLEnumType, GraphQLInterfaceType, GraphQLList, GraphQLNonNull, GraphQLObjectType, GraphQLOutputType, GraphQLScalarType, GraphQLSchema, GraphQLUnionType } from 'graphql';
|
|
2
|
+
import { MeshTransform, MeshTransformOptions, YamlConfig } from '@graphql-mesh/types';
|
|
3
|
+
export declare type GraphQLTypePointer = GraphQLList<GraphQLOutputType> | GraphQLNonNull<GraphQLScalarType | GraphQLObjectType | GraphQLInterfaceType | GraphQLUnionType | GraphQLEnumType | GraphQLList<GraphQLOutputType>>;
|
|
4
|
+
export default class NamingConventionTransform implements MeshTransform {
|
|
5
|
+
noWrap: boolean;
|
|
6
|
+
config: Omit<YamlConfig.NamingConventionTransformConfig, 'mode'>;
|
|
7
|
+
constructor(options: MeshTransformOptions<YamlConfig.NamingConventionTransformConfig>);
|
|
8
|
+
transformSchema(schema: GraphQLSchema): GraphQLSchema;
|
|
9
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { GraphQLEnumType, GraphQLInterfaceType, GraphQLList, GraphQLNonNull, GraphQLObjectType, GraphQLOutputType, GraphQLScalarType, GraphQLSchema, GraphQLUnionType } from 'graphql';
|
|
2
|
+
import { MeshTransform, MeshTransformOptions, YamlConfig } from '@graphql-mesh/types';
|
|
3
|
+
export declare type GraphQLTypePointer = GraphQLList<GraphQLOutputType> | GraphQLNonNull<GraphQLScalarType | GraphQLObjectType | GraphQLInterfaceType | GraphQLUnionType | GraphQLEnumType | GraphQLList<GraphQLOutputType>>;
|
|
4
|
+
export default class NamingConventionTransform implements MeshTransform {
|
|
5
|
+
noWrap: boolean;
|
|
6
|
+
config: Omit<YamlConfig.NamingConventionTransformConfig, 'mode'>;
|
|
7
|
+
constructor(options: MeshTransformOptions<YamlConfig.NamingConventionTransformConfig>);
|
|
8
|
+
transformSchema(schema: GraphQLSchema): GraphQLSchema;
|
|
9
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { MeshTransformOptions, YamlConfig } from '@graphql-mesh/types';
|
|
2
|
+
import BareNamingConvention from './bareNamingConvention.cjs';
|
|
3
|
+
import WrapNamingConvention from './wrapNamingConvention.cjs';
|
|
4
|
+
interface NamingConventionTransformConstructor {
|
|
5
|
+
new (options: MeshTransformOptions<YamlConfig.NamingConventionTransformConfig>): WrapNamingConvention | BareNamingConvention;
|
|
6
|
+
}
|
|
7
|
+
declare const _default: NamingConventionTransformConstructor;
|
|
8
|
+
export default _default;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { MeshTransformOptions, YamlConfig } from '@graphql-mesh/types';
|
|
2
|
+
import BareNamingConvention from './bareNamingConvention.js';
|
|
3
|
+
import WrapNamingConvention from './wrapNamingConvention.js';
|
|
4
|
+
interface NamingConventionTransformConstructor {
|
|
5
|
+
new (options: MeshTransformOptions<YamlConfig.NamingConventionTransformConfig>): WrapNamingConvention | BareNamingConvention;
|
|
6
|
+
}
|
|
7
|
+
declare const _default: NamingConventionTransformConstructor;
|
|
8
|
+
export default _default;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { YamlConfig } from '@graphql-mesh/types';
|
|
2
|
+
type NamingConventionFn = (input: string) => string;
|
|
3
|
+
type NamingConventionType = YamlConfig.NamingConventionTransformConfig['typeNames'];
|
|
4
|
+
export declare const NAMING_CONVENTIONS: Record<NamingConventionType, NamingConventionFn>;
|
|
5
|
+
export declare const IGNORED_ROOT_FIELD_NAMES: string[];
|
|
6
|
+
export declare const IGNORED_TYPE_NAMES: string[];
|
|
7
|
+
export {};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { YamlConfig } from '@graphql-mesh/types';
|
|
2
|
+
type NamingConventionFn = (input: string) => string;
|
|
3
|
+
type NamingConventionType = YamlConfig.NamingConventionTransformConfig['typeNames'];
|
|
4
|
+
export declare const NAMING_CONVENTIONS: Record<NamingConventionType, NamingConventionFn>;
|
|
5
|
+
export declare const IGNORED_ROOT_FIELD_NAMES: string[];
|
|
6
|
+
export declare const IGNORED_TYPE_NAMES: string[];
|
|
7
|
+
export {};
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { GraphQLSchema } from 'graphql';
|
|
2
|
-
import { MeshTransform,
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
2
|
+
import { MeshTransform, MeshTransformOptions, YamlConfig } from '@graphql-mesh/types';
|
|
3
|
+
import { DelegationContext, SubschemaConfig } from '@graphql-tools/delegate';
|
|
4
|
+
import { ExecutionRequest, ExecutionResult } from '@graphql-tools/utils';
|
|
5
5
|
export default class NamingConventionTransform implements MeshTransform {
|
|
6
6
|
private transforms;
|
|
7
7
|
constructor(options: MeshTransformOptions<YamlConfig.NamingConventionTransformConfig>);
|
|
8
8
|
transformSchema(originalWrappingSchema: GraphQLSchema, subschemaConfig: SubschemaConfig, transformedSchema?: GraphQLSchema): GraphQLSchema;
|
|
9
|
-
transformRequest(originalRequest: ExecutionRequest, delegationContext: DelegationContext, transformationContext: Record<string, any>): ExecutionRequest<
|
|
10
|
-
transformResult(originalResult: ExecutionResult, delegationContext: DelegationContext, transformationContext: any): ExecutionResult<
|
|
9
|
+
transformRequest(originalRequest: ExecutionRequest, delegationContext: DelegationContext, transformationContext: Record<string, any>): ExecutionRequest<any, any, any, Record<string, any>, any>;
|
|
10
|
+
transformResult(originalResult: ExecutionResult, delegationContext: DelegationContext, transformationContext: any): ExecutionResult<any, any>;
|
|
11
11
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { GraphQLSchema } from 'graphql';
|
|
2
|
+
import { MeshTransform, MeshTransformOptions, YamlConfig } from '@graphql-mesh/types';
|
|
3
|
+
import { DelegationContext, SubschemaConfig } from '@graphql-tools/delegate';
|
|
4
|
+
import { ExecutionRequest, ExecutionResult } from '@graphql-tools/utils';
|
|
5
|
+
export default class NamingConventionTransform implements MeshTransform {
|
|
6
|
+
private transforms;
|
|
7
|
+
constructor(options: MeshTransformOptions<YamlConfig.NamingConventionTransformConfig>);
|
|
8
|
+
transformSchema(originalWrappingSchema: GraphQLSchema, subschemaConfig: SubschemaConfig, transformedSchema?: GraphQLSchema): GraphQLSchema;
|
|
9
|
+
transformRequest(originalRequest: ExecutionRequest, delegationContext: DelegationContext, transformationContext: Record<string, any>): ExecutionRequest<any, any, any, Record<string, any>, any>;
|
|
10
|
+
transformResult(originalResult: ExecutionResult, delegationContext: DelegationContext, transformationContext: any): ExecutionResult<any, any>;
|
|
11
|
+
}
|
package/index.js
DELETED
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const wrap = require('@graphql-tools/wrap');
|
|
4
|
-
const utils = require('@graphql-mesh/utils');
|
|
5
|
-
const changeCase = require('change-case');
|
|
6
|
-
const upperCase = require('upper-case');
|
|
7
|
-
const lowerCase = require('lower-case');
|
|
8
|
-
const graphqlScalars = require('graphql-scalars');
|
|
9
|
-
|
|
10
|
-
const NAMING_CONVENTIONS = {
|
|
11
|
-
camelCase: changeCase.camelCase,
|
|
12
|
-
capitalCase: changeCase.capitalCase,
|
|
13
|
-
constantCase: changeCase.constantCase,
|
|
14
|
-
dotCase: changeCase.dotCase,
|
|
15
|
-
headerCase: changeCase.headerCase,
|
|
16
|
-
noCase: changeCase.noCase,
|
|
17
|
-
paramCase: changeCase.paramCase,
|
|
18
|
-
pascalCase: changeCase.pascalCase,
|
|
19
|
-
pathCase: changeCase.pathCase,
|
|
20
|
-
sentenceCase: changeCase.sentenceCase,
|
|
21
|
-
snakeCase: changeCase.snakeCase,
|
|
22
|
-
upperCase: upperCase.upperCase,
|
|
23
|
-
lowerCase: lowerCase.lowerCase,
|
|
24
|
-
};
|
|
25
|
-
// Ignore fields needed by Federation spec
|
|
26
|
-
const IGNORED_ROOT_FIELD_NAMES = ['_service', '_entities'];
|
|
27
|
-
const IGNORED_TYPE_NAMES = [
|
|
28
|
-
'date',
|
|
29
|
-
'hostname',
|
|
30
|
-
'regex',
|
|
31
|
-
'json-pointer',
|
|
32
|
-
'relative-json-pointer',
|
|
33
|
-
'uri-reference',
|
|
34
|
-
'uri-template',
|
|
35
|
-
...Object.keys(graphqlScalars.resolvers),
|
|
36
|
-
];
|
|
37
|
-
class NamingConventionTransform {
|
|
38
|
-
constructor(options) {
|
|
39
|
-
this.transforms = [];
|
|
40
|
-
if (options.config.typeNames) {
|
|
41
|
-
const namingConventionFn = NAMING_CONVENTIONS[options.config.typeNames];
|
|
42
|
-
this.transforms.push(new wrap.RenameTypes(typeName => IGNORED_TYPE_NAMES.includes(typeName) ? typeName : namingConventionFn(typeName) || typeName));
|
|
43
|
-
}
|
|
44
|
-
if (options.config.fieldNames) {
|
|
45
|
-
const fieldNamingConventionFn = options.config.fieldNames
|
|
46
|
-
? NAMING_CONVENTIONS[options.config.fieldNames]
|
|
47
|
-
: (s) => s;
|
|
48
|
-
this.transforms.push(new wrap.RenameInputObjectFields((_, fieldName) => fieldNamingConventionFn(fieldName) || fieldName), new wrap.TransformObjectFields((_, fieldName, fieldConfig) => [
|
|
49
|
-
IGNORED_ROOT_FIELD_NAMES.includes(fieldName) ? fieldName : fieldNamingConventionFn(fieldName) || fieldName,
|
|
50
|
-
fieldConfig,
|
|
51
|
-
]), new wrap.RenameInterfaceFields((_, fieldName) => fieldNamingConventionFn(fieldName) || fieldName));
|
|
52
|
-
}
|
|
53
|
-
if (options.config.fieldArgumentNames) {
|
|
54
|
-
const fieldArgNamingConventionFn = options.config.fieldArgumentNames
|
|
55
|
-
? NAMING_CONVENTIONS[options.config.fieldArgumentNames]
|
|
56
|
-
: (s) => s;
|
|
57
|
-
this.transforms.push(new wrap.RenameObjectFieldArguments((_typeName, _fieldName, argName) => fieldArgNamingConventionFn(argName)));
|
|
58
|
-
}
|
|
59
|
-
if (options.config.enumValues) {
|
|
60
|
-
const namingConventionFn = NAMING_CONVENTIONS[options.config.enumValues];
|
|
61
|
-
this.transforms.push(new wrap.TransformEnumValues((typeName, externalValue, enumValueConfig) => {
|
|
62
|
-
const newEnumValue = namingConventionFn(externalValue) || externalValue;
|
|
63
|
-
return [
|
|
64
|
-
newEnumValue,
|
|
65
|
-
{
|
|
66
|
-
...enumValueConfig,
|
|
67
|
-
value: newEnumValue,
|
|
68
|
-
},
|
|
69
|
-
];
|
|
70
|
-
}));
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
|
|
74
|
-
return utils.applySchemaTransforms(originalWrappingSchema, subschemaConfig, transformedSchema, this.transforms);
|
|
75
|
-
}
|
|
76
|
-
transformRequest(originalRequest, delegationContext, transformationContext) {
|
|
77
|
-
return utils.applyRequestTransforms(originalRequest, delegationContext, transformationContext, this.transforms);
|
|
78
|
-
}
|
|
79
|
-
transformResult(originalResult, delegationContext, transformationContext) {
|
|
80
|
-
return utils.applyResultTransforms(originalResult, delegationContext, transformationContext, this.transforms);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
module.exports = NamingConventionTransform;
|