การกำหนดค่า Schema
การกำหนดค่า Schemaการใช้ "field to input"

การใช้ "field to input"

ดึงค่าของฟิลด์ จัดการค่านั้น และส่งเป็น input ไปยังฟิลด์อื่น ทั้งหมดภายใน query เดียวกัน

วิธีการใช้งาน

ฟิลด์ที่ต้องการดึงค่ามาใช้จะถูกอ้างอิงด้วยไวยากรณ์ "Variable" คือ $ และ __ นำหน้า alias หรือชื่อฟิลด์ (เช่น ค่าจากฟิลด์ excerpt จะถูกอ้างอิงเป็น $__excerpt) ผลลัพธ์จากฟิลด์ที่สองยังสามารถนำไปใช้เป็น input ของฟิลด์อื่นได้อีกด้วย:

{
  posts {
    excerpt
 
    # Referencing previous field with name "excerpt"
    isEmptyExcerpt: _isEmpty(value: $__excerpt)
 
    # Referencing previous field with alias "isEmptyExcerpt"
    isNotEmptyExcerpt: _not(value: $__isEmptyExcerpt)
  }
}

ผลลัพธ์ที่ได้จะเป็น:

{
  "data": {
    "posts": [
      {
        "excerpt": "Some post excerpt",
        "isEmptyExcerpt": false,
        "isNotEmptyExcerpt": true
      },
      {
        "excerpt": "",
        "isEmptyExcerpt": true,
        "isNotEmptyExcerpt": false
      }
    ]
  }
}

ฟิลด์สามารถถูกอ้างอิงได้เฉพาะจาก sibling field ก่อนหน้าในโหนดเดียวกันเท่านั้น queries ต่อไปนี้จะไม่ทำงาน:

# This will fail because the reference to the field must appear after the field, not before
{
  posts {
    isEmptyExcerpt: _isEmpty(value: $__excerpt)
    excerpt
  }
}
 
# This will fail because the reference must be done within the same node
{
  posts {
    excerpt
  }
  isEmptyExcerpt: _isEmpty(value: $__excerpt)
}

นอกจากนี้ ฟิลด์ยังไม่สามารถถูกอ้างอิงจาก argument ของ directive ได้ (สำหรับกรณีนี้ให้ใช้ directive @passOnwards):

# This will fail because the reference can be only used as input to a field, not to a directive
{
  posts {
    hasComments
    title @include(if: $__hasComments)
  }
}

ตัวอย่าง

หาก excerpt ของโพสต์ว่างเปล่า ให้ใช้ title แทน:

{
  posts {
    title
    originalExcerpt: excerpt
    isEmptyExcerpt: _isEmpty(value: $__originalExcerpt)
    excerpt: if(condition: $__isEmptyExcerpt, then: $__title, else: $__originalExcerpt)
  }
}

ดึงข้อมูลจาก REST endpoint ภายนอก และจัดการข้อมูลนั้นให้ตรงตามความต้องการของคุณ

{
  externalData: _getJSON(url: "https://example.com/rest/some-external-endpoint")
  userName: _extract(object: $__externalData, path: "data.user.name")
  userLastName: _extract(object: $__externalData, path: "data.user.surname")
}

ผลลัพธ์ที่ได้จะเป็น:

{
  "data": {
    "externalData": {
      "data": {
        "user": {
          "id": 1,
          "name": "Leo",
          "surname": "Loso"
        }
      }
    },
    "userName": "Leo",
    "userLastName": "Loso"
  }
}

การใช้ directive @remove กับ externalData ยังช่วยให้เราไม่ต้องแสดงข้อมูลต้นทางจาก external endpoint ในผลลัพธ์ได้อีกด้วย:

{
  externalData: _getJSON(url: "https://example.com/rest/some-external-endpoint") @remove
  userName: _extract(object: $__externalData, path: "data.user.name")
  userLastName: _extract(object: $__externalData, path: "data.user.surname")
}

ผลลัพธ์ที่ได้จะเป็น:

{
  "data": {
    "userName": "Leo",
    "userLastName": "Loso"
  }
}

ดึงโพสต์ของผู้ใช้ทุกคนที่อ้างถึงอีเมลของผู้ใช้:

{
  users {
    email
    posts(filter: { search: $__email }) {
      id
      title
    }
  }
}

ส่ง newsletter โดยกำหนดอีเมล to และ from ผ่านฟิลด์ optionValue:

mutation {
  fromEmail: optionValue(name: "admin_email")
  toEmail: optionValue(name: "subscribers_email_list_recipient_address")
  _sendEmail(
    from: {
      email: $__fromEmail
    }
    to: $__toEmail
    subject: "Weekly summary"
    messageAs: {
      html: "..."
    }
  )
}