dgraph-io:master
← dpeek:graphql-timestamps
opened 07:53AM - 07 Sep 21 UTC
Adds support for `@default` directive to GraphQL input schema.
Syntax:
```…graphql
type Type {
field: FieldType @default(
add: {value: "value"}
update: { value: "value"}
)
}
```
Where a value is not provided as input for a mutation, the `add` value will be used if the node is being created, and the `update` value will be used if the node exists and is being updated. Values are provided as strings, parsed into the correct field type by Dgraph.
The string `$now` is replaced by the current DateTime string on the server, ie:
```graphql
type Type {
createdAt: DateTime! @default(
add: { value: "$now" }
)
updatedAt: DateTime! @default(
add: { value: "$now" }
update: { value: "$now" }
)
}
```
Schema validation will check that:
- `Int` field values can be parsed `strconv.ParseInt`
- `Float` field values can be parsed by `strconv.ParseFloat`
- `Boolean` field values are `true` or `false`
- `$now` can only be used with fields of type `DateTime` (could be extended to include `String`?)
Schema validation does not currently ensure that `@default` values for enums are a valid member of the enum, so this is allowed:
```graphql
enum State {
HOT
NOT
}
type Type {
state: State @default(add: { value: "FOO"})
}
```
Schema validation also prevents use of `@default` on:
- Types with `@remote` directive
- Fields of type `ID`
- Fields that are not scalar types (`Int`, `Float`, `String`, `Boolean`, `DateTime`) or an enum
- Fields that are list types (ie `[String])
- Fields with `@id`, `@custom`, `@lambda`
Output schema changes:
- Fields with `@default(add)` values become optional in `AddTypeInput`
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/dgraph-io/dgraph/8017)