Query data WordPress
Query data WordPressDirektif

Direktif

Direktif disediakan melalui ekstensi Gato GraphQL. Berikut hanya beberapa contohnya.

Direktif operasi

Buat operation pipeline melalui @depends, dan jalankan salah satu operasinya secara kondisional, berdasarkan nilai dinamis tertentu, melalui @skip dan @include:

query CheckIfPostExists($id: ID!) {
  # Inisialisasi variabel dinamis ke `false`
  postExists: _echo(value: false)
    @export(as: "postExists")
 
  post(by: { id: $id }) {
    # Post ditemukan => Setel variabel dinamis ke `true`
    postExists: _echo(value: true)
      @export(as: "postExists")
  }
}
 
mutation ExecuteOnlyIfPostExists
  @depends(on: "CheckIfPostExists")
  @include(if: $postExists)
{
  # Lakukan sesuatu...
}

Direktif field

Ubah field menjadi huruf kecil melalui @strLowerCase:

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

Berikan nilai default untuk field melalui @default:

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

Hapus output field dari respons melalui @remove:

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

Terapkan function field pada nilai field tertentu, melalui @passOnwards dan @applyFunction:

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