Skip to content

Commit e0e6948

Browse files
committed
chore: moved hint to mutation
1 parent 9a63fc4 commit e0e6948

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

content/graphql/mutations.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,35 @@ export class UpvotePostInput {
4141

4242
> info **Hint** The `@InputType()` decorator takes an options object as an argument, so you can, for example, specify the input type's description. Note that, due to TypeScript's metadata reflection system limitations, you must either use the `@Field` decorator to manually indicate a type, or use a [CLI plugin](/graphql/cli-plugin).
4343
44+
> info **Hint** When a field is nullable you need to type it verbosely in order to prevent any runtime issue:
45+
>
46+
> ```ts
47+
> @Field(() => String, {{ '{' }} nullable: true {{ '}' }}) title?: string | null;
48+
> ```
49+
>
50+
> This is particularly important when dealing with partial updates in your API, especially if the corresponding database field is non-nullable (let's assume `author.title` is a mandatory field in database).
51+
>
52+
> Problem: assume you want to allow users to perform partial updates on `author`'s info. So you define the `title` field in your `Input` object type:
53+
>
54+
> ```ts
55+
> @Field(() => String, {{ '{' }} nullable: true {{ '}' }}) title?: string;
56+
> ```
57+
>
58+
> 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, something like this:
59+
>
60+
> ```ts
61+
> prisma.author.update({{ '{' }}
62+
> where: {{ '{' }}
63+
> id: author.id
64+
> {{ '}' }},
65+
> data: dto
66+
> {{ '}' }})
67+
> ```
68+
>
69+
> Then your database will throw an error because `null` is not assignable to `title` which is non-nullable.
70+
>
71+
> 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`.
72+
4473
We can then use this type in the resolver class:
4574
4675
```typescript

content/graphql/resolvers-map.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ 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`). **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. **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`.
82-
8381
> info **Hint** You can also add a description to, or deprecate, the whole object type: `@ObjectType({{ '{' }} description: 'Author model' {{ '}' }})`.
8482
8583
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)