Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Add a List of Posts to a Gatsby Blog with a GraphQL Page Query

    Taylor BellTaylor Bell

    In this lesson, you’ll use GraphQL to query for transformed Markdown blog posts and display links to each of them on the blog’s homepage.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    Instructor: Let's add a list of all of our posts. Since we're going to be writing a page-level query, we can start by removing the static query import. We'll write our query at the bottom of the file, above our export.

    We'll write "export const query" and then "graphql" with two backticks for our tag template. We'll use the query keyword because that's what we're going to get from GraphQL .

    Let's call our query home pageQuery. Now we can use the GraphQL browser in order to help us write our query. We can browse through the root query type. We're going to be doing posts. We want allMarkdownRemark.

    Remember that this is a remark connection. We click that. Then we have edges, which is the paths in the file system. A node is going to be each file. We go through. We have the frontmatter. These are all of our frontmatter options.

    To close this out, we'll move this over to have a little room. You can see that I've written the query here. If I click run, this is all the information that we have, which is what we would expect.

    Let's go ahead and add it to our file. Now that we've written our query, we need to bring it into our layout component. Let's take a look at the props that get passed into our layout.

    Inside the console of our localhost 8000, we can see that our query is showing up under the data key. This means that we can ahead and destructure data from props inside of our layout component.

    We can get to our edges from data.allMarkdownRemark. Now let's try just console.logging edges. These have our nodes like we'd expect. Now let's map over our edges in order to display our post titles.

    Between a couple braces, we'll do edges.map. We'll pass each edge in. From each edge, we want to get to the frontmatter. We'll destructure it. Then we'll return a div with the frontmatter's title.

    The first thing I notice is that I should add a key. Now we have a list of our posts, but you'll notice that they're not in order. Let's fix it.

    Going back to the GraphQL browser, we can scroll back through to allMarkdownRemark. We can see that it will take some arguments, one of them being sort. We can sort on fields. We can provide an order.

    The fields include a frontmatter date, which is a pretty solid choice, and an order. Makes sense that it would be ascending or descending.

    What we can do here is in our query, next to allMarkdownRemark, add a couple parens. We are going to sort and provide options here. Let's do order descending. For fields, we'll provide it with frontmatter___date.

    Save it. Check it out. There they are, newest to oldest. Now, through the magic of editing, I've added some styling.