@eddeee888/gcg-typescript-resolver-files
Version:
This [GraphQL Code Generator](https://www.the-guild.dev/graphql/codegen) plugin creates resolvers given GraphQL schema.
38 lines • 1.83 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.ensureEnumTypeResolversAreGenerated = void 0;
const ts_morph_1 = require("ts-morph");
const getVariableStatementWithExpectedIdentifier_1 = require("./getVariableStatementWithExpectedIdentifier");
const ensureEnumTypeResolversAreGenerated = (sourceFile, resolverFile) => {
const { variableStatement } = (0, getVariableStatementWithExpectedIdentifier_1.getVariableStatementWithExpectedIdentifier)(sourceFile, resolverFile);
if (!variableStatement) {
throw new Error('Missing variableStatement in ensureEnumTypeResolversAreGenerated.');
}
const allowedValuesMap = resolverFile.meta.allowedValues.reduce((res, allowedValue) => {
res[allowedValue] = { implemented: false };
return res;
}, {});
// Mark allowed value as implemented if found
variableStatement
.getDescendantsOfKind(ts_morph_1.SyntaxKind.PropertyAssignment)
.forEach((propertyAssignment) => {
const propertyName = propertyAssignment.getName();
if (allowedValuesMap[propertyName]) {
allowedValuesMap[propertyName].implemented = true;
}
});
// Add unimplemented allowed values
Object.entries(allowedValuesMap).forEach(([allowedValue, { implemented }]) => {
if (!implemented) {
variableStatement
.getDescendantsOfKind(ts_morph_1.SyntaxKind.ObjectLiteralExpression)[0]
.addPropertyAssignment({
name: allowedValue,
initializer: `'${allowedValue}'`,
});
resolverFile.filesystem.contentUpdated = true;
}
});
};
exports.ensureEnumTypeResolversAreGenerated = ensureEnumTypeResolversAreGenerated;
//# sourceMappingURL=ensureEnumTypeResolversAreGenerated.js.map
;