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

fix: enable non-nullable, defaulted fields in code-first schema #2500

Merged
merged 2 commits into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -67,18 +67,7 @@ export class TypeMapperSevice {
);
}

let isNotNullable: boolean;
if (isInputTypeCtx) {
sam-super marked this conversation as resolved.
Show resolved Hide resolved
/**
* 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