@graphql-codegen/c-sharp-common
Version:
79 lines (78 loc) • 3.04 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.wrapFieldType = exports.getListInnerTypeNode = exports.getListTypeDepth = exports.getListTypeField = exports.isValueType = exports.transformComment = void 0;
// This file is bundled and inlined.
// We should probably make this a shared package though.
// eslint-disable-next-line import/no-extraneous-dependencies
const graphql_1 = require("graphql");
// eslint-disable-next-line import/no-extraneous-dependencies
const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common");
const scalars_js_1 = require("./scalars.js");
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 => (0, visitor_plugin_common_1.indent)(line, indentLevel))
.concat('')
.join('\n');
}
exports.transformComment = transformComment;
function isStringValueNode(node) {
return node && typeof node === 'object' && node.kind === graphql_1.Kind.STRING;
}
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 scalars_js_1.csharpValueTypes.includes(type);
}
exports.isValueType = isValueType;
function getListTypeField(typeNode) {
if (typeNode.kind === graphql_1.Kind.LIST_TYPE) {
return {
required: false,
type: getListTypeField(typeNode.type),
};
}
if (typeNode.kind === graphql_1.Kind.NON_NULL_TYPE && typeNode.type.kind === graphql_1.Kind.LIST_TYPE) {
return Object.assign(getListTypeField(typeNode.type), {
required: true,
});
}
if (typeNode.kind === graphql_1.Kind.NON_NULL_TYPE) {
return getListTypeField(typeNode.type);
}
return undefined;
}
exports.getListTypeField = getListTypeField;
function getListTypeDepth(listType) {
if (listType) {
return getListTypeDepth(listType.type) + 1;
}
return 0;
}
exports.getListTypeDepth = getListTypeDepth;
function getListInnerTypeNode(typeNode) {
if (typeNode.kind === graphql_1.Kind.LIST_TYPE) {
return getListInnerTypeNode(typeNode.type);
}
if (typeNode.kind === graphql_1.Kind.NON_NULL_TYPE && typeNode.type.kind === graphql_1.Kind.LIST_TYPE) {
return getListInnerTypeNode(typeNode.type);
}
return typeNode;
}
exports.getListInnerTypeNode = getListInnerTypeNode;
function wrapFieldType(fieldType, listTypeField, listType = 'IEnumerable') {
if (listTypeField) {
const innerType = wrapFieldType(fieldType, listTypeField.type, listType);
return `${listType}<${innerType}>`;
}
return fieldType.innerTypeName;
}
exports.wrapFieldType = wrapFieldType;
;