Skip to content

Commit

Permalink
fix: the test case around ...args to parent
Browse files Browse the repository at this point in the history
  • Loading branch information
rintoj committed Jun 6, 2024
1 parent 19b02fa commit d4610f4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
6 changes: 4 additions & 2 deletions src/gql/gql-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,12 +385,14 @@ export function createPromiseType(...types: string[]) {
export function getType(node: ts.PropertyDeclaration | ts.MethodDeclaration) {
const typeFromDecorator = getTypeFromDecorator(node, 'Field')
if (typeFromDecorator) return typeFromDecorator
if (getName(node) === 'id') return 'ID'
const [type, secondType] = Array.from(new Set(getAllTypes(node.type))).filter(
i => !['null', 'undefined', 'Promise'].includes(i),
)
if (secondType) throw new Error('Return type can not be a union type.')
if (type === 'string') return
if (type === 'number') return 'Int'
if (type) return type
if (getName(node) === 'id') return 'ID'
}

export function addNullability<T extends ts.PropertyDeclaration | ts.MethodDeclaration>(
Expand Down Expand Up @@ -462,7 +464,7 @@ export function createFieldDecorator(
factory.createIdentifier(type),
),
)
if (type === 'ID') context.imports.push(createImport('@nestjs/graphql', 'ID'))
if (['ID', 'INT'].includes(type)) context.imports.push(createImport('@nestjs/graphql', type))
}

if (isNullable(node) || !!comment) {
Expand Down
9 changes: 6 additions & 3 deletions src/gql/resolver/resolver-generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@ describe('generateResolver', () => {
)
expect(toParsedOutput(output)).toBe(
toParsedOutput(`
import { Query, Resolver } from '@nestjs/graphql'
import { Parent, ResolveField, Resolver } from '@nestjs/graphql'
@Resolver(() => UserModel)
export class UserResolver {
@Query(() => User)
createdBy(): Promise<User> {}
@ResolveField(() => User)
createdBy(
@Parent()
parent: User,
): Promise<User> {}
}
`),
)
Expand Down
24 changes: 20 additions & 4 deletions src/gql/resolver/resolver-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
getAllTypes,
getName,
getParameterType,
getType,
getTypeFromDecorator,
hasDecorator,
hasImplementationByName,
Expand All @@ -41,7 +42,23 @@ function processParameters(
...node,
parameters: toNonNullArray(
node.parameters.map(parameter => {
if (!!parameter.dotDotDotToken) return
if (!!parameter.dotDotDotToken) {
const type = getType(node)
return addDecorator(
factory.createParameterDeclaration(
undefined,
undefined,
factory.createIdentifier('parent'),
undefined,
factory.createTypeReferenceNode(
factory.createIdentifier(parentType ?? type ?? 'unknown'),
undefined,
),
undefined,
),
createParentDecorator(context),
)
}
const name = getName(parameter)
if (name === 'parent') {
return addDecorator(
Expand Down Expand Up @@ -160,9 +177,8 @@ function getTypeNameFromParameters(node: ts.ClassDeclaration) {
function getFieldDecoratorType(node: ts.Node) {
if (
hasDecorator(node, 'ResolveField') ||
((ts.isFunctionTypeNode(node)
? hasParameter(node.type, 'parent')
: hasParameter(node, 'parent')) &&
((hasParameter(ts.isFunctionTypeNode(node) ? node.type : node, 'parent') ||
hasParameter(ts.isFunctionTypeNode(node) ? node.type : node, 'args')) &&
!hasDecorator(node, 'Query') &&
!hasDecorator(node, 'Mutation'))
) {
Expand Down

0 comments on commit d4610f4

Please sign in to comment.