@eddeee888/gcg-typescript-resolver-files
Version:
This [GraphQL Code Generator](https://www.the-guild.dev/graphql/codegen) plugin creates resolvers given GraphQL schema.
116 lines • 5.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.collectTypeMappersFromSourceFile = void 0;
const tslib_1 = require("tslib");
const path = tslib_1.__importStar(require("path"));
const ts_morph_1 = require("ts-morph");
const utils_1 = require("../utils");
const collectTypeMappersFromSourceFile = ({ typeMappersSourceFile, typeMappersSuffix, resolverTypesPath, emitLegacyCommonJSImports, }, result) => {
// Look for interfaces with exported keywords
typeMappersSourceFile.getInterfaces().forEach((interfaceDeclaration) => {
if (!interfaceDeclaration.hasExportKeyword()) {
return;
}
addTypeMapperDetailsIfValid({
kind: ts_morph_1.SyntaxKind.InterfaceDeclaration,
identifierNode: interfaceDeclaration.getNameNode(),
typeMappersSuffix,
typeMappersFilePath: typeMappersSourceFile.getFilePath(),
resolverTypesPath,
emitLegacyCommonJSImports,
}, result);
});
// Look for exported types with exported keywords
typeMappersSourceFile.getTypeAliases().forEach((typeAlias) => {
if (!typeAlias.hasExportKeyword()) {
return;
}
const identifierNode = typeAlias.getNameNode();
addTypeMapperDetailsIfValid({
kind: ts_morph_1.SyntaxKind.TypeAliasDeclaration,
identifierNode,
typeMappersSuffix,
typeMappersFilePath: typeMappersSourceFile.getFilePath(),
resolverTypesPath,
emitLegacyCommonJSImports,
}, result);
});
// Look for named exports e.g.
// - export { something } from 'module';
// - export type { something } from 'module';
// - export { something, somethingelse as somethingelse2 }'
typeMappersSourceFile.getExportDeclarations().forEach((exportDeclaration) => {
exportDeclaration.getNamedExports().forEach((namedExport) => {
let identifierNode = namedExport.getNameNode();
const aliasNode = namedExport.getAliasNode();
if (aliasNode) {
identifierNode = aliasNode;
}
addTypeMapperDetailsIfValid({
kind: ts_morph_1.SyntaxKind.ExportSpecifier,
identifierNode,
typeMappersSuffix,
typeMappersFilePath: typeMappersSourceFile.getFilePath(),
resolverTypesPath,
emitLegacyCommonJSImports,
}, result);
});
});
typeMappersSourceFile.getClasses().forEach((classDeclaration) => {
if (!classDeclaration.hasExportKeyword()) {
return;
}
const identifierNode = classDeclaration.getNameNode();
if (!identifierNode) {
// Anonymous class is skipped
return;
}
addTypeMapperDetailsIfValid({
kind: ts_morph_1.SyntaxKind.ClassDeclaration,
identifierNode,
typeMappersSuffix,
typeMappersFilePath: typeMappersSourceFile.getFilePath(),
resolverTypesPath,
emitLegacyCommonJSImports,
}, result);
});
};
exports.collectTypeMappersFromSourceFile = collectTypeMappersFromSourceFile;
const addTypeMapperDetailsIfValid = ({ kind, identifierNode, typeMappersSuffix, typeMappersFilePath, resolverTypesPath, emitLegacyCommonJSImports, }, result) => {
const identifierName = identifierNode.getText();
if (!identifierName.endsWith(typeMappersSuffix)) {
return;
}
const [schemaType] = identifierName.split(typeMappersSuffix);
if (!schemaType) {
return;
}
/**
* We MUST use path.relative() instead of path.posix.relative() here
* Reason being `typeMappersFilePath` is a file system path e.g. C://Windows/Path/To/File or /Unix/path/to/file
* path.relative works correctly but returns inconsistent path seperator e.g. relative\\path\\windows or relative/path/unix
*
* Therefore, we need to split/join to normalise the path to posix path e.g relative/path/unix
*/
const relativePath = path
.relative(path.dirname(resolverTypesPath), typeMappersFilePath)
.split(path.sep)
.join(path.posix.sep);
const parsedRelativePathFromResolverTypesToSourceFile = path.parse(relativePath);
const relativeImportPathFromResolverTypesToSourceFile = (0, utils_1.normalizeRelativePath)(path.posix.join(parsedRelativePathFromResolverTypesToSourceFile.dir, parsedRelativePathFromResolverTypesToSourceFile.name));
const fileExtension = emitLegacyCommonJSImports ? '' : '.js';
const configImportPath = `${relativeImportPathFromResolverTypesToSourceFile}${fileExtension}#${identifierName}`;
if (result[schemaType]) {
throw new Error(`GraphQL type "${schemaType}" has duplicated "${identifierName}" mappers:\n - ${configImportPath}\n - ${result[schemaType].configImportPath}`);
}
result[schemaType] = {
schemaType,
mapper: {
name: identifierName,
filename: typeMappersFilePath,
kind,
},
configImportPath,
};
};
//# sourceMappingURL=collectTypeMappersFromSourceFile.js.map