Query WordPress 数据
Query 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
      )
  }
}