1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.TypeScriptSelectionSetProcessor = void 0;
|
4 | const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common");
|
5 | class TypeScriptSelectionSetProcessor extends visitor_plugin_common_1.BaseSelectionSetProcessor {
|
6 | transformPrimitiveFields(schemaType, fields, unsetTypes) {
|
7 | if (fields.length === 0) {
|
8 | return [];
|
9 | }
|
10 | const parentName = (this.config.namespacedImportName ? `${this.config.namespacedImportName}.` : '') +
|
11 | this.config.convertName(schemaType.name, {
|
12 | useTypesPrefix: true,
|
13 | });
|
14 | if (unsetTypes) {
|
15 | return [`MakeEmpty<${parentName}, ${fields.map(field => `'${field.fieldName}'`).join(' | ')}>`];
|
16 | }
|
17 | let hasConditionals = false;
|
18 | const conditilnalsList = [];
|
19 | let resString = `Pick<${parentName}, ${fields
|
20 | .map(field => {
|
21 | if (field.isConditional) {
|
22 | hasConditionals = true;
|
23 | conditilnalsList.push(field.fieldName);
|
24 | }
|
25 | return `'${field.fieldName}'`;
|
26 | })
|
27 | .join(' | ')}>`;
|
28 | if (hasConditionals) {
|
29 | const avoidOptional =
|
30 |
|
31 | this.config.avoidOptionals === true ||
|
32 | (typeof this.config.avoidOptionals === 'object' &&
|
33 | (this.config.avoidOptionals.field ||
|
34 | this.config.avoidOptionals.inputValue ||
|
35 | this.config.avoidOptionals.object));
|
36 | const transform = avoidOptional ? 'MakeMaybe' : 'MakeOptional';
|
37 | resString = `${this.config.namespacedImportName ? `${this.config.namespacedImportName}.` : ''}${transform}<${resString}, ${conditilnalsList.map(field => `'${field}'`).join(' | ')}>`;
|
38 | }
|
39 | return [resString];
|
40 | }
|
41 | transformTypenameField(type, name) {
|
42 | return [`{ ${name}: ${type} }`];
|
43 | }
|
44 | transformAliasesPrimitiveFields(schemaType, fields) {
|
45 | if (fields.length === 0) {
|
46 | return [];
|
47 | }
|
48 | const parentName = (this.config.namespacedImportName ? `${this.config.namespacedImportName}.` : '') +
|
49 | this.config.convertName(schemaType.name, {
|
50 | useTypesPrefix: true,
|
51 | });
|
52 | return [
|
53 | `{ ${fields
|
54 | .map(aliasedField => {
|
55 | const value = aliasedField.fieldName === '__typename'
|
56 | ? `'${schemaType.name}'`
|
57 | : `${parentName}['${aliasedField.fieldName}']`;
|
58 | return `${aliasedField.alias}: ${value}`;
|
59 | })
|
60 | .join(', ')} }`,
|
61 | ];
|
62 | }
|
63 | transformLinkFields(fields) {
|
64 | if (fields.length === 0) {
|
65 | return [];
|
66 | }
|
67 | return [`{ ${fields.map(field => `${field.alias || field.name}: ${field.selectionSet}`).join(', ')} }`];
|
68 | }
|
69 | }
|
70 | exports.TypeScriptSelectionSetProcessor = TypeScriptSelectionSetProcessor;
|