Query Functions
Query FunctionsField To Input

Field To Input

Included in the “Power Extensions” bundle

ดึงค่าของฟิลด์ ปรับแต่งค่านั้น และส่งเป็น input ให้กับฟิลด์หรือ directive อื่นภายใน operation เดียวกัน

ส่งค่าของฟิลด์ field เป็น input ให้กับฟิลด์อื่นผ่าน $__field และส่งเป็น input ให้กับ directive ผ่าน field @passOnwards(as: "variableName")

$__field

ส่งค่าของฟิลด์เป็น input ให้กับฟิลด์อื่น syntax ที่ใช้อ้างอิงค่าของฟิลด์คือ $ (ซึ่งเป็นสัญลักษณ์ของตัวแปรใน GraphQL) ตามด้วย __ และ alias หรือชื่อของฟิลด์

ตัวอย่างเช่น ค่าจากฟิลด์ excerpt อ้างอิงด้วย $__excerpt และ postTitle: title อ้างอิงด้วย $__postTitle

response จากฟิลด์ที่สองสามารถนำไปใช้เป็น input ให้กับฟิลด์อื่นได้อีก:

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

response จะเป็น:

{
  "data": {
    "posts": [
      {
        "excerpt": "Some post excerpt",
        "isEmptyExcerpt": false,
        "isNotEmptyExcerpt": true
      },
      {
        "excerpt": "",
        "isEmptyExcerpt": true,
        "isNotEmptyExcerpt": false
      }
    ]
  }
}
# 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 ได้ (สำหรับกรณีนั้น ให้ใช้ @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)
  }
}

@passOnwards

directive @passOnwards ทำให้ค่าที่ resolve แล้วของฟิลด์พร้อมใช้งานสำหรับ directive ที่ตามมาผ่าน dynamic variable

ใน query ด้านล่าง ฟิลด์ notHasComments สร้างขึ้นโดยดึงค่าจากฟิลด์ hasComments แล้วคำนวณค่าตรงข้ามของมัน ซึ่งทำงานดังนี้:

  • ทำให้ค่าของฟิลด์พร้อมใช้งานผ่าน @passOnwards จากนั้นค่าของฟิลด์สามารถส่งเป็น input ให้กับ directive ที่ตามมาได้
  • @applyField รับ input (ที่ export ไว้ใน dynamic variable $postHasComments) นำ global field not มาใช้กับมัน และเก็บผลลัพธ์กลับไปที่ฟิลด์
{
  posts {
    id
    hasComments
    notHasComments: hasComments
      @passOnwards(as: "postHasComments")
      @applyField(
        name: "_not"
        arguments: {
          value: $postHasComments
        },
        setResultInResponse: true
      )
  }
}

ซึ่งจะให้ผลลัพธ์:

{
  "data": {
    "posts": [
      {
        "id": 1724,
        "hasComments": true,
        "notHasComments": false
      },
      {
        "id": 358,
        "hasComments": false,
        "notHasComments": true
      },
      {
        "id": 555,
        "hasComments": false,
        "notHasComments": true
      }
    ]
  }
}

นอกจากนี้ยังสามารถดึงค่าของฟิลด์ที่ resolve แล้วใดก็ได้ในออบเจกต์ โดยส่ง alias หรือชื่อฟิลด์ผ่าน argument property

ตัวอย่างเช่น ใน query นี้ เราเข้าถึงค่าที่ resolve แล้วด้วยชื่อฟิลด์ id หรือ alias second และ export ค่านั้นผ่าน dynamic variable เพื่อแสดงผลใน query ที่ตามมา:

query One {
  id
  second: _echo(value: 2)
    @passOnwards(
      property: "id",
      as: "resolvedFirstValue"
    )
    @exportFrom(
      scopedDynamicVariable: $resolvedFirstValue,
      as: "firstValue"
    )
  third: _echo(value: 3)
    @passOnwards(
      property: "second",
      as: "resolvedSecondValue"
    )
    @exportFrom(
      scopedDynamicVariable: $resolvedSecondValue,
      as: "secondValue"
    )
}
 
query Two @depends(on: "One") {
  firstValue: _echo(value: $firstValue)
  secondValue: _echo(value: $secondValue)
}

ซึ่งจะให้ผลลัพธ์:

{
  "data": {
    "id": "root",
    "second": 2,
    "third": 3,
    "firstValue": "root",
    "secondValue": 2
  }
}

ตัวอย่าง

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

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

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

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

ซึ่งจะให้ผลลัพธ์:

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

การใช้ directive @remove กับ externalData ยังช่วยให้เราสามารถหลีกเลี่ยงการแสดงข้อมูล source จาก external endpoint ใน response ได้:

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

ซึ่งจะให้ผลลัพธ์ดังนี้:

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

ดึงโพสต์ของผู้ใช้แต่ละคนที่กล่าวถึง email ของผู้ใช้นั้น:

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

ส่ง newsletter โดยกำหนด email 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: "..."
    }
  )
}

ดำเนิน operation แบบมีเงื่อนไขตามค่าของฟิลด์ ใน query นี้ ผู้ใช้ "Leo" และ "Peter" จะได้รับการแปลงชื่อเป็นตัวพิมพ์ใหญ่ เนื่องจากอยู่ใน array "ผู้ใช้พิเศษ" ในขณะที่ "Martin" ไม่ได้รับการแปลง:

query {
  users {
    name
      @passOnwards(as: "userName")
      @applyField(
        name: "_inArray"
        arguments: {
          value: $userName
          array: ["Leo", "John", "Peter"]
        }
        passOnwardsAs: "isSpecialUser"
      )
      @if(
        condition: $isSpecialUser
      )
        @strUpperCase
  }
}

...ให้ผลลัพธ์:

{
  "data": {
    "users": [
      {
        "name": "LEO"
      },
      {
        "name": "Martin"
      },
      {
        "name": "PETER"
      }
    ]
  }
}