UNPKG

3.94 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.convertFactory = void 0;
4const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
5const change_case_all_1 = require("change-case-all");
6const utils_js_1 = require("./utils.js");
7function getKind(node) {
8 if (typeof node === 'string') {
9 return 'typeNames';
10 }
11 if (['EnumValueDefinition', 'EnumValue'].includes(node.kind)) {
12 return 'enumValues';
13 }
14 return 'typeNames';
15}
16function getName(node) {
17 if (node == null) {
18 return undefined;
19 }
20 if (typeof node === 'string') {
21 return node;
22 }
23 switch (node.kind) {
24 case 'OperationDefinition':
25 case 'Variable':
26 case 'Argument':
27 case 'FragmentSpread':
28 case 'FragmentDefinition':
29 case 'ObjectField':
30 case 'Directive':
31 case 'NamedType':
32 case 'ScalarTypeDefinition':
33 case 'ObjectTypeDefinition':
34 case 'FieldDefinition':
35 case 'InputValueDefinition':
36 case 'InterfaceTypeDefinition':
37 case 'UnionTypeDefinition':
38 case 'EnumTypeDefinition':
39 case 'EnumValueDefinition':
40 case 'InputObjectTypeDefinition':
41 case 'DirectiveDefinition': {
42 return getName(node.name);
43 }
44 case 'Name': {
45 return node.value;
46 }
47 case 'Field': {
48 return getName(node.alias || node.name);
49 }
50 case 'VariableDefinition': {
51 return getName(node.variable);
52 }
53 }
54 return undefined;
55}
56function convertFactory(config) {
57 function resolveConventionName(type) {
58 if (!config.namingConvention) {
59 return (str, opts = {}) => {
60 return (0, utils_js_1.convertNameParts)(str, change_case_all_1.pascalCase, (0, utils_js_1.getConfigValue)((opts || {}).transformUnderscore, false));
61 };
62 }
63 if (typeof config.namingConvention === 'string') {
64 if (config.namingConvention === 'keep') {
65 return str => str;
66 }
67 return (str, opts = {}) => {
68 return (0, utils_js_1.convertNameParts)(str, (0, plugin_helpers_1.resolveExternalModuleAndFn)(config.namingConvention), (0, utils_js_1.getConfigValue)((opts || {}).transformUnderscore, false));
69 };
70 }
71 if (typeof config.namingConvention === 'function') {
72 return (str, opts = {}) => {
73 return (0, utils_js_1.convertNameParts)(str, config.namingConvention, (0, utils_js_1.getConfigValue)((opts || {}).transformUnderscore, false));
74 };
75 }
76 if (typeof config.namingConvention === 'object' && config.namingConvention[type] === 'keep') {
77 return str => str;
78 }
79 if (typeof config.namingConvention === 'object') {
80 if (!config.namingConvention[type]) {
81 return (str, opts = {}) => {
82 const transformUnderscore = config.namingConvention.transformUnderscore || (opts || {}).transformUnderscore;
83 return (0, utils_js_1.convertNameParts)(str, change_case_all_1.pascalCase, (0, utils_js_1.getConfigValue)(transformUnderscore, false));
84 };
85 }
86 return (str, opts = {}) => {
87 return (0, utils_js_1.convertNameParts)(str, (0, plugin_helpers_1.resolveExternalModuleAndFn)(config.namingConvention[type]), (0, utils_js_1.getConfigValue)((opts || {}).transformUnderscore, true));
88 };
89 }
90 return config.namingConvention[type];
91 }
92 return (node, opts) => {
93 const prefix = opts && opts.prefix;
94 const suffix = opts && opts.suffix;
95 const kind = getKind(node);
96 const str = [prefix || '', getName(node), suffix || ''].join('');
97 return resolveConventionName(kind)(str, opts);
98 };
99}
100exports.convertFactory = convertFactory;