UNPKG

@graphql-codegen/typescript-nest

Version:

GraphQL Code Generator plugin for generating NestJS compatible types

50 lines (49 loc) 1.49 kB
export const isDefinitionInterface = definition => definition.includes('@Nest.InterfaceType()'); export const escapeString = (str) => "'" + String(str || '') .replace(/\\/g, '\\\\') .replace(/'/g, "\\'") + "'"; export 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(', ') + ' }')); }; export 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; }; export const fixDecorator = (type, typeString) => { return type.isArray || type.isNullable || type.isScalar ? typeString : `FixDecorator<${typeString}>`; }; export const getNestNullableValue = (type) => { if (type.isNullable) { if (type.isItemsNullable) { return "'itemsAndList'"; } return 'true'; } if (type.isItemsNullable) { return "'items'"; } return undefined; };