Tags

GraphQL
API
Data
external

Filtering and sorting in the external api

In order to use either filter or sort functions your blueprint must not be unique. This arguments are only available to the all queries.

Filter

The filter is always dynamically generated from the fields a blueprint has. Very common is to filter on a string field. Lets for example you are building a blog, each page gets a slug string field to define the path ending of each page. So you you would hit the page /hello-world your slug would be hello-world . Your framework will the be able to return you the slug and you can use it in the graphql query as a variable like so:

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 the sorting, the external api generates arguments on the all query that can be used. You can always sort on the fields either upwards or downwards. By default, if no argument is provided the documents are always sorted by the last published date in descending order.

For example if you project would have a blueprint called Submission and a field for the submitDate you could sort the submissions by the latest one submitted like so:

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


Tags

GraphQL
API
Data
external