Query Functions
Query Functions字段默认值

字段默认值

Included in the “Power Extensions” bundle

@default 指令用于为 null 或空字段赋值。

说明

@default 指令接受两个参数:

  1. value:默认值,可使用任意标量类型(string、boolean、integer、float 或 ID)。
  2. condition:通过枚举值 IS_NULLIS_EMPTY 指定字段必须为 null 或空的条件。默认为 null。

在以下示例中,当文章没有特色图片时,字段 featuredImage 返回 null

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

通过使用 @default,可以获取默认图片:

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