@graphql-codegen/typescript-nest
Version:
GraphQL Code Generator plugin for generating NestJS compatible types
59 lines (58 loc) • 1.98 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getNestNullableValue = exports.fixDecorator = exports.buildTypeString = exports.formatDecoratorOptions = exports.escapeString = exports.isDefinitionInterface = void 0;
const isDefinitionInterface = definition => definition.includes('@Nest.InterfaceType()');
exports.isDefinitionInterface = isDefinitionInterface;
const escapeString = (str) => "'" +
String(str || '')
.replace(/\\/g, '\\\\')
.replace(/'/g, "\\'") +
"'";
exports.escapeString = escapeString;
const formatDecoratorOptions = (options, isFirstArgument = true) => {
if (!Object.keys(options).length) {
return '';
}
return ((isFirstArgument ? '' : ', ') +
('{ ' +
Object.entries(options)
.map(([key, value]) => `${key}: ${JSON.stringify(value).replace(/"/g, '')}`)
.join(', ') +
' }'));
};
exports.formatDecoratorOptions = formatDecoratorOptions;
const buildTypeString = (type) => {
if (!type.isArray && !type.isScalar && !type.isNullable) {
type.type = `FixDecorator<${type.type}>`;
}
if (type.isScalar) {
type.type = `Scalars['${type.type}']`;
}
if (type.isArray) {
type.type = `Array<${type.type}>`;
}
if (type.isNullable) {
type.type = `Maybe<${type.type}>`;
}
return type.type;
};
exports.buildTypeString = buildTypeString;
const fixDecorator = (type, typeString) => {
return type.isArray || type.isNullable || type.isScalar
? typeString
: `FixDecorator<${typeString}>`;
};
exports.fixDecorator = fixDecorator;
const getNestNullableValue = (type) => {
if (type.isNullable) {
if (type.isItemsNullable) {
return "'itemsAndList'";
}
return 'true';
}
if (type.isItemsNullable) {
return "'items'";
}
return undefined;
};
exports.getNestNullableValue = getNestNullableValue;