Reading Data

Now we've created some data in our backend, we will now explore how to read data.

Grafbase automatically generates the queries you need to fetch data by ID, unique field, or by collection.

Let's begin by creating our first GraphQL query to fetch all of our Posts using the generated postCollection query.

Grafbase also has support for pagination built-in, so we'll need to pass the amount of items we want per page in our query.

{
  postCollection(first: 10) {
    edges {
      node {
        id
        title
        url
      }
    }
  }
}

Playground Query

Thanks to the power of GraphQL, we can also fetch related comments in the same query. These relational queries also support pagination, so don't forget to pass the number of nodes you want to retrieve.

query {
  postCollection(first: 10) {
    edges {
      node {
        id
        title
        url
        comments(first: 100) {
          edges {
            node {
              id
              message
            }
          }
        }
      }
    }
  }
}

We'll next explore how to fetch a Post by id or url.

Grafbase generates a unique input type for the argument by for the query post.

{
  post(by: { id: "post_01GQ7YSZCB4ENYZVGBTBQFS4XE" }) {
    title
    url
  }
}
Was this page helpful?