UNPKG

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