Query examples
Query examplesUsers

Users

These are examples of queries to fetch user data and log the user in.

Fetching users

A single user with his/her posts:

query {
  user(by: { id: 1 }) {
    name
    email
    url
    posts {
      id
      title
      excerpt
    }
  }
}

A list of 5 users, ordered by name:

query {
  users(
    pagination: { limit: 5 }
    sort: { by: NAME, order: ASC }
  ) {
    id
    displayName
    websiteURL
  }
}

A list of predefined users, with their avatars:

{
  users(filter: { ids: [2, 3, 5] }) {
    id
    displayName
    url
    avatar(size: 150) {
      size
      src
    }
  }
}

Filtering users by name:

query {
  users(filter: { searchBy: { name: "le" } }) {
    id
    name
    email
  }
}

Counting user results:

query {
  userCount(
    filter: { searchBy: { name: "le" } }
  )
}

Paginating users:

query {
  users(
    pagination: {
      limit: 5,
      offset: 5
    }
  ) {
    id
    name
  }
}

Fetching meta values:

query {
  users {
    id
    name
    metaValue(
      key: "last_name",
    )
  }
}

Fetching roles and capabilities

Getting the roles and capabilities for the users:

query {
  users {
    id
    displayName
    roles {
      name
      capabilities
    }
  }
}

Logging the user in and out

Logging the user in is required to execute mutations (create a post, add a comment, etc).

This query logs the user in:

mutation {
  loginUser(
    by: {
      credentials: {
        usernameOrEmail: "test",
        password: "pass"
      }
    }
  ) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
      ...on GenericErrorPayload {
        code
      }
    }
    userID
  }
}

To retrieve the logged-in user:

query {
  me {
    id
    name
  }
}

Log the user out:

mutation {
  logoutUser {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
      ...on GenericErrorPayload {
        code
      }
    }
    userID
  }
}