Skip to content

Commit 54571d2

Browse files
committed
fix: Hide field for model type
Fix: unlight#14
1 parent 92d9857 commit 54571d2

File tree

4 files changed

+106
-67
lines changed

4 files changed

+106
-67
lines changed

src/generate.spec.ts

Lines changed: 68 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,9 +1298,10 @@ describe('export all from index', () => {
12981298
});
12991299

13001300
describe('hide field', () => {
1301-
before(async () => {
1302-
await testGenerate({
1303-
schema: `
1301+
describe('scalar field', () => {
1302+
before(async () => {
1303+
await testGenerate({
1304+
schema: `
13041305
model User {
13051306
id String @id
13061307
/// @TypeGraphQL.omit(output: true)
@@ -1311,40 +1312,79 @@ describe('hide field', () => {
13111312
password2 String
13121313
}
13131314
`,
1314-
options: [],
1315+
options: [],
1316+
});
1317+
// const filePaths = sourceFiles.map(s => s.getFilePath());
13151318
});
1316-
// const filePaths = sourceFiles.map(s => s.getFilePath());
1317-
});
13181319

1319-
describe('model', () => {
1320-
before(() => {
1321-
sourceFile = project.getSourceFile(s =>
1322-
s.getFilePath().endsWith('/user.model.ts'),
1323-
)!;
1324-
});
1320+
describe('model', () => {
1321+
before(() => {
1322+
sourceFile = project.getSourceFile(s =>
1323+
s.getFilePath().endsWith('/user.model.ts'),
1324+
)!;
1325+
});
13251326

1326-
// it('^', () => console.log(sourceFile.getText()));
1327+
// it('^', () => console.log(sourceFile.getText()));
13271328

1328-
it('TypeGraphQL omit should hide password1', () => {
1329-
expect(d('password1')?.name).toBe('HideField');
1330-
expect(d('password1')?.arguments).toEqual([]);
1329+
it('TypeGraphQL omit should hide password1', () => {
1330+
expect(d('password1')?.name).toBe('HideField');
1331+
expect(d('password1')?.arguments).toEqual([]);
1332+
});
1333+
1334+
it('HideField should hide field', () => {
1335+
expect(d('password2')?.name).toBe('HideField');
1336+
expect(d('password2')?.arguments).toEqual([]);
1337+
});
13311338
});
13321339

1333-
it('HideField should hide field', () => {
1334-
expect(d('password2')?.name).toBe('HideField');
1335-
expect(d('password2')?.arguments).toEqual([]);
1340+
describe('other outputs', () => {
1341+
it('user-max-aggregate', () => {
1342+
sourceFile = project.getSourceFile(s =>
1343+
s.getFilePath().endsWith('/user-max-aggregate.output.ts'),
1344+
)!;
1345+
expect(d('password1')?.name).toBe('HideField');
1346+
expect(d('password1')?.arguments).toEqual([]);
1347+
expect(d('password2')?.name).toBe('HideField');
1348+
expect(d('password2')?.arguments).toEqual([]);
1349+
});
13361350
});
13371351
});
13381352

1339-
describe('other outputs', () => {
1340-
it('user-max-aggregate', () => {
1341-
sourceFile = project.getSourceFile(s =>
1342-
s.getFilePath().endsWith('/user-max-aggregate.output.ts'),
1343-
)!;
1344-
expect(d('password1')?.name).toBe('HideField');
1345-
expect(d('password1')?.arguments).toEqual([]);
1346-
expect(d('password2')?.name).toBe('HideField');
1347-
expect(d('password2')?.arguments).toEqual([]);
1353+
describe('hide on non scalar', () => {
1354+
before(async () => {
1355+
await testGenerate({
1356+
schema: `
1357+
model User {
1358+
id String @id
1359+
/// @HideField()
1360+
secret Secret @relation(fields: [secretId], references: [id])
1361+
secretId String
1362+
}
1363+
1364+
model Secret {
1365+
id String @id
1366+
users User[]
1367+
}
1368+
`,
1369+
options: [],
1370+
});
1371+
});
1372+
1373+
describe('model', () => {
1374+
before(() => {
1375+
sourceFile = project.getSourceFile(s =>
1376+
s.getFilePath().endsWith('/user.model.ts'),
1377+
)!;
1378+
});
1379+
1380+
it('type should be imported', () => {
1381+
const imports = getImportDeclarations(sourceFile);
1382+
expect(imports).toContainEqual(
1383+
expect.objectContaining({ name: 'Secret' }),
1384+
);
1385+
});
1386+
1387+
// it('^', () => console.log(sourceFile.getText()));
13481388
});
13491389
});
13501390
});

src/handlers/model-output-type.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,6 @@ export function modelOutputType(outputType: OutputType, args: EventArguments) {
7777
isList,
7878
});
7979

80-
if (fieldMeta?.hideOutput) {
81-
generateImport({
82-
sourceFile,
83-
name: 'HideField',
84-
moduleSpecifier: '@nestjs/graphql',
85-
});
86-
propertyDeclaration.addDecorator({ name: 'HideField()' });
87-
continue;
88-
}
89-
9080
const graphqlType =
9181
customType?.graphqlType ??
9282
getGraphqlType({
@@ -122,14 +112,23 @@ export function modelOutputType(outputType: OutputType, args: EventArguments) {
122112
});
123113
}
124114

125-
generateDecorator({
126-
propertyDeclaration,
127-
graphqlType,
128-
isList,
129-
isNullable: field.isNullable,
130-
defaultValue: modelField?.default,
131-
description: modelField?.documentation,
132-
});
115+
if (fieldMeta?.hideOutput) {
116+
generateImport({
117+
sourceFile,
118+
name: 'HideField',
119+
moduleSpecifier: '@nestjs/graphql',
120+
});
121+
propertyDeclaration.addDecorator({ name: 'HideField()' });
122+
} else {
123+
generateDecorator({
124+
propertyDeclaration,
125+
graphqlType,
126+
isList,
127+
isNullable: field.isNullable,
128+
defaultValue: modelField?.default,
129+
description: modelField?.documentation,
130+
});
131+
}
133132
}
134133

135134
// Check re-export, comment generated class if found

src/handlers/output-type.ts

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,6 @@ export function outputType(outputType: OutputType, args: EventArguments) {
107107

108108
classStructure.properties?.push(property);
109109

110-
if (fieldMeta?.hideOutput) {
111-
importDeclarations.add('HideField', {
112-
namedImports: [{ name: 'HideField' }],
113-
moduleSpecifier: '@nestjs/graphql',
114-
});
115-
property.decorators?.push({ name: 'HideField', arguments: [] });
116-
continue;
117-
}
118-
119110
const graphqlType =
120111
customType?.graphqlType ??
121112
getGraphqlType({
@@ -158,16 +149,24 @@ export function outputType(outputType: OutputType, args: EventArguments) {
158149
});
159150
}
160151

161-
// Generate `@Field()` decorator
162-
property.decorators?.push({
163-
name: 'Field',
164-
arguments: [
165-
`() => ${isList ? `[${graphqlType}]` : graphqlType}`,
166-
JSON5.stringify({
167-
nullable: Boolean(field.isNullable),
168-
}),
169-
],
170-
});
152+
if (fieldMeta?.hideOutput) {
153+
importDeclarations.add('HideField', {
154+
namedImports: [{ name: 'HideField' }],
155+
moduleSpecifier: '@nestjs/graphql',
156+
});
157+
property.decorators?.push({ name: 'HideField', arguments: [] });
158+
} else {
159+
// Generate `@Field()` decorator
160+
property.decorators?.push({
161+
name: 'Field',
162+
arguments: [
163+
`() => ${isList ? `[${graphqlType}]` : graphqlType}`,
164+
JSON5.stringify({
165+
nullable: Boolean(field.isNullable),
166+
}),
167+
],
168+
});
169+
}
171170
}
172171

173172
sourceFile.set({

src/helpers/property-structure.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { OptionalKind, PropertyDeclarationStructure } from 'ts-morph';
1+
import { PropertyDeclarationStructure, StructureKind } from 'ts-morph';
22

33
/**
44
* Get property structure (field) for class.
@@ -8,13 +8,14 @@ export function propertyStructure(args: {
88
isList: boolean;
99
name: string;
1010
isNullable?: boolean;
11-
}): OptionalKind<PropertyDeclarationStructure> {
11+
}): PropertyDeclarationStructure {
1212
const { isNullable, propertyType, name, isList } = args;
1313
const type = propertyType
1414
.map(type => (isList ? `Array<${type}>` : type))
1515
.join(' | ');
1616

1717
return {
18+
kind: StructureKind.Property,
1819
name,
1920
type,
2021
hasQuestionToken: isNullable,

0 commit comments

Comments
 (0)