WordPressデータのクエリ
WordPressデータのクエリディレクティブ

ディレクティブ

ディレクティブはGato GraphQL拡張機能を通じて提供されます。以下はその一部の例です。

オペレーションディレクティブ

@dependsを使ってオペレーションパイプラインを作成し、@skip@includeを使って動的な値に基づいてオペレーションを条件付きで実行します:

query CheckIfPostExists($id: ID!) {
  # Initialize the dynamic variable to `false`
  postExists: _echo(value: false)
    @export(as: "postExists")
 
  post(by: { id: $id }) {
    # Found the Post => Set dynamic variable to `true`
    postExists: _echo(value: true)
      @export(as: "postExists")
  }
}
 
mutation ExecuteOnlyIfPostExists
  @depends(on: "CheckIfPostExists")
  @include(if: $postExists)
{
  # Do something...
}

フィールドディレクティブ

@strLowerCaseを使ってフィールドを小文字に変換します:

{
  posts(pagination: { limit: 3 }) {
    id
    title @strLowerCase
  }
}

@defaultを使ってフィールドにデフォルト値を設定します:

query GetFeaturedImages {
  posts(pagination: { limit: 10 }) {
    id
    title
    hasFeaturedImage
    featuredImage @default(value: 1505) {
      id
      src
    }
  }
}

@removeを使ってフィールドの出力をレスポンスから削除します:

query GetFeaturedImages {
  posts(pagination: { limit: 10 }) {
    id
    title
    hasFeaturedImage
    featuredImage @remove(condition: IS_NULL) {
      src
    }
    sourceFeaturedImage: featuredImage {
      src
    }
  }
}

@passOnwards@applyFunctionを使って、フィールド値にfunction fieldを適用します:

{
  posts {
    id
    hasComments
    notHasComments: hasComments
      @passOnwards(as: "postHasComments")
      @applyFunction(
        name: "_not"
        arguments: {
          value: $postHasComments
        },
        setResultInResponse: true
      )
  }
}