UNPKG

4.86 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.OperationVariablesToObject = void 0;
4const tslib_1 = require("tslib");
5const graphql_1 = require("graphql");
6const utils_js_1 = require("./utils.js");
7const auto_bind_1 = tslib_1.__importDefault(require("auto-bind"));
8class OperationVariablesToObject {
9 constructor(_scalars, _convertName, _namespacedImportName = null, _enumNames = [], _enumPrefix = true, _enumValues = {}, _applyCoercion = false, _directiveArgumentAndInputFieldMappings = {}) {
10 this._scalars = _scalars;
11 this._convertName = _convertName;
12 this._namespacedImportName = _namespacedImportName;
13 this._enumNames = _enumNames;
14 this._enumPrefix = _enumPrefix;
15 this._enumValues = _enumValues;
16 this._applyCoercion = _applyCoercion;
17 this._directiveArgumentAndInputFieldMappings = _directiveArgumentAndInputFieldMappings;
18 (0, auto_bind_1.default)(this);
19 }
20 getName(node) {
21 if (node.name) {
22 if (typeof node.name === 'string') {
23 return node.name;
24 }
25 return node.name.value;
26 }
27 if (node.variable) {
28 return node.variable.name.value;
29 }
30 return null;
31 }
32 transform(variablesNode) {
33 if (!variablesNode || variablesNode.length === 0) {
34 return null;
35 }
36 return (variablesNode.map(variable => (0, utils_js_1.indent)(this.transformVariable(variable))).join(`${this.getPunctuation()}\n`) +
37 this.getPunctuation());
38 }
39 getScalar(name) {
40 const prefix = this._namespacedImportName ? `${this._namespacedImportName}.` : '';
41 return `${prefix}Scalars['${name}']`;
42 }
43 getDirectiveMapping(name) {
44 return `DirectiveArgumentAndInputFieldMappings['${name}']`;
45 }
46 getDirectiveOverrideType(directives) {
47 if (!this._directiveArgumentAndInputFieldMappings)
48 return null;
49 const type = directives
50 .map(directive => {
51 const directiveName = directive.name.value;
52 if (this._directiveArgumentAndInputFieldMappings[directiveName]) {
53 return this.getDirectiveMapping(directiveName);
54 }
55 return null;
56 })
57 .reverse()
58 .find(a => !!a);
59 return type || null;
60 }
61 transformVariable(variable) {
62 let typeValue = null;
63 const prefix = this._namespacedImportName ? `${this._namespacedImportName}.` : '';
64 if (typeof variable.type === 'string') {
65 typeValue = variable.type;
66 }
67 else {
68 const baseType = (0, utils_js_1.getBaseTypeNode)(variable.type);
69 const overrideType = variable.directives ? this.getDirectiveOverrideType(variable.directives) : null;
70 const typeName = baseType.name.value;
71 if (overrideType) {
72 typeValue = overrideType;
73 }
74 else if (this._scalars[typeName]) {
75 typeValue = this.getScalar(typeName);
76 }
77 else if (this._enumValues[typeName] && this._enumValues[typeName].sourceFile) {
78 typeValue = this._enumValues[typeName].typeIdentifier || this._enumValues[typeName].sourceIdentifier;
79 }
80 else {
81 typeValue = `${prefix}${this._convertName(baseType, {
82 useTypesPrefix: this._enumNames.includes(typeName) ? this._enumPrefix : true,
83 })}`;
84 }
85 }
86 const fieldName = this.getName(variable);
87 const fieldType = this.wrapAstTypeWithModifiers(typeValue, variable.type, this._applyCoercion);
88 const hasDefaultValue = variable.defaultValue != null && typeof variable.defaultValue !== 'undefined';
89 const isNonNullType = variable.type.kind === graphql_1.Kind.NON_NULL_TYPE;
90 const formattedFieldString = this.formatFieldString(fieldName, isNonNullType, hasDefaultValue);
91 const formattedTypeString = this.formatTypeString(fieldType, isNonNullType, hasDefaultValue);
92 return `${formattedFieldString}: ${formattedTypeString}`;
93 }
94 wrapAstTypeWithModifiers(_baseType, _typeNode, _applyCoercion) {
95 throw new Error(`You must override "wrapAstTypeWithModifiers" of OperationVariablesToObject!`);
96 }
97 formatFieldString(fieldName, isNonNullType, _hasDefaultValue) {
98 return fieldName;
99 }
100 formatTypeString(fieldType, isNonNullType, hasDefaultValue) {
101 const prefix = this._namespacedImportName ? `${this._namespacedImportName}.` : '';
102 if (hasDefaultValue) {
103 return `${prefix}Maybe<${fieldType}>`;
104 }
105 return fieldType;
106 }
107 getPunctuation() {
108 return ',';
109 }
110}
111exports.OperationVariablesToObject = OperationVariablesToObject;