UNPKG

@graphql-codegen/c-sharp-common

Version:
70 lines (69 loc) 2.43 kB
// This file is bundled and inlined. // We should probably make this a shared package though. // eslint-disable-next-line import/no-extraneous-dependencies import { Kind } from 'graphql'; // eslint-disable-next-line import/no-extraneous-dependencies import { indent } from '@graphql-codegen/visitor-plugin-common'; import { csharpValueTypes } from './scalars.js'; export function transformComment(comment, indentLevel = 0) { if (!comment) { return ''; } if (isStringValueNode(comment)) { comment = comment.value; } comment = comment.trimStart().split('*/').join('*\\/'); let lines = comment.split('\n'); lines = ['/// <summary>', ...lines.map(line => `/// ${line}`), '/// </summary>']; return lines .map(line => indent(line, indentLevel)) .concat('') .join('\n'); } function isStringValueNode(node) { return node && typeof node === 'object' && node.kind === Kind.STRING; } export function isValueType(type) { // Limitation: only checks the list of known built in value types // Eg .NET types and struct types won't be detected correctly return csharpValueTypes.includes(type); } export function getListTypeField(typeNode) { if (typeNode.kind === Kind.LIST_TYPE) { return { required: false, type: getListTypeField(typeNode.type), }; } if (typeNode.kind === Kind.NON_NULL_TYPE && typeNode.type.kind === Kind.LIST_TYPE) { return Object.assign(getListTypeField(typeNode.type), { required: true, }); } if (typeNode.kind === Kind.NON_NULL_TYPE) { return getListTypeField(typeNode.type); } return undefined; } export function getListTypeDepth(listType) { if (listType) { return getListTypeDepth(listType.type) + 1; } return 0; } export function getListInnerTypeNode(typeNode) { if (typeNode.kind === Kind.LIST_TYPE) { return getListInnerTypeNode(typeNode.type); } if (typeNode.kind === Kind.NON_NULL_TYPE && typeNode.type.kind === Kind.LIST_TYPE) { return getListInnerTypeNode(typeNode.type); } return typeNode; } export function wrapFieldType(fieldType, listTypeField, listType = 'IEnumerable') { if (listTypeField) { const innerType = wrapFieldType(fieldType, listTypeField.type, listType); return `${listType}<${innerType}>`; } return fieldType.innerTypeName; }