Hàm Queries
Hàm QueriesField To Input

Field To Input

Included in the “Power Extensions” bundle

Lấy giá trị của một trường, thao tác với nó, và đưa vào một trường hoặc chỉ thị khác, tất cả trong cùng một thao tác.

Truyền giá trị của trường field làm đầu vào cho một trường khác thông qua $__field, và làm đầu vào cho một chỉ thị thông qua field @passOnwards(as: "variableName").

$__field

Truyền giá trị của trường làm đầu vào cho một trường khác. Cú pháp để tham chiếu giá trị của trường là: $ (tức là ký hiệu biến trong GraphQL), theo sau là __ và bí danh hoặc tên của trường.

Ví dụ, giá trị từ trường excerpt được tham chiếu là $__excerpt, và postTitle: title được tham chiếu là $__postTitle.

Phản hồi từ trường thứ hai có thể bản thân nó được dùng làm đầu vào cho một trường khác:

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

Phản hồi sẽ là:

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

Trường cũng không thể được tham chiếu từ một đối số của chỉ thị (để làm điều đó, hãy dùng @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

Chỉ thị @passOnwards làm cho giá trị đã được phân giải của trường trở nên khả dụng cho các chỉ thị tiếp theo thông qua một biến động.

Trong query bên dưới, trường notHasComments được tạo bằng cách lấy giá trị từ trường hasComments và tính giá trị ngược lại. Cách thức hoạt động:

  • Giá trị của trường được cung cấp thông qua @passOnwards; giá trị của trường sau đó có thể được đưa vào bất kỳ chỉ thị tiếp theo nào
  • @applyField nhận đầu vào (được xuất dưới biến động $postHasComments), áp dụng trường toàn cục not vào nó, và lưu kết quả trở lại vào trường
{
  posts {
    id
    hasComments
    notHasComments: hasComments
      @passOnwards(as: "postHasComments")
      @applyField(
        name: "_not"
        arguments: {
          value: $postHasComments
        },
        setResultInResponse: true
      )
  }
}

Điều này sẽ tạo ra:

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

Chúng ta cũng có thể lấy giá trị của bất kỳ trường đã phân giải nào trong đối tượng, bằng cách truyền bí danh hoặc tên trường vào đối số property.

Ví dụ, trong query này, chúng ta truy cập giá trị đã phân giải theo tên trường id, hoặc bí danh second, và xuất giá trị đó thông qua một biến động để in ra trong một query tiếp theo:

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)
}

Điều này sẽ tạo ra:

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

Ví dụ

Nếu đoạn trích của bài viết trống, hãy dùng tiêu đề thay thế:

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

Lấy dữ liệu từ một endpoint REST bên ngoài, và thao tác dữ liệu đó theo yêu cầu của bạn.

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

Điều này sẽ tạo ra:

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

Sử dụng chỉ thị @remove trên externalData, chúng ta cũng có thể tránh in dữ liệu nguồn của endpoint bên ngoài trong phản hồi:

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

Điều này bây giờ sẽ tạo ra:

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

Lấy các bài viết của mỗi người dùng có đề cập đến email của người dùng đó:

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

Gửi bản tin định nghĩa email tofrom thông qua trường 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: "..."
    }
  )
}

Thực hiện các thao tác có điều kiện dựa trên giá trị của trường. Trong query này, người dùng "Leo""Peter" có tên được chuyển đổi thành chữ hoa, vì họ nằm trong mảng "người dùng đặc biệt", trong khi "Martin" thì không:

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

...tạo ra:

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