DEV Community

Rasul Kireev
Rasul Kireev

Posted on • Originally published at rasulkireev.com on

Multiple Graphql Queries on a Single Page with Gridsome

Ever wonder how include multiple queries on the same page, when using Gridsome? Well, this is how.

Let's say you have the following queries:

// query details about the current blog post 
query Post ($path: String!) {
  post: post (path: $path) {
    title
    date (format: "MMMM D, Y")
    content
    path
  }
Enter fullscreen mode Exit fullscreen mode

and

// query to pull all the webmentions on a specific post
query($path: String!) {
    mentions: allWebMention(filter: { wmTarget: { regex: $path } }) {
      edges {
        node {
          wmId
          wmProperty
          content {
            text
          }
          author {
            name
          }
        }
      }
    }
  }
Enter fullscreen mode Exit fullscreen mode

The question is: How do you get both of those queries in your Post.vue template?

I was going to give you all the options that I tried before making this work, but that would be the waste of your time. So here is he working version:

<page-query>
query Post ($path: String!) {
  post: post (path: $path) {
    title
    date (format: "MMMM D, Y")
    content
    path
  }
  mentions: allWebMention (filter: { wmTarget: { regex: $path } }) {
    totalCount
    edges {
      node {
        wmId
        wmProperty
        content {
          text
        }
        author {
          name
        }
      }
    }
  }
}
</page-query>
Enter fullscreen mode Exit fullscreen mode

Then you can access these queries like this:

<div>{{ $page.post.title }}</div>

<div 
v-for="mention in $page.mentions.edges"
:key="mention.node.wmId"
>
    <p>{{ mention.node.wmProperty }}</p>
    <p>{{ mention.node.content.text }}</p>
</div>
Enter fullscreen mode Exit fullscreen mode

If you have any thougths, comments or questions please let me know on Twitter.

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay