Skip to content

Commit

Permalink
fix(hide field): Missing import of enum type
Browse files Browse the repository at this point in the history
close: unlight#73
  • Loading branch information
unlight committed Nov 25, 2021
1 parent fa8e7fa commit b067142
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/handlers/input-type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ok } from 'assert';
import JSON5 from 'json5';
import { castArray } from 'lodash';
import { castArray, last } from 'lodash';
import pupa from 'pupa';
import { ClassDeclarationStructure, StructureKind } from 'ts-morph';

Expand Down Expand Up @@ -132,12 +132,16 @@ export function inputType(
});

graphqlType = graphqlImport.name;
let referenceName = propertyType[0];
if (location === 'enumTypes') {
referenceName = last(referenceName.split(' ')) as string;
}

if (
graphqlImport.specifier &&
!importDeclarations.has(graphqlImport.name) &&
((graphqlImport.name !== inputType.name && !shouldHideField) ||
(shouldHideField && propertyType[0] === graphqlImport.name))
(shouldHideField && referenceName === graphqlImport.name))
) {
importDeclarations.set(graphqlImport.name, {
namedImports: [{ name: graphqlImport.name }],
Expand Down
8 changes: 6 additions & 2 deletions src/handlers/output-type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ok } from 'assert';
import JSON5 from 'json5';
import { castArray } from 'lodash';
import { castArray, last } from 'lodash';
import { ClassDeclarationStructure, StructureKind } from 'ts-morph';

import { getGraphqlImport } from '../helpers/get-graphql-import';
Expand Down Expand Up @@ -114,12 +114,16 @@ export function outputType(outputType: OutputType, args: EventArguments) {
});

graphqlType = graphqlImport.name;
let referenceName = propertyType[0];
if (location === 'enumTypes') {
referenceName = last(referenceName.split(' ')) as string;
}

if (
graphqlImport.specifier &&
!importDeclarations.has(graphqlImport.name) &&
((graphqlImport.name !== outputType.name && !shouldHideField) ||
(shouldHideField && propertyType[0] === graphqlImport.name))
(shouldHideField && referenceName === graphqlImport.name))
) {
importDeclarations.set(graphqlImport.name, {
namedImports: [{ name: graphqlImport.name }],
Expand Down
44 changes: 44 additions & 0 deletions src/test/generate.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import expect from 'expect';
import JSON5 from 'json5';
import { trim } from 'lodash';
import { async } from 'rxjs';
import {
ClassDeclaration,
EnumDeclarationStructure,
Expand Down Expand Up @@ -1421,6 +1422,49 @@ describe('hide field', () => {
specifier: './user-relation-filter.input',
});
});

describe('enums are not imported in classes when decorated', () => {
before(async () => {
({ project, sourceFiles } = await testGenerate({
schema: `
model User {
id Int @id
/// @HideField({ input: true, output: true })
role Role
}
enum Role {
USER
ADMIN
}
`,
options: [`outputFilePattern = "{name}.{type}.ts"`],
}));
});

it('check files', () => {
[
'user-group-by.output.ts',
'user-max-aggregate.output.ts',
'user-min-aggregate.output.ts',
'user-create-many.input.ts',
'user.model.ts',
].forEach(() => {
const s = testSourceFile({
project,
file: 'user-group-by.output.ts',
property: 'role',
});

expect(s.namedImports).toContainEqual({
name: 'Role',
specifier: './role.enum',
});
expect(s.propertyDecorators).toContainEqual(
expect.objectContaining({ name: 'HideField' }),
);
});
});
});
});

it('model with prisma keyword output', async () => {
Expand Down

0 comments on commit b067142

Please sign in to comment.