Query Functions
Query FunctionsField To Input

Field To Input

Included in the “Power Extensions” bundle

获取字段的值,对其进行处理,并在同一操作中将其作为输入传递给另一个字段或指令。

通过 $__fieldfield 字段的值作为输入传递给另一个字段,通过 field @passOnwards(as: "variableName") 将其作为输入传递给指令。

$__field

将字段的值作为输入传递给另一个字段。引用字段值的语法为:$(即 GraphQL 中的变量符号),后跟 __ 和字段的别名或名称。

例如,excerpt 字段的值被引用为 $__excerptpostTitle: title 被引用为 $__postTitle

第二个字段的响应本身也可以作为输入传递给另一个字段:

{
  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
      }
    ]
  }
}
# 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)
}

字段也不能从指令参数中引用(如需此功能,请使用 @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

指令 @passOnwards 通过动态变量将字段的已解析值提供给后续指令使用。

在下面的 Query 中,notHasComments 字段通过获取 hasComments 字段的值并计算其相反值来构成。其工作原理如下:

  • 通过 @passOnwards 使字段的值可用;字段的值随后可作为输入传递给任何后续指令
  • @applyField 接收输入(以动态变量 $postHasComments 导出),对其应用全局字段 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
      }
    ]
  }
}

通过在 property 参数中传入别名或字段名,还可以获取对象中任意已解析字段的值。

例如,在此 Query 中,我们通过字段名 id 或别名 second 访问已解析的值,并通过动态变量导出该值,以便在后续 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
  }
}

使用示例

如果文章摘要为空,则使用标题代替:

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

从外部 REST 端点获取数据,并根据需求对其进行处理。

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

externalData 使用 @remove 指令,还可以避免在响应中输出外部端点的源数据:

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

获取每个用户提及其邮箱地址的文章:

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

通过 optionValue 字段定义 tofrom 邮箱地址,发送新闻通讯:

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: "..."
    }
  )
}

根据字段的值执行条件操作。在此 Query 中,"Leo""Peter" 因为在"特殊用户"数组中,其名称被转换为大写,而 "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"
      }
    ]
  }
}