Skip to content

Commit

Permalink
feat: do not register enums more than once
Browse files Browse the repository at this point in the history
  • Loading branch information
rintoj committed Jun 7, 2024
1 parent 07de328 commit 2f20ea9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/gql/enum/enum-generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,30 @@ describe('generateModel', () => {
`),
)
})

test('should not register twice', async () => {
const output = await generate(
'user.enum.ts',
`
import { registerEnumType } from '@nestjs/graphql'
enum Status {
ACTIVE = 'ACTIVE',
DELETED = 'DELETED',
}
registerEnumType(Status, { name: 'Status' })
`,
)
expect(toParsedOutput(output)).toBe(
toParsedOutput(`
import { registerEnumType } from '@nestjs/graphql'
enum Status {
ACTIVE = 'ACTIVE',
DELETED = 'DELETED',
}
registerEnumType(Status, { name: 'Status' })
`),
)
})
})
8 changes: 8 additions & 0 deletions src/gql/enum/enum-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ export async function generateEnum(sourceFile: ts.SourceFile): Promise<ts.Source
sourceFile,
node => {
if (ts.isEnumDeclaration(node)) return processEnumDeclaration(node, context)
if (
ts.isExpressionStatement(node) &&
ts.isCallExpression(node.expression) &&
ts.isIdentifier(node.expression.expression) &&
node.expression.expression.text === 'registerEnumType'
) {
return
}
return node
},
undefined,
Expand Down

0 comments on commit 2f20ea9

Please sign in to comment.