Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(types): Type check Field defaultValue #2330

Merged
merged 1 commit into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 25 additions & 14 deletions packages/graphql/lib/decorators/field.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ import { Type } from '@nestjs/common';
import { isFunction } from '@nestjs/common/utils/shared.utils';
import { Complexity, FieldMiddleware } from '../interfaces';
import { BaseTypeOptions } from '../interfaces/base-type-options.interface';
import { ReturnTypeFunc } from '../interfaces/return-type-func.interface';
import {
GqlTypeReference,
ReturnTypeFunc,
ReturnTypeFuncValue,
} from '../interfaces/return-type-func.interface';
import { LazyMetadataStorage } from '../schema-builder/storages/lazy-metadata.storage';
import { TypeMetadataStorage } from '../schema-builder/storages/type-metadata.storage';
import { reflectTypeFromMetadata } from '../utils/reflection.utilts';

/**
* Interface defining options that can be passed to `@Field()` decorator.
*/
export interface FieldOptions extends BaseTypeOptions {
export interface FieldOptions<T = any> extends BaseTypeOptions<T> {
/**
* Name of the field.
*/
Expand All @@ -40,6 +44,12 @@ export interface FieldOptions extends BaseTypeOptions {
middleware?: FieldMiddleware[];
}

type FieldOptionsExtractor<T> = T extends [GqlTypeReference<infer P>]
? FieldOptions<P[]>
: T extends GqlTypeReference<infer P>
? FieldOptions<P>
: never;

/**
* @Field() decorator is used to mark a specific class property as a GraphQL field.
* Only properties decorated with this decorator will be defined in the schema.
Expand All @@ -49,24 +59,25 @@ export function Field(): PropertyDecorator & MethodDecorator;
* @Field() decorator is used to mark a specific class property as a GraphQL field.
* Only properties decorated with this decorator will be defined in the schema.
*/
export function Field(
options: FieldOptions,
export function Field<T extends ReturnTypeFuncValue>(
options: FieldOptionsExtractor<T>,
): PropertyDecorator & MethodDecorator;
/**
* @Field() decorator is used to mark a specific class property as a GraphQL field.
* Only properties decorated with this decorator will be defined in the schema.
*/
export function Field(
returnTypeFunction?: ReturnTypeFunc,
options?: FieldOptions,
export function Field<T extends ReturnTypeFuncValue>(
returnTypeFunction?: ReturnTypeFunc<T>,
options?: FieldOptionsExtractor<T>,
): PropertyDecorator & MethodDecorator;

/**
* @Field() decorator is used to mark a specific class property as a GraphQL field.
* Only properties decorated with this decorator will be defined in the schema.
*/
export function Field(
typeOrOptions?: ReturnTypeFunc | FieldOptions,
fieldOptions?: FieldOptions,
export function Field<T extends ReturnTypeFuncValue>(
typeOrOptions?: ReturnTypeFunc<T> | FieldOptionsExtractor<T>,
fieldOptions?: FieldOptionsExtractor<T>,
): PropertyDecorator & MethodDecorator {
return (
prototype: Object,
Expand All @@ -83,9 +94,9 @@ export function Field(
};
}

export function addFieldMetadata(
typeOrOptions: ReturnTypeFunc | FieldOptions,
fieldOptions: FieldOptions,
export function addFieldMetadata<T extends ReturnTypeFuncValue>(
typeOrOptions: ReturnTypeFunc<T> | FieldOptionsExtractor<T>,
fieldOptions: FieldOptionsExtractor<T>,
prototype: Object,
propertyKey?: string,
descriptor?: TypedPropertyDescriptor<any>,
Expand All @@ -103,7 +114,7 @@ export function addFieldMetadata(
metadataKey: isResolverMethod ? 'design:returntype' : 'design:type',
prototype,
propertyKey,
explicitTypeFn: typeFunc as ReturnTypeFunc,
explicitTypeFn: typeFunc as ReturnTypeFunc<T>,
typeOptions: options,
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
export type NullableList = 'items' | 'itemsAndList';
export interface BaseTypeOptions {
export interface BaseTypeOptions<T = any> {
/**
* Determines whether field/argument/etc is nullable.
*/
nullable?: boolean | NullableList;
/**
* Default value.
*/
defaultValue?: any;
defaultValue?: T;
}
8 changes: 5 additions & 3 deletions packages/graphql/lib/interfaces/return-type-func.interface.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Type } from '@nestjs/common';
import { GraphQLScalarType } from 'graphql';

export type GqlTypeReference =
| Type<any>
export type GqlTypeReference<T = any> =
| Type<T>
| GraphQLScalarType
| Function
| object
| symbol;
export type ReturnTypeFuncValue = GqlTypeReference | [GqlTypeReference];
export type ReturnTypeFunc = (returns?: void) => ReturnTypeFuncValue;
export type ReturnTypeFunc<T extends ReturnTypeFuncValue = any> = (
returns?: void,
) => T;
2 changes: 1 addition & 1 deletion packages/graphql/lib/interfaces/type-options.interface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BaseTypeOptions } from './base-type-options.interface';

export interface TypeOptions extends BaseTypeOptions {
export interface TypeOptions<T = any> extends BaseTypeOptions<T> {
isArray?: boolean;
arrayDepth?: number;
}
41 changes: 41 additions & 0 deletions packages/graphql/tests/plugin/decorators/field.decorator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Field } from '../../../lib/decorators';

class Inner {
test: string;
}

// It should expect the right primitive as defaultValue
class WrongPrimitive {
// @ts-expect-error The defaultValue should be a boolean
@Field(() => Boolean, { defaultValue: 'true' })
bool: boolean;
}

class CorrectPrimitive {
@Field(() => Boolean, { defaultValue: true })
bool: boolean;
}

// It should expect the right object as defaultValue
class WrongObject {
// @ts-expect-error The defaultValue should be of the shape of Inner
@Field(() => Inner, { defaultValue: { success: true } })
inner: Inner;
}

class CorrectObject {
@Field(() => Inner, { defaultValue: { test: 'hello' } })
inner: Inner;
}

// It should expect an Array as defaultValue
class WrongArray {
// @ts-expect-error The defaultValue should be an Array
@Field(() => [Inner], { defaultValue: { test: 'test' } })
inners: Inner[];
}

class CorrectArray {
@Field(() => [Inner], { defaultValue: [{ test: 'test' }] })
inner: Inner;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be Inner[]?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you're right ^^

}