การ Query ข้อมูล WordPress
การ Query ข้อมูล WordPressไดเรกทีฟ

ไดเรกทีฟ

ไดเรกทีฟให้บริการผ่านส่วนขยายของ Gato GraphQL ต่อไปนี้คือตัวอย่างบางส่วน

ไดเรกทีฟสำหรับ Operation

สร้าง operation pipeline ด้วย @depends และดำเนินการ operation ใดหนึ่งตามเงื่อนไขโดยอาศัยค่าไดนามิก ผ่าน @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...
}

ไดเรกทีฟสำหรับ Field

แปลงค่าของ field ให้เป็นตัวพิมพ์เล็กด้วย @strLowerCase:

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

กำหนดค่าเริ่มต้นให้กับ field ด้วย @default:

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

ลบผลลัพธ์ของ field ออกจาก response ด้วย @remove:

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

นำ function field ไปใช้กับค่าของ field ผ่าน @passOnwards และ @applyFunction:

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