1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.parseEnumValues = void 0;
|
4 | const graphql_1 = require("graphql");
|
5 | const mappers_js_1 = require("./mappers.js");
|
6 | function escapeString(str) {
|
7 | return str.replace(/\\/g, '\\\\').replace(/\n/g, '\\n').replace(/'/g, "\\'");
|
8 | }
|
9 | function parseEnumValues({ schema, mapOrStr = {}, ignoreEnumValuesFromSchema, }) {
|
10 | const allTypes = schema.getTypeMap();
|
11 | const allEnums = Object.keys(allTypes).filter(t => (0, graphql_1.isEnumType)(allTypes[t]));
|
12 | if (typeof mapOrStr === 'object') {
|
13 | if (!ignoreEnumValuesFromSchema) {
|
14 | for (const enumTypeName of allEnums) {
|
15 | const enumType = schema.getType(enumTypeName);
|
16 | for (const { name, value } of enumType.getValues()) {
|
17 | if (value !== name) {
|
18 | mapOrStr[enumTypeName] = mapOrStr[enumTypeName] || {};
|
19 | if (typeof mapOrStr[enumTypeName] !== 'string' && !mapOrStr[enumTypeName][name]) {
|
20 | mapOrStr[enumTypeName][name] = typeof value === 'string' ? escapeString(value) : value;
|
21 | }
|
22 | }
|
23 | }
|
24 | }
|
25 | }
|
26 | const invalidMappings = Object.keys(mapOrStr).filter(gqlName => !allEnums.includes(gqlName));
|
27 | if (invalidMappings.length > 0) {
|
28 | throw new Error(`Invalid 'enumValues' mapping! \n
|
29 | The following types does not exist in your GraphQL schema: ${invalidMappings.join(', ')}`);
|
30 | }
|
31 | return Object.keys(mapOrStr).reduce((prev, gqlIdentifier) => {
|
32 | const pointer = mapOrStr[gqlIdentifier];
|
33 | if (typeof pointer === 'string') {
|
34 | const mapper = (0, mappers_js_1.parseMapper)(pointer, gqlIdentifier);
|
35 | return {
|
36 | ...prev,
|
37 | [gqlIdentifier]: {
|
38 | isDefault: mapper.isExternal && mapper.default,
|
39 | typeIdentifier: gqlIdentifier,
|
40 | sourceFile: mapper.isExternal ? mapper.source : null,
|
41 | sourceIdentifier: mapper.type,
|
42 | importIdentifier: mapper.isExternal ? mapper.import : null,
|
43 | mappedValues: null,
|
44 | },
|
45 | };
|
46 | }
|
47 | if (typeof pointer === 'object') {
|
48 | return {
|
49 | ...prev,
|
50 | [gqlIdentifier]: {
|
51 | isDefault: false,
|
52 | typeIdentifier: gqlIdentifier,
|
53 | sourceFile: null,
|
54 | sourceIdentifier: null,
|
55 | importIdentifier: null,
|
56 | mappedValues: pointer,
|
57 | },
|
58 | };
|
59 | }
|
60 | throw new Error(`Invalid "enumValues" configuration \n
|
61 | Enum "${gqlIdentifier}": expected string or object (with enum values mapping)`);
|
62 | }, {});
|
63 | }
|
64 | if (typeof mapOrStr === 'string') {
|
65 | return allEnums
|
66 | .filter(enumName => !enumName.startsWith('__'))
|
67 | .reduce((prev, enumName) => {
|
68 | return {
|
69 | ...prev,
|
70 | [enumName]: {
|
71 | isDefault: false,
|
72 | typeIdentifier: enumName,
|
73 | sourceFile: mapOrStr,
|
74 | sourceIdentifier: enumName,
|
75 | importIdentifier: enumName,
|
76 | mappedValues: null,
|
77 | },
|
78 | };
|
79 | }, {});
|
80 | }
|
81 | return {};
|
82 | }
|
83 | exports.parseEnumValues = parseEnumValues;
|