Skip to content

Commit 137d5d0

Browse files
committed
docs: add documentation for @defaultValue and @calcMutations
1 parent 2166d16 commit 137d5d0

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

docs/modelling.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,70 @@ only have the `sparse` option because `@unique` is its own directive.
626626
is a filter that makes sure no `null` values can be returned (even if there are actually no
627627
`null` values currently in the database).
628628

629+
## More field features
630+
631+
There are two additional directives you can use on fields: `@defaultValue` and `@calcMutations`.
632+
633+
### Default values
634+
635+
When an object is created without specifying all fields, the unspecified fields will be set to
636+
`null` (scalars and value objects), `{}` (entity extensions), or `[]` (child entities).
637+
638+
You can add the `@defaultValue` directive to a field to change this behavior. **The default value
639+
will be used during object creation** if the field value is not specified. The directive does not
640+
affect existing objects. This still applies if you add a new field with the `@defaultValue`
641+
directive: only newly created objects will have the default value.
642+
643+
Be careful with specifying the field value exactly, because there are currently no typechecks for
644+
default values. For example, for fields of type `Int`, make sure you specify a number and not a
645+
string value:
646+
647+
```graphql
648+
type Delivery @rootEntity {
649+
isShipped: Boolean @defaultValue(value: false)
650+
}
651+
```
652+
653+
### Calc mutations
654+
655+
If you add the `@calcMutations` directive to a scalar field, additional input fields will be
656+
available for the update mutations. These fields perform simple calculations like incrementing a
657+
value or appending a list item. Calc mutations are always executed within the transaction, so you
658+
can use this to e.g. update a number range without risk of race conditions.
659+
660+
For example, given the following type definition:
661+
662+
```graphql
663+
type NumberRange @rootEntity {
664+
name: String @key
665+
value: Int @calcMutations(operators: [ADD])
666+
}
667+
```
668+
669+
You can increment the `value` with a mutation like this:
670+
671+
```graphql
672+
mutation UpdateNumberRange {
673+
updateNumberRange(input: { name: "deliveries", value_add: 1 }) {
674+
value
675+
}
676+
}
677+
```
678+
679+
The following operators are available on numeric types (`GraphQLInt`, `GraphQLInt53`,
680+
`GraphQLFloat`, `GraphQLDecimal1`, `GraphQLDecimal2` and `GraphQLDecimal3`);
681+
682+
- `MULTIPLY`
683+
- `DIVIDE`
684+
- `ADD`
685+
- `SUBTRACT`
686+
- `MODULO`
687+
688+
The following operators are available on the type `String`:
689+
690+
- `APPEND`
691+
- `PREPEND`
692+
629693
## Scalar types
630694

631695
### Text types

0 commit comments

Comments
 (0)