Tags

GraphQL
API
Data
external

Filtering and sorting in the external api

To use filtering or sorting functions, your blueprint must not be unique. These arguments are only available for all queries.

Filter

To use the filter and sort functions in a document query, your blueprint must not be unique. These arguments are only available in all queries. The filter is dynamically generated based on the fields present in the blueprint. For instance, in a blog, each page has a slug string field to define the path ending of each page. If you want to access the page located at /hello-world, the slug would be hello-world. Your framework can then retrieve the slug and you can use it in the GraphQL query as a variable like this:

query PageBySlug {
  allPage(where: { slug: { eq: "hello-world" } }) {
    edges {
      node {
        id
        title
        slug
      }
    }
  }
}

You probably want to pass the variable in a more dynamic way, so you would declare it as a variable as follows:

query PageBySlug($slug: String!) {
  allPage(where: { slug: { eq: $slug } }) {
    edges {
      node {
        id
        title
        slug
      }
    }
  }
}

Sorting

When you want to use sorting, the external API generates arguments for the all query that can be used. You can sort on fields either upward or downward. By default, if no argument is provided, the documents are always sorted by the last published date in descending order.
For example, if your project has a blueprint called Submission and a field for the submitDate, you could sort the submissions by the latest submission like this:

query{
  allSubmission(sort: {submitDate: DESC}){
    edges{
      node{
        id
        submitDate
        slug
      }
    }
  }
}

If you want to sort by multiple fields, you can use an array to pass multiple fields, and they will be interpreted in the order of lowest index first. In this example, the results will first be sorted by 'lastname' in ascending order. Then, all the guests with the same 'lastname' will be sorted by 'firstname' in descending order.

allGuest(sort: [{lastname: ASC}, {firstname: DESC}])

Tags

GraphQL
API
Data
external