-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
API Generator: Change default value for input field if property has n…
…o initializer (#2324) Previously, the following property of an entity ```ts @Property({ type: types.date, nullable: true }) @field({ nullable: true }) availableSince?: Date; ``` resulted in the following input being generated: ```ts @isnullable() @isdate() @field({ nullable: true }) availableSince?: Date; ``` This was problematic for two reasons: 1. The error message would be misleading when trying to create an entity without providing a value for the property. For example, a valid GraphQL mutation ```graphql mutation CreateProduct { createProduct(input: { title: "A", slug: "A", description: "FOO" }) { id availableSince } } ``` would result in the following error: ``` "isDate": "availableSince must be a Date instance" ``` 2. Relying on the initializer as the default value is not obvious and appears somewhat magical. To address this, we now use `null` as the default value for nullable properties if no initializer is provided. If an initializer is provided, it is used as the default value. --------- Co-authored-by: Johannes Obermair <johannes.obermair@vivid-planet.com>
- Loading branch information
1 parent
ff526f2
commit bb04fb8
Showing
3 changed files
with
170 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
--- | ||
"@comet/cms-api": minor | ||
--- | ||
|
||
API Generator: Change default value for input field if property has no initializer | ||
|
||
Previously, the following property of an entity | ||
|
||
```ts | ||
@Property({ type: types.date, nullable: true }) | ||
@Field({ nullable: true }) | ||
availableSince?: Date; | ||
``` | ||
|
||
resulted in the following input being generated: | ||
|
||
```ts | ||
@IsNullable() | ||
@IsDate() | ||
@Field({ nullable: true }) | ||
availableSince?: Date; | ||
``` | ||
|
||
This was problematic for two reasons: | ||
|
||
1. The error message would be misleading when trying to create an entity without providing a value for the property. For example, a valid GraphQL mutation | ||
|
||
```graphql | ||
mutation CreateProduct { | ||
createProduct(input: { title: "A", slug: "A", description: "FOO" }) { | ||
id | ||
availableSince | ||
} | ||
} | ||
``` | ||
|
||
would result in the following error: | ||
|
||
``` | ||
"isDate": "availableSince must be a Date instance" | ||
``` | ||
|
||
2. Relying on the initializer as the default value is not obvious and appears somewhat magical. | ||
|
||
To address this, we now use `null` as the default value for nullable properties if no initializer is provided. If an initializer is provided, it is used as the default value. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters