From 8bffbd15e76452dad9334658e4789280338ac2bb Mon Sep 17 00:00:00 2001 From: Sam Adams Date: Wed, 9 Nov 2022 19:51:40 +0000 Subject: [PATCH 1/2] fix: enable non-nullable, defaulted fields in code-first schema --- .../code-first/recipes/dto/new-recipe.input.ts | 3 +++ .../tests/code-first/recipes/dto/recipes.args.ts | 7 +++++-- .../tests/code-first/recipes/recipes.resolver.ts | 1 + .../apollo/tests/e2e/code-first-schema.spec.ts | 14 ++++++++++++++ .../apollo/tests/utils/printed-schema.snapshot.ts | 2 ++ .../schema-builder/services/type-mapper.service.ts | 13 +------------ 6 files changed, 26 insertions(+), 14 deletions(-) diff --git a/packages/apollo/tests/code-first/recipes/dto/new-recipe.input.ts b/packages/apollo/tests/code-first/recipes/dto/new-recipe.input.ts index 698908607..5bb297751 100644 --- a/packages/apollo/tests/code-first/recipes/dto/new-recipe.input.ts +++ b/packages/apollo/tests/code-first/recipes/dto/new-recipe.input.ts @@ -12,6 +12,9 @@ export class NewRecipeInput { @Length(30, 255) description?: string; + @Field({ nullable: false, defaultValue: 'published' }) + status: string; + @Type(() => String) @Field((type) => [String]) ingredients: string[]; diff --git a/packages/apollo/tests/code-first/recipes/dto/recipes.args.ts b/packages/apollo/tests/code-first/recipes/dto/recipes.args.ts index 33931c919..cbfc77898 100644 --- a/packages/apollo/tests/code-first/recipes/dto/recipes.args.ts +++ b/packages/apollo/tests/code-first/recipes/dto/recipes.args.ts @@ -2,11 +2,14 @@ import { ArgsType, Field, Int } from '@nestjs/graphql'; import { Max, Min } from 'class-validator'; @ArgsType() export class RecipesArgs { - @Field((type) => Int, { description: 'number of items to skip' }) + @Field((type) => Int, { + description: 'number of items to skip', + nullable: true, + }) @Min(0) skip: number = 0; - @Field((type) => Int) + @Field((type) => Int, { nullable: true }) @Min(1) @Max(50) take: number = 25; diff --git a/packages/apollo/tests/code-first/recipes/recipes.resolver.ts b/packages/apollo/tests/code-first/recipes/recipes.resolver.ts index 7a5432127..80b140cfd 100644 --- a/packages/apollo/tests/code-first/recipes/recipes.resolver.ts +++ b/packages/apollo/tests/code-first/recipes/recipes.resolver.ts @@ -31,6 +31,7 @@ export class RecipesResolver { @Args('id', { defaultValue: '1', description: 'recipe id', + nullable: true, }) id: string, ): Promise { diff --git a/packages/apollo/tests/e2e/code-first-schema.spec.ts b/packages/apollo/tests/e2e/code-first-schema.spec.ts index 38090ce9b..21f113d40 100644 --- a/packages/apollo/tests/e2e/code-first-schema.spec.ts +++ b/packages/apollo/tests/e2e/code-first-schema.spec.ts @@ -651,6 +651,20 @@ describe('Code-first - schema factory', () => { name: 'description', type: { kind: TypeKind.SCALAR, name: 'String', ofType: null }, }, + { + defaultValue: '"published"', + description: null, + name: 'status', + type: { + kind: TypeKind.NON_NULL, + name: null, + ofType: { + kind: TypeKind.SCALAR, + name: 'String', + ofType: null, + }, + }, + }, { defaultValue: null, description: null, diff --git a/packages/apollo/tests/utils/printed-schema.snapshot.ts b/packages/apollo/tests/utils/printed-schema.snapshot.ts index 5d8f579fd..951086caa 100644 --- a/packages/apollo/tests/utils/printed-schema.snapshot.ts +++ b/packages/apollo/tests/utils/printed-schema.snapshot.ts @@ -105,6 +105,7 @@ input NewRecipeInput { """recipe title""" title: String! description: String + status: String! = "published" ingredients: [String!]! } @@ -163,6 +164,7 @@ type Mutation { input NewRecipeInput { description: String ingredients: [String!]! + status: String! = "published" """recipe title""" title: String! diff --git a/packages/graphql/lib/schema-builder/services/type-mapper.service.ts b/packages/graphql/lib/schema-builder/services/type-mapper.service.ts index b7e5447bf..dcca8b9d5 100644 --- a/packages/graphql/lib/schema-builder/services/type-mapper.service.ts +++ b/packages/graphql/lib/schema-builder/services/type-mapper.service.ts @@ -67,18 +67,7 @@ export class TypeMapperSevice { ); } - let isNotNullable: boolean; - if (isInputTypeCtx) { - /** - * The input values (e.g., args) remain "nullable" - * even if the "defaultValue" is specified. - */ - isNotNullable = - isUndefined(options.defaultValue) && - (!options.nullable || options.nullable === 'items'); - } else { - isNotNullable = !options.nullable || options.nullable === 'items'; - } + const isNotNullable = !options.nullable || options.nullable === 'items'; return isNotNullable ? (new GraphQLNonNull(graphqlType) as T) : (graphqlType as T); From e82062c4d959af2ad9af88884acc82ae3f26be44 Mon Sep 17 00:00:00 2001 From: Sam Adams Date: Fri, 11 Nov 2022 17:28:06 +0000 Subject: [PATCH 2/2] chore: remove unused argument `isInputTypeCtx` --- .../lib/schema-builder/factories/input-type.factory.ts | 1 - .../lib/schema-builder/factories/output-type.factory.ts | 7 +------ .../lib/schema-builder/services/type-mapper.service.ts | 1 - 3 files changed, 1 insertion(+), 8 deletions(-) diff --git a/packages/graphql/lib/schema-builder/factories/input-type.factory.ts b/packages/graphql/lib/schema-builder/factories/input-type.factory.ts index 893f5c3da..06ecfb888 100644 --- a/packages/graphql/lib/schema-builder/factories/input-type.factory.ts +++ b/packages/graphql/lib/schema-builder/factories/input-type.factory.ts @@ -38,7 +38,6 @@ export class InputTypeFactory { hostType, inputType, typeOptions, - true, ); } } diff --git a/packages/graphql/lib/schema-builder/factories/output-type.factory.ts b/packages/graphql/lib/schema-builder/factories/output-type.factory.ts index 78d59d0ad..d1082ba74 100644 --- a/packages/graphql/lib/schema-builder/factories/output-type.factory.ts +++ b/packages/graphql/lib/schema-builder/factories/output-type.factory.ts @@ -32,11 +32,6 @@ export class OutputTypeFactory { throw new CannotDetermineOutputTypeError(hostType); } } - return this.typeMapperService.mapToGqlType( - hostType, - gqlType, - typeOptions, - false, - ); + return this.typeMapperService.mapToGqlType(hostType, gqlType, typeOptions); } } diff --git a/packages/graphql/lib/schema-builder/services/type-mapper.service.ts b/packages/graphql/lib/schema-builder/services/type-mapper.service.ts index dcca8b9d5..230cf5d05 100644 --- a/packages/graphql/lib/schema-builder/services/type-mapper.service.ts +++ b/packages/graphql/lib/schema-builder/services/type-mapper.service.ts @@ -54,7 +54,6 @@ export class TypeMapperSevice { hostType: string, typeRef: T, options: TypeOptions, - isInputTypeCtx: boolean, ): T { this.validateTypeOptions(hostType, options); let graphqlType: T | GraphQLList | GraphQLNonNull = typeRef;