@graphql-codegen/flow
Version:
GraphQL Code Generator plugin for generating Flow types
43 lines (42 loc) • 1.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FlowOperationVariablesToObject = void 0;
const graphql_1 = require("graphql");
const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common");
class FlowOperationVariablesToObject extends visitor_plugin_common_1.OperationVariablesToObject {
clearOptional(str) {
if (str.startsWith('?')) {
return str.replace(/^\?(.*?)$/i, '$1');
}
return str;
}
getScalar(name) {
const prefix = this._namespacedImportName ? `${this._namespacedImportName}.` : '';
return `$ElementType<${prefix}Scalars, '${name}'>`;
}
wrapAstTypeWithModifiers(baseType, typeNode, applyCoercion = false) {
if (typeNode.kind === graphql_1.Kind.NON_NULL_TYPE) {
const type = this.wrapAstTypeWithModifiers(baseType, typeNode.type, applyCoercion);
return this.clearOptional(type);
}
if (typeNode.kind === graphql_1.Kind.LIST_TYPE) {
const innerType = this.wrapAstTypeWithModifiers(baseType, typeNode.type, applyCoercion);
const listInputCoercionExtension = applyCoercion ? ` | ${innerType}` : '';
return `?Array<${innerType}>${listInputCoercionExtension}`;
}
return `?${baseType}`;
}
formatFieldString(fieldName, isNonNullType, hasDefaultValue) {
if (hasDefaultValue || isNonNullType) {
return fieldName;
}
return `${fieldName}?`;
}
formatTypeString(fieldType, isNonNullType, hasDefaultValue) {
if (hasDefaultValue && !isNonNullType) {
return this.clearOptional(fieldType);
}
return fieldType;
}
}
exports.FlowOperationVariablesToObject = FlowOperationVariablesToObject;