Query Functions
Query FunctionsField To Input

Field To Input

Included in the “Power Extensions” bundle

フィールドの値を取得し、加工して、同じオペレーション内で別のフィールドやディレクティブへの入力として渡します。

field フィールドの値を $__field を使って別のフィールドへの入力として渡し、field @passOnwards(as: "variableName") を使ってディレクティブへの入力として渡します。

$__field

フィールドの値を別のフィールドへの入力として渡します。フィールドの値を参照するための構文は、$(GraphQL における変数のシンボル)に続けて __ とフィールドのエイリアスまたは名前を記述します。

例えば、excerpt フィールドの値は $__excerpt として参照され、postTitle: title$__postTitle として参照されます。

2番目のフィールドのレスポンスは、さらに別のフィールドへの入力として使用できます。

{
  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 は、フィールドの解決済みの値をダイナミック変数を通じて後続のディレクティブから利用できるようにします。

以下のクエリでは、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 引数にエイリアスまたはフィールド名を渡すことで、オブジェクト内の任意の解決済みフィールドの値を取得することもできます。

例えば、このクエリでは、フィールド名 id またはエイリアス second によって解決済みの値にアクセスし、その値をダイナミック変数でエクスポートして後続のクエリで出力します。

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

フィールドの値に基づいて条件付きオペレーションを実行します。このクエリでは、「特別なユーザー」配列に含まれている "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"
      }
    ]
  }
}