Query Functions
Query Functionsฟิลด์บนฟิลด์

ฟิลด์บนฟิลด์

Included in the “Power Extensions” bundle

ไดเรกทีฟ @applyField ใช้สำหรับรันฟิลด์หนึ่งบนค่าที่ได้รับจากฟิลด์ที่ถูก resolve

คำอธิบาย

เมื่อนำไปใช้กับฟิลด์ใดฟิลด์หนึ่ง ไดเรกทีฟ @applyField ช่วยให้สามารถรันฟิลด์อื่น (ที่มีอยู่ในประเภทเดียวกันและถูกนำไปใช้กับอ็อบเจกต์เดียวกัน) แล้วส่งค่าผลลัพธ์นั้นต่อไปยังไดเรกทีฟอื่น หรือแทนที่ค่าของฟิลด์ได้

สิ่งนี้ทำให้เราสามารถจัดการค่าของฟิลด์ได้หลายวิธี โดยนำฟังก์ชันการทำงานที่มีให้ผ่านส่วนขยาย PHP Functions via Schema มาประยุกต์ใช้ และบันทึกผลลัพธ์ใหม่ในการตอบสนอง

ใน queries ด้านล่าง ฟิลด์ Post.title ของอ็อบเจกต์มีค่าเป็น "Hello world!" โดยการเพิ่ม @applyField เพื่อรันฟิลด์ _strUpperCase (และนำหน้าด้วย @passOnwards ซึ่งส่งออกค่าของฟิลด์ภายใต้ $input แบบไดนามิก):

{
  post(by: { id: 1 }) {
    title
      @passOnwards(as: "input")
      @applyField(
        name: "_strUpperCase"
        arguments: {
          text: $input
        },
        setResultInResponse: true
      )
  }
}

...ค่าของฟิลด์จะถูกแปลงเป็นตัวพิมพ์ใหญ่ทั้งหมด ได้ผลลัพธ์เป็น:

{
  "data": {
    "post": {
      "title": "HELLO WORLD!"
    }
  }
}

เราสามารถเชื่อมต่อ @applyFunction หลายตัวเข้าด้วยกัน โดยใช้การตอบสนองจากตัวหนึ่งเป็นอินพุตของอีกตัวหนึ่ง ทำให้สามารถดำเนินการหลายอย่างกับค่าฟิลด์เดียวกัน

ใน queries ด้านล่างมีการนำ @applyFunction 2 ตัวมาใช้:

  1. แปลงเป็นตัวพิมพ์ใหญ่ทั้งหมด และส่งค่าต่อไปภายใต้ $ucTitle
  2. แทนที่ " " ด้วย "-" และแทนที่ค่าของฟิลด์
{
  post(by: { id: 1 }) {
    title
      @passOnwards(as: "input")
      @applyField(
        name: "_strUpperCase"
        arguments: {
          text: $input
        },
        passOnwardsAs: "ucTitle"
      )
      @applyField(
        name: "_strReplace"
        arguments: {
          search: " ",
          replaceWith: "-",
          in: $ucTitle
        },
        setResultInResponse: true
      )
  }
}

...ได้ผลลัพธ์เป็น:

{
  "data": {
    "post": {
      "title": "HELLO-WORLD!"
    }
  }
}

ตัวอย่างเพิ่มเติม

ดึงค่าตรงข้ามกับที่ฟิลด์ให้มา:

{
  posts {
    id
    notHasComments: hasComments
      @passOnwards(as: "hasComments")
      @applyField(
        name: "_not",
        arguments: {
          value: $hasComments
        },
        setResultInResponse: true
      )
  }
}

ร่วมกับส่วนขยาย Data Iteration Meta Directives จัดการรายการทั้งหมดในอาร์เรย์ โดยย่อแต่ละรายการให้ไม่เกิน 20 อักขระ:

{
  posts {
    categoryNames
      @underEachArrayItem(passValueOnwardsAs: "categoryName")
        @applyField(
          name: "_strSubstr"
          arguments: {
            string: $categoryName,
            offset: 0,
            length: 20
          },
          setResultInResponse: true
        )
  }
}

ร่วมกับส่วนขยาย Data Iteration Meta Directives แปลงรายการแรกของอาร์เรย์เป็นตัวพิมพ์ใหญ่ทั้งหมด:

{
  posts {
    categoryNames
      @underArrayItem(passOnwardsAs: "value", index: 0)
        @applyField(
          name: "_strUpperCase"
          arguments: {
            text: $value
          },
          setResultInResponse: true
        )
  }
}