Skip to content

Commit

Permalink
fix(compatibility): - Optimized enum value processing to match Prisma…
Browse files Browse the repository at this point in the history
…'s expected documentation structure.

BREAKING CHANGE:
This change ensures that enum values are processed in a way that aligns with Prisma's engine,
maintaining consistency in documentation handling and filtering out unnecessary entries.- Added
specific handling for `@deprecated` documentation, mapping it to - Updated the `registerEnum`
function to properly filter out empty entries in the `valuesMap` based on enum value documentation.-
Ensured that enum values without `description` or `deprecationReason` are excluded from the
`valuesMap` when registering with `registerEnumType`.the `deprecationReason` field in line with
Prisma Engine behavior.- Improved compatibility with Prisma's enum value documentation format by
ensuring only relevant metadata is included in the GraphQL schema.
  • Loading branch information
javan-b committed Feb 21, 2025
1 parent 074c2de commit 213c8ad
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
8 changes: 8 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,19 @@ model Comment {
articleId String?
}

/// user access control
enum Role {
/// default user access control
USER
/// have full access control
NINGA
/// @deprecated Use USER instead
ADMIN
REVIEWER // no comment and won't show in registerenum valuemaps
}

model Profile {
/// @deprecated Use new name instead
id Int @id @default(autoincrement())
user User @relation(fields: [userId], references: [id])
userId String @unique
Expand Down
44 changes: 38 additions & 6 deletions src/handlers/register-enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import { ImportDeclarationMap } from '../helpers/import-declaration-map';
import { EventArguments, SchemaEnum } from '../types';

export function registerEnum(enumType: SchemaEnum, args: EventArguments) {
const { getSourceFile, enums, config } = args;
const { config, enums, getSourceFile } = args;

if (!config.emitBlocks.prismaEnums && !enums[enumType.name]) return;

const dataModelEnum = enums[enumType.name];
const enumTypesData = dataModelEnum?.values || [];
console.log('enumTypesData', enumTypesData);
const sourceFile = getSourceFile({
name: enumType.name,
type: 'enum',
Expand All @@ -17,18 +19,48 @@ export function registerEnum(enumType: SchemaEnum, args: EventArguments) {
const importDeclarations = new ImportDeclarationMap();

importDeclarations.set('registerEnumType', {
namedImports: [{ name: 'registerEnumType' }],
moduleSpecifier: '@nestjs/graphql',
namedImports: [{ name: 'registerEnumType' }],
});

// Create valuesMap based on documentation
const valuesMap = Object.fromEntries(
enumTypesData.map(({ name, documentation }) => {
let entry = {};
if (documentation) {
if (documentation.startsWith('@deprecated')) {
entry = {
deprecationReason: documentation.slice(11).trim(), // Extract deprecation reason
};
} else {
entry = {
description: documentation, // Use the documentation as description
};
}
}
return [name, entry]; // Return entry, even if empty
}),
);

// Filter out empty entries (those that don't have description or deprecationReason)
const filteredValuesMap = Object.fromEntries(
Object.entries(valuesMap).filter(([key, value]) => Object.keys(value).length > 0),
);

// Format valuesMap for the final output
const formattedValuesMap = JSON.stringify(filteredValuesMap, null, 2).replace(
/"([^"]+)":/g,
'$1:',
);

const enumStructure: EnumDeclarationStructure = {
kind: StructureKind.Enum,
isExported: true,
name: enumType.name,
kind: StructureKind.Enum,
members: enumType.values.map(v => ({
name: v,
initializer: JSON.stringify(v),
name: v,
})),
name: enumType.name,
};

sourceFile.set({
Expand All @@ -38,7 +70,7 @@ export function registerEnum(enumType: SchemaEnum, args: EventArguments) {
'\n',
`registerEnumType(${enumType.name}, { name: '${
enumType.name
}', description: ${JSON.stringify(dataModelEnum?.documentation)} })`,
}', description: ${JSON.stringify(dataModelEnum?.documentation)}, valuesMap: ${formattedValuesMap} })`,
],
});
}

0 comments on commit 213c8ad

Please sign in to comment.