Skip to content

Commit

Permalink
fix(other): Ignore @HideField() for output count fields
Browse files Browse the repository at this point in the history
close: unlight#33
  • Loading branch information
unlight committed Jun 5, 2021
1 parent 0fbdda6 commit ce3eec2
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 39 deletions.
40 changes: 23 additions & 17 deletions src/handlers/output-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ export function outputType(outputType: OutputType, args: EventArguments) {
const fileType = 'output';
const modelName = getModelName(outputType.name) || '';
const model = models.get(modelName);
const shouldEmitAggregateOutput =
const isAggregateOutput =
model &&
/(Count|Avg|Sum|Min|Max)AggregateOutputType$/.test(outputType.name) &&
String(outputType.name).startsWith(model.name);
const isCountOutput =
model?.name && outputType.name === `${model.name}CountOutputType`;
// Get rid of bogus suffixes
outputType.name = getOutputTypeName(outputType.name);

if (shouldEmitAggregateOutput) {
if (isAggregateOutput) {
eventEmitter.emitSync('AggregateOutput', { ...args, outputType });
}

Expand Down Expand Up @@ -55,10 +57,12 @@ export function outputType(outputType: OutputType, args: EventArguments) {
for (const field of outputType.fields) {
const { location, isList, type } = field.outputType;
const outputTypeName = getOutputTypeName(String(type));
const settings = model && fieldSettings.get(model.name)?.get(field.name);
const settings = isCountOutput
? undefined
: model && fieldSettings.get(model.name)?.get(field.name);
const propertySettings = settings?.getPropertyType();
// const isCustomsApplicable =
// outputTypeName === model?.fields.find(f => f.name === field.name)?.type;
const isCustomsApplicable =
outputTypeName === model?.fields.find(f => f.name === field.name)?.type;
// todo: remove
const customType = config.types[outputTypeName];

Expand Down Expand Up @@ -130,19 +134,21 @@ export function outputType(outputType: OutputType, args: EventArguments) {
],
});

for (const options of settings || []) {
if (!options.output || options.kind !== 'Decorator') {
continue;
if (isCustomsApplicable) {
for (const options of settings || []) {
if (!options.output || options.kind !== 'Decorator') {
continue;
}
property.decorators?.push({
name: options.name,
arguments: options.arguments,
});
ok(
options.from,
"Missed 'from' part in configuration or field setting",
);
importDeclarations.create(options);
}
property.decorators?.push({
name: options.name,
arguments: options.arguments,
});
ok(
options.from,
"Missed 'from' part in configuration or field setting",
);
importDeclarations.create(options);
}
}

Expand Down
56 changes: 36 additions & 20 deletions src/test/generate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1038,10 +1038,6 @@ describe('get rid of atomic number operations', () => {
before(() => {
setSourceFile('user-updatefriends.input.ts');
});

// it('', () => {
// console.log(sourceText);
// });
});

describe('date field files', () => {
Expand Down Expand Up @@ -1221,25 +1217,23 @@ describe('hide field', () => {
before(async () => {
({ project, sourceFiles } = await testGenerate({
schema: `
model User {
id String @id
/// Password1
/// @TypeGraphQL.omit(output: true)
password1 String
/// Password2
/// @HideField()
password2 String
}
model User {
id String @id
/// Password1
/// @TypeGraphQL.omit(output: true)
password1 String
/// Password2
/// @HideField()
password2 String
}
`,
options: [],
}));
});

describe('model', () => {
before(() => {
sourceFile = project.getSourceFile(s =>
s.getFilePath().endsWith('/user.model.ts'),
)!;
setSourceFile('/user.model.ts');
});

// it('^', () => console.log(sourceFile.getText()));
Expand All @@ -1257,9 +1251,7 @@ describe('hide field', () => {

describe('other outputs', () => {
it('user-max-aggregate', () => {
sourceFile = project.getSourceFile(s =>
s.getFilePath().endsWith('/user-max-aggregate.output.ts'),
)!;
setSourceFile('/user-max-aggregate.output.ts');
expect(d('password1')?.name).toBe('HideField');
expect(d('password1')?.arguments).toEqual([]);
expect(d('password2')?.name).toBe('HideField');
Expand All @@ -1278,7 +1270,6 @@ describe('hide field', () => {
secret Secret @relation(fields: [secretId], references: [id])
secretId String
}
model Secret {
id String @id
users User[]
Expand Down Expand Up @@ -1792,3 +1783,28 @@ describe('model autoincrement int', () => {
expect(f).toBeUndefined();
});
});

describe('output without fields', () => {
before(async () => {
({ project, sourceFiles } = await testGenerate({
schema: `
model Comment {
id String @id @default(cuid())
dummy Dummy @relation(fields: [dummyId], references: [id])
dummyId String
}
model Dummy {
id String @id
/// @HideField({ input: true, output: true })
comments Comment[]
}
`,
options: [`outputFilePattern = "{name}.{type}.ts"`],
}));
});

it('count output', () => {
setSourceFile('dummy-count.output.ts');
expect(t('comments')).toEqual('() => Int');
});
});
13 changes: 11 additions & 2 deletions src/test/test-generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export async function testGenerate(args: {

ok(project, 'Project is not defined');
const sourceFiles = project.getSourceFiles();
const emptyFieldsFiles: string[] = [];

for (const sourceFile of sourceFiles) {
const filePath = sourceFile.getFilePath();
Expand All @@ -77,7 +78,7 @@ export async function testGenerate(args: {
.map(s => s.getBaseName())
.join(', ')}`;
}
throw message;
throw new Error(message);
}
const imports = sourceFile
.getImportDeclarations()
Expand All @@ -91,8 +92,16 @@ export async function testGenerate(args: {
].filter(Boolean);
});
if (uniq(imports).length !== imports.length) {
throw `Duplicated import in ${filePath}: ${imports.toString()}`;
throw new Error(`Duplicated import in ${filePath}: ${imports.toString()}`);
}
// Find classes without @Field() (must define one or more fields)
const properties = sourceFile.getClass(() => true)?.getProperties();
if (properties && !properties.some(p => p.getDecorator('Field'))) {
emptyFieldsFiles.push(sourceFile.getBaseName());
}
}
if (emptyFieldsFiles.length > 0) {
throw new Error(`No defined fields in ${emptyFieldsFiles.join(', ')}`);
}

return { project, sourceFiles };
Expand Down

0 comments on commit ce3eec2

Please sign in to comment.