Skip to content

Commit 75c3e0e

Browse files
authored
chore: rephrased a sentence to prevent confusion + adding a hint
1 parent beecac0 commit 75c3e0e

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

content/graphql/resolvers-map.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ The type function is required when there's the potential for ambiguity between t
6767

6868
The options object can have any of the following key/value pairs:
6969

70-
- `nullable`: for specifying whether a field is nullable (in SDL, each field is non-nullable by default); `boolean`
70+
- `nullable`: for specifying whether a field is nullable (in `@nestjs/graphql`, each field is non-nullable by default); `boolean`
7171
- `description`: for setting a field description; `string`
7272
- `deprecationReason`: for marking a field as deprecated; `string`
7373

@@ -78,6 +78,12 @@ For example:
7878
title: string;
7979
```
8080

81+
> info **Hint** When a field is nullable you need to type it verbosely in order to prevent any runtime issue: `@Field(() => String, {nullable: true}) title?: string | null;`. This particularly important when dealing with parcial updates in your API, especially if the corresponding database field is non-nullable (e.g. `author.title`).
82+
>
83+
> **Problem**: assume the `title` field is defined like this: `@Field(() => String, { nullable: true }) title?: string;`, indicating that user can skip `title` entirely in an update mutation. However if a user sends `null` explicitly for `title` and you pass the DTO directly to your database: `prisma.author.update({ where: { id: author.id }, data: dto })` your database will throw an error because `null` is not assignable to `title` which is non-nullable.
84+
>
85+
> **Solution**: To prevent this from happening you can be more specific when you're defining `title`'s type: `@Field(() => String, {nullable: true}) title?: string | null;`. Now when you're passing your DTO directly to the underlying ORM TS complain that `null` is not assignable to `string`.
86+
8187
> info **Hint** You can also add a description to, or deprecate, the whole object type: `@ObjectType({{ '{' }} description: 'Author model' {{ '}' }})`.
8288
8389
When the field is an array, we must manually indicate the array type in the `Field()` decorator's type function, as shown below:

0 commit comments

Comments
 (0)