Skip to content

Commit b2e970e

Browse files
fix(graphql): plugin crashes when import * as is used #2441
1 parent 4057b87 commit b2e970e

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

packages/graphql/lib/plugin/utils/ast-utils.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,26 @@ export function getDecoratorName(decorator: Decorator) {
149149
const isDecoratorFactory =
150150
decorator.expression.kind === SyntaxKind.CallExpression;
151151
if (isDecoratorFactory) {
152-
const callExpression = decorator.expression;
153-
const identifier = (callExpression as CallExpression)
154-
.expression as Identifier;
155-
if (isDynamicallyAdded(identifier)) {
156-
return undefined;
152+
const callExpression = decorator.expression as ts.CallExpression;
153+
154+
if (
155+
callExpression.expression?.kind === ts.SyntaxKind.PropertyAccessExpression
156+
) {
157+
// When "import * as _" is used
158+
const propertyAccessExpression =
159+
callExpression.expression as PropertyAccessExpression;
160+
return getIdentifierFromName(propertyAccessExpression.name).getText();
161+
}
162+
163+
if (callExpression.kind === ts.SyntaxKind.CallExpression) {
164+
const identifier = (callExpression as CallExpression)
165+
.expression as Identifier;
166+
167+
if (isDynamicallyAdded(identifier)) {
168+
return undefined;
169+
}
170+
return getIdentifierFromName(identifier).getText();
157171
}
158-
return getIdentifierFromName(
159-
(callExpression as CallExpression).expression,
160-
).getText();
161172
}
162173
return getIdentifierFromName(decorator.expression).getText();
163174
}

packages/graphql/tests/plugin/fixtures/create-cat.dto.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
export const createCatDtoText = `
2+
import * as gql from "@nestjs/graphql";
3+
24
enum Status {
35
ENABLED,
46
DISABLED
@@ -12,6 +14,7 @@ interface Node {
1214
id: number;
1315
}
1416
17+
@gql.InputType()
1518
@ObjectType()
1619
export class CreateCatDto {
1720
name: string;
@@ -69,7 +72,8 @@ export class CreateCatArgs {
6972
}
7073
`;
7174

72-
export const createCatDtoTextTranspiled = `var Status;
75+
export const createCatDtoTextTranspiled = `import * as gql from "@nestjs/graphql";
76+
var Status;
7377
(function (Status) {
7478
Status[Status["ENABLED"] = 0] = "ENABLED";
7579
Status[Status["DISABLED"] = 1] = "DISABLED";
@@ -91,6 +95,7 @@ __decorate([
9195
HideField()
9296
], CreateCatDto.prototype, "hidden", void 0);
9397
CreateCatDto = __decorate([
98+
gql.InputType(),
9499
ObjectType()
95100
], CreateCatDto);
96101
export { CreateCatDto };

0 commit comments

Comments
 (0)