Query examples
Query examplesPages

Pages

These are examples of queries to fetch page data.

Fetching pages

A single page:

query {
  page(by: { id: 2 }) {
    id
    title
    content
    url
    date
  }
}

A list of pages:

query {
  pages(pagination: { limit: 5 }) {
    id
    title
    excerpt
    url
    dateStr(format: "d/m/Y")
  }
}

Top-level pages with their children:

query {
  pages(filter: { parentID: 0 }) {
    ...PageProps
    children {
      ...PageProps
      children(pagination: { limit: 3 }) {
        ...PageProps
      }
    }
  }
}
 
fragment PageProps on Page {
  id
  title
  date
  urlAbsolutePath
}

Fetching the logged-in user's pages

Fields page, pages and pageCount only retrieve pages with status "publish".

To fetch pages from the logged-in user, with any status ("publish", "pending", "draft" or "trash"), use these fields:

  • myPage
  • myPages
  • myPageCount
query {
  myPages(filter: { status: [draft, pending] }) {
    id
    title
    status
  }
}

Creating pages

Only logged-in users can create pages.

mutation {
  createPage(
    input: {
      title: "Hi there!"
      contentAs: { html: "How do you like it?" }
      status: draft
    }
  ) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
      ...on GenericErrorPayload {
        code
      }
    }
    pageID
    page {
      status
      title
      content
      url
      date
      author {
        id
        name
      }
    }
  }
}

Updating pages

Only users with the corresponding capabilities can edit pages.

mutation {
  updatePage(
    input: {
      id: 2,
      title: "This is my new title",
    }
  ) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
      ...on GenericErrorPayload {
        code
      }
    }
    page {
      id
      title
    }
  }
}

This query uses nested mutations to update the page:

mutation {
  page(by: { id: 2 }) {
    originalTitle: title
    update(input: {
      title: "This is my new title",
      contentAs: { html: "This rocks!" }
    }) {
      status
      errors {
        __typename
        ...on ErrorPayload {
          message
        }
      }
      page {
        newTitle: title
        content
      }
    }
  }
}
Prev
Next