@eddeee888/gcg-typescript-resolver-files
Version:
This [GraphQL Code Generator](https://www.the-guild.dev/graphql/codegen) plugin creates resolvers given GraphQL schema.
58 lines • 2.86 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.addObjectTypeResolversPropertyAssignmentNodesIfNotImplemented = void 0;
const ts_morph_1 = require("ts-morph");
const getVariableStatementWithExpectedIdentifier_1 = require("./getVariableStatementWithExpectedIdentifier");
/**
* Ensure objectTypeResolver files have all the resolvers due to mismatched types
*/
const addObjectTypeResolversPropertyAssignmentNodesIfNotImplemented = ({ addedPropertyAssignmentNodes, sourceFile, resolverFile, }) => {
const sourceFilePath = sourceFile.getFilePath().toString();
addedPropertyAssignmentNodes[sourceFilePath] =
addedPropertyAssignmentNodes[sourceFilePath] || {};
const resolversToGenerate = resolverFile.meta.resolversToGenerate || {};
if (!Object.keys(resolversToGenerate).length) {
return;
}
const { variableStatement } = (0, getVariableStatementWithExpectedIdentifier_1.getVariableStatementWithExpectedIdentifier)(sourceFile, resolverFile);
if (!variableStatement) {
throw new Error('Missing variableStatement in addObjectTypeResolversPropertyAssignmentNodesIfNotImplemented.');
}
// 1. Check to see which generated to-be-generated resolvers are implemented
const resolversData = Object.assign({}, resolversToGenerate);
variableStatement
.getDescendantsOfKind(ts_morph_1.SyntaxKind.PropertyAssignment)
.forEach((propertyAssignment) => {
const resolverName = propertyAssignment.getName();
if (resolversData[resolverName]) {
resolversData[resolverName].implemented = true;
}
});
variableStatement
.getDescendantsOfKind(ts_morph_1.SyntaxKind.MethodDeclaration)
.forEach((methodDeclaration) => {
const resolverName = methodDeclaration.getName();
if (resolversData[resolverName]) {
resolversData[resolverName].implemented = true;
}
});
// 2. Add missing resolver properties if they haven't been implemented
Object.values(resolversData).forEach(({ resolverName, resolverDeclaration, implemented }) => {
if (implemented) {
return;
}
const addedNode = variableStatement
.getDescendantsOfKind(ts_morph_1.SyntaxKind.ObjectLiteralExpression)[0]
.addPropertyAssignment({
name: resolverName,
initializer: resolverDeclaration,
});
addedPropertyAssignmentNodes[sourceFilePath][addedNode.getStartLineNumber()] = {
node: addedNode,
resolverFile,
__toBeRemoved: true,
};
});
};
exports.addObjectTypeResolversPropertyAssignmentNodesIfNotImplemented = addObjectTypeResolversPropertyAssignmentNodesIfNotImplemented;
//# sourceMappingURL=addObjectTypeResolversPropertyAssignmentNodesIfNotImplemented.js.map
;