Query FunctionsField Default Value
Field Default Value
Included in the “Power Extensions” bundle
@default directive, to set a value to null or empty fields.
Description
Directive @default accepts two arguments:
value: the default value, from any scalar type (string, boolean, integer, float or ID).condition: if the field must be null or empty, via enum valuesIS_NULLorIS_EMPTY. By default it is null.
In the example below, when a post does not have a featured image, field featuredImage returns null:
{
post(by: { id: 1 }) {
featuredImage {
id
src
}
}
}{
"data": {
"post": {
"featuredImage": null
}
}
}By using @default, we can then retrieve some default image:
{
post(by: { id: 1 }) {
featuredImage @default(value: 55) {
id
src
}
}
}{
"data": {
"post": {
"featuredImage": {
"id": 55,
"src": "http://mysite.com/wp-content/uploads/my-default-image.webp"
}
}
}
}