Skip to content

Commit

Permalink
Merge pull request #2500 from sam-super/fix-not-null-default-val
Browse files Browse the repository at this point in the history
fix: enable non-nullable, defaulted fields in code-first schema
  • Loading branch information
kamilmysliwiec committed Dec 7, 2022
2 parents 6864eb2 + e82062c commit 2baa066
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down
7 changes: 5 additions & 2 deletions packages/apollo/tests/code-first/recipes/dto/recipes.args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class RecipesResolver {
@Args('id', {
defaultValue: '1',
description: 'recipe id',
nullable: true,
})
id: string,
): Promise<IRecipe> {
Expand Down
14 changes: 14 additions & 0 deletions packages/apollo/tests/e2e/code-first-schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions packages/apollo/tests/utils/printed-schema.snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ input NewRecipeInput {
"""recipe title"""
title: String!
description: String
status: String! = "published"
ingredients: [String!]!
}
Expand Down Expand Up @@ -163,6 +164,7 @@ type Mutation {
input NewRecipeInput {
description: String
ingredients: [String!]!
status: String! = "published"
"""recipe title"""
title: String!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export class InputTypeFactory {
hostType,
inputType,
typeOptions,
true,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ export class TypeMapperSevice {
hostType: string,
typeRef: T,
options: TypeOptions,
isInputTypeCtx: boolean,
): T {
this.validateTypeOptions(hostType, options);
let graphqlType: T | GraphQLList<T> | GraphQLNonNull<T> = typeRef;
Expand All @@ -67,18 +66,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);
Expand Down

0 comments on commit 2baa066

Please sign in to comment.