Query Functions
Query FunctionsValore Predefinito di un Campo

Valore Predefinito di un Campo

Included in the “Power Extensions” bundle

Direttiva @default, per assegnare un valore ai campi null o vuoti.

Descrizione

La direttiva @default accetta due argomenti:

  1. value: il valore predefinito, di qualsiasi tipo scalare (string, boolean, integer, float o ID).
  2. condition: se il campo deve essere null o vuoto, tramite i valori enum IS_NULL o IS_EMPTY. Per impostazione predefinita è null.

Nell'esempio seguente, quando un articolo non ha un'immagine in evidenza, il campo featuredImage restituisce null:

{
  post(by: { id: 1 }) {
    featuredImage {
      id
      src
    }
  }
}
{
  "data": {
    "post": {
      "featuredImage": null
    }
  }
}

Utilizzando @default, possiamo quindi recuperare un'immagine predefinita:

{
  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"
      }
    }
  }
}