Skip to content

Commit 2baa066

Browse files
Merge pull request #2500 from sam-super/fix-not-null-default-val
fix: enable non-nullable, defaulted fields in code-first schema
2 parents 6864eb2 + e82062c commit 2baa066

File tree

8 files changed

+27
-22
lines changed

8 files changed

+27
-22
lines changed

packages/apollo/tests/code-first/recipes/dto/new-recipe.input.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ export class NewRecipeInput {
1212
@Length(30, 255)
1313
description?: string;
1414

15+
@Field({ nullable: false, defaultValue: 'published' })
16+
status: string;
17+
1518
@Type(() => String)
1619
@Field((type) => [String])
1720
ingredients: string[];

packages/apollo/tests/code-first/recipes/dto/recipes.args.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ import { ArgsType, Field, Int } from '@nestjs/graphql';
22
import { Max, Min } from 'class-validator';
33
@ArgsType()
44
export class RecipesArgs {
5-
@Field((type) => Int, { description: 'number of items to skip' })
5+
@Field((type) => Int, {
6+
description: 'number of items to skip',
7+
nullable: true,
8+
})
69
@Min(0)
710
skip: number = 0;
811

9-
@Field((type) => Int)
12+
@Field((type) => Int, { nullable: true })
1013
@Min(1)
1114
@Max(50)
1215
take: number = 25;

packages/apollo/tests/code-first/recipes/recipes.resolver.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export class RecipesResolver {
3131
@Args('id', {
3232
defaultValue: '1',
3333
description: 'recipe id',
34+
nullable: true,
3435
})
3536
id: string,
3637
): Promise<IRecipe> {

packages/apollo/tests/e2e/code-first-schema.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,20 @@ describe('Code-first - schema factory', () => {
651651
name: 'description',
652652
type: { kind: TypeKind.SCALAR, name: 'String', ofType: null },
653653
},
654+
{
655+
defaultValue: '"published"',
656+
description: null,
657+
name: 'status',
658+
type: {
659+
kind: TypeKind.NON_NULL,
660+
name: null,
661+
ofType: {
662+
kind: TypeKind.SCALAR,
663+
name: 'String',
664+
ofType: null,
665+
},
666+
},
667+
},
654668
{
655669
defaultValue: null,
656670
description: null,

packages/apollo/tests/utils/printed-schema.snapshot.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ input NewRecipeInput {
105105
"""recipe title"""
106106
title: String!
107107
description: String
108+
status: String! = "published"
108109
ingredients: [String!]!
109110
}
110111
@@ -163,6 +164,7 @@ type Mutation {
163164
input NewRecipeInput {
164165
description: String
165166
ingredients: [String!]!
167+
status: String! = "published"
166168
167169
"""recipe title"""
168170
title: String!

packages/graphql/lib/schema-builder/factories/input-type.factory.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ export class InputTypeFactory {
3838
hostType,
3939
inputType,
4040
typeOptions,
41-
true,
4241
);
4342
}
4443
}

packages/graphql/lib/schema-builder/factories/output-type.factory.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@ export class OutputTypeFactory {
3232
throw new CannotDetermineOutputTypeError(hostType);
3333
}
3434
}
35-
return this.typeMapperService.mapToGqlType(
36-
hostType,
37-
gqlType,
38-
typeOptions,
39-
false,
40-
);
35+
return this.typeMapperService.mapToGqlType(hostType, gqlType, typeOptions);
4136
}
4237
}

packages/graphql/lib/schema-builder/services/type-mapper.service.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ export class TypeMapperSevice {
5454
hostType: string,
5555
typeRef: T,
5656
options: TypeOptions,
57-
isInputTypeCtx: boolean,
5857
): T {
5958
this.validateTypeOptions(hostType, options);
6059
let graphqlType: T | GraphQLList<T> | GraphQLNonNull<T> = typeRef;
@@ -67,18 +66,7 @@ export class TypeMapperSevice {
6766
);
6867
}
6968

70-
let isNotNullable: boolean;
71-
if (isInputTypeCtx) {
72-
/**
73-
* The input values (e.g., args) remain "nullable"
74-
* even if the "defaultValue" is specified.
75-
*/
76-
isNotNullable =
77-
isUndefined(options.defaultValue) &&
78-
(!options.nullable || options.nullable === 'items');
79-
} else {
80-
isNotNullable = !options.nullable || options.nullable === 'items';
81-
}
69+
const isNotNullable = !options.nullable || options.nullable === 'items';
8270
return isNotNullable
8371
? (new GraphQLNonNull(graphqlType) as T)
8472
: (graphqlType as T);

0 commit comments

Comments
 (0)