Query Functions
Query Functionsค่าเริ่มต้นของฟิลด์

ค่าเริ่มต้นของฟิลด์

Included in the “Power Extensions” bundle

@default directive ใช้สำหรับกำหนดค่าให้กับฟิลด์ที่มีค่าเป็น null หรือว่างเปล่า

คำอธิบาย

@default directive รับอาร์กิวเมนต์สองตัว:

  1. value: ค่าเริ่มต้น ซึ่งสามารถเป็น scalar type ใดก็ได้ (string, boolean, integer, float หรือ ID)
  2. condition: ระบุว่าฟิลด์ต้องเป็น null หรือว่างเปล่า ผ่านค่า enum IS_NULL หรือ IS_EMPTY ค่าเริ่มต้นคือ 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"
      }
    }
  }
}