Truy vấn dữ liệu WordPress
Truy vấn dữ liệu WordPressChỉ thị (Directives)

Chỉ thị (Directives)

Các chỉ thị được cung cấp thông qua các tiện ích mở rộng của Gato GraphQL. Dưới đây chỉ là một vài ví dụ.

Chỉ thị thao tác (Operation directives)

Tạo một pipeline thao tác thông qua @depends, và thực thi một trong các thao tác của nó theo điều kiện, dựa trên một giá trị động, thông qua @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...
}

Chỉ thị trường (Field directives)

Chuyển đổi một trường sang chữ thường thông qua @strLowerCase:

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

Cung cấp giá trị mặc định cho trường thông qua @default:

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

Xóa kết quả đầu ra của trường khỏi phản hồi thông qua @remove:

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

Áp dụng một function field trên giá trị của một trường, thông qua @passOnwards@applyFunction:

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