Skip to content

Commit 29e5a8e

Browse files
committed
fix: Missing import in hidden type
close: #62
1 parent 49cb251 commit 29e5a8e

File tree

4 files changed

+127
-114
lines changed

4 files changed

+127
-114
lines changed

src/handlers/input-type.ts

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -101,49 +101,57 @@ export function inputType(
101101
propertyType,
102102
isList,
103103
});
104-
classStructure.properties?.push(property);
104+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
105+
classStructure.properties!.push(property);
105106

106107
if (propertySettings) {
107108
importDeclarations.create({ ...propertySettings });
108109
}
109110

110-
if (settings?.shouldHideField({ name: inputType.name, input: true })) {
111-
importDeclarations.add('HideField', '@nestjs/graphql');
112-
property.decorators?.push({ name: 'HideField', arguments: [] });
113-
} else {
114-
ok(property.decorators);
111+
// Get graphql type
112+
let graphqlType: string;
113+
const shouldHideField = settings?.shouldHideField({
114+
name: inputType.name,
115+
input: true,
116+
});
117+
const fieldType = settings?.getFieldType({
118+
name: inputType.name,
119+
input: true,
120+
});
115121

116-
let graphqlType: string;
117-
const fieldType = settings?.getFieldType({
118-
name: inputType.name,
119-
input: true,
122+
if (fieldType && isCustomsApplicable && !shouldHideField) {
123+
graphqlType = fieldType.name;
124+
importDeclarations.create({ ...fieldType });
125+
} else {
126+
// Import property type class
127+
const graphqlImport = getGraphqlImport({
128+
sourceFile,
129+
location,
130+
typeName,
131+
getSourceFile,
120132
});
121133

122-
if (fieldType && isCustomsApplicable) {
123-
graphqlType = fieldType.name;
124-
importDeclarations.create({ ...fieldType });
125-
} else {
126-
const graphqlImport = getGraphqlImport({
127-
sourceFile,
128-
location,
129-
typeName,
130-
getSourceFile,
134+
graphqlType = graphqlImport.name;
135+
136+
if (
137+
graphqlImport.specifier &&
138+
!importDeclarations.has(graphqlImport.name) &&
139+
((graphqlImport.name !== inputType.name && !shouldHideField) ||
140+
(shouldHideField && propertyType[0] === graphqlImport.name))
141+
) {
142+
importDeclarations.set(graphqlImport.name, {
143+
namedImports: [{ name: graphqlImport.name }],
144+
moduleSpecifier: graphqlImport.specifier,
131145
});
132-
133-
graphqlType = graphqlImport.name;
134-
135-
if (
136-
graphqlImport.name !== inputType.name &&
137-
graphqlImport.specifier &&
138-
!importDeclarations.has(graphqlImport.name)
139-
) {
140-
importDeclarations.set(graphqlImport.name, {
141-
namedImports: [{ name: graphqlImport.name }],
142-
moduleSpecifier: graphqlImport.specifier,
143-
});
144-
}
145146
}
147+
}
148+
149+
ok(property.decorators, 'property.decorators is undefined');
146150

151+
if (shouldHideField) {
152+
importDeclarations.add('HideField', '@nestjs/graphql');
153+
property.decorators.push({ name: 'HideField', arguments: [] });
154+
} else {
147155
// Generate `@Field()` decorator
148156
property.decorators.push({
149157
name: 'Field',

src/handlers/output-type.ts

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -89,45 +89,51 @@ export function outputType(outputType: OutputType, args: EventArguments) {
8989
importDeclarations.create({ ...propertySettings });
9090
}
9191

92-
ok(property.decorators, 'property.decorators is undefined');
92+
// Get graphql type
93+
let graphqlType: string;
94+
const shouldHideField = settings?.shouldHideField({
95+
name: outputType.name,
96+
output: true,
97+
});
98+
const fieldType = settings?.getFieldType({
99+
name: outputType.name,
100+
output: true,
101+
});
93102

94-
if (settings?.shouldHideField({ name: outputType.name, output: true })) {
95-
importDeclarations.add('HideField', nestjsGraphql);
96-
property.decorators.push({ name: 'HideField', arguments: [] });
103+
if (fieldType && isCustomsApplicable && !shouldHideField) {
104+
graphqlType = fieldType.name;
105+
importDeclarations.create({ ...fieldType });
97106
} else {
98-
let graphqlType: string;
99-
const fieldType = settings?.getFieldType({
100-
name: outputType.name,
101-
output: true,
107+
const graphqlImport = getGraphqlImport({
108+
sourceFile,
109+
fileType,
110+
location,
111+
isId: false,
112+
typeName: outputTypeName,
113+
getSourceFile,
102114
});
103115

104-
if (fieldType && isCustomsApplicable) {
105-
graphqlType = fieldType.name;
106-
importDeclarations.create({ ...fieldType });
107-
} else {
108-
const graphqlImport = getGraphqlImport({
109-
sourceFile,
110-
fileType,
111-
location,
112-
isId: false,
113-
typeName: outputTypeName,
114-
getSourceFile,
116+
graphqlType = graphqlImport.name;
117+
118+
if (
119+
graphqlImport.specifier &&
120+
!importDeclarations.has(graphqlImport.name) &&
121+
((graphqlImport.name !== outputType.name && !shouldHideField) ||
122+
(shouldHideField && propertyType[0] === graphqlImport.name))
123+
) {
124+
importDeclarations.set(graphqlImport.name, {
125+
namedImports: [{ name: graphqlImport.name }],
126+
moduleSpecifier: graphqlImport.specifier,
115127
});
116-
117-
graphqlType = graphqlImport.name;
118-
119-
if (
120-
graphqlImport.name !== outputType.name &&
121-
graphqlImport.specifier &&
122-
!importDeclarations.has(graphqlImport.name)
123-
) {
124-
importDeclarations.set(graphqlImport.name, {
125-
namedImports: [{ name: graphqlImport.name }],
126-
moduleSpecifier: graphqlImport.specifier,
127-
});
128-
}
129128
}
129+
}
130130

131+
ok(property.decorators, 'property.decorators is undefined');
132+
133+
if (shouldHideField) {
134+
importDeclarations.add('HideField', nestjsGraphql);
135+
property.decorators.push({ name: 'HideField', arguments: [] });
136+
} else {
131137
// Generate `@Field()` decorator
132138
property.decorators.push({
133139
name: 'Field',

src/helpers/generate-import.ts

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/test/generate.spec.ts

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,6 +1406,31 @@ describe('hide field', () => {
14061406
);
14071407
});
14081408
});
1409+
1410+
it('hidden relations result in un-imported types', async () => {
1411+
({ project, sourceFiles } = await testGenerate({
1412+
schema: `
1413+
model User {
1414+
id String @id @default(uuid())
1415+
userApiKey UserApiKey[]
1416+
}
1417+
1418+
model UserApiKey {
1419+
id String @id @default(uuid())
1420+
userId String
1421+
/// @HideField({ input: true })
1422+
user User @relation(fields: [userId], references: [id])
1423+
}
1424+
`,
1425+
options: [`outputFilePattern = "{name}.{type}.ts"`],
1426+
}));
1427+
setSourceFile('user-api-key-where.input.ts');
1428+
expect(p('user')?.type).toEqual('UserRelationFilter');
1429+
expect(imports).toContainEqual({
1430+
name: 'UserRelationFilter',
1431+
specifier: './user-relation-filter.input',
1432+
});
1433+
});
14091434
});
14101435

14111436
it('model with prisma keyword output', async () => {
@@ -2167,7 +2192,6 @@ describe('property type', () => {
21672192
it('user-create.input', () => {
21682193
setSourceFile('user-create.input.ts');
21692194
expect(p('profile')?.type).toEqual('JsonObject');
2170-
21712195
});
21722196

21732197
it('should use default scalar type in user-update-many-mutation.input', () => {
@@ -2182,9 +2206,10 @@ describe('property type', () => {
21822206
});
21832207
});
21842208

2185-
it('hidefield on groupby', async () => {
2186-
({ project, sourceFiles } = await testGenerate({
2187-
schema: `
2209+
describe('hidefield on groupby output', () => {
2210+
before(async () => {
2211+
({ project, sourceFiles } = await testGenerate({
2212+
schema: `
21882213
model User {
21892214
id Int @id
21902215
/// @HideField({ match: '*GroupBy' })
@@ -2193,19 +2218,26 @@ it('hidefield on groupby', async () => {
21932218
profile Json
21942219
}
21952220
`,
2196-
options: [`outputFilePattern = "{name}.{type}.ts"`],
2197-
}));
2198-
setSourceFile('user-group-by.output.ts');
2199-
expect(imports).not.toContainEqual(
2200-
expect.objectContaining({
2201-
name: 'GraphQLJSONObject',
2202-
}),
2203-
);
2204-
expect(imports).not.toContainEqual(
2205-
expect.objectContaining({
2206-
name: 'GraphQLJSON',
2207-
}),
2208-
);
2221+
options: [`outputFilePattern = "{name}.{type}.ts"`],
2222+
}));
2223+
setSourceFile('user-group-by.output.ts');
2224+
});
2225+
2226+
it('no graphqljsonobject', () => {
2227+
expect(imports).not.toContainEqual(
2228+
expect.objectContaining({
2229+
name: 'GraphQLJSONObject',
2230+
}),
2231+
);
2232+
});
2233+
2234+
it('no graphqljson', () => {
2235+
expect(imports).not.toContainEqual(
2236+
expect.objectContaining({
2237+
name: 'GraphQLJSON',
2238+
}),
2239+
);
2240+
});
22092241
});
22102242

22112243
describe('non list optional properties should be nullable', () => {

0 commit comments

Comments
 (0)