Querying data

You can query data from the GraphQL data layer into any Page, Template or Component. Queries are added with a <page-query> or <static-query> block in Vue Components.

  • Use <page-query> in Pages & Templates.
  • Use <static-query> in Components.

How to query with GraphQL

Working with GraphQL in Gridsome is easy and you don't need to know much about GraphQL. Here is an example of how to use GraphQL in page-query for a page:

<template>
  <div>
    <div v-for="edge in $page.posts.edges" :key="edge.node.id">
      <h2>{{ edge.node.title }}</h2>
    </div>
  </div>
</template>

<page-query>
query Posts {
  posts: allWordPressPost {
    edges {
      node {
        id
        title
      }
    }
  }
}
</page-query>

With GraphQL you only query the data you need. This makes it easier and more tidy to work with data. A query always starts with query and then something like Posts(Can be anything). Then you write something like posts: allWordPressPost. The allWordPressPost is the name of the GraphQL collection you want to query. The posts: part is an optional alias. When using posts as alias, your data will be available at $page.posts (or $static.posts if you use <static-query>). Otherwise it will be available at $page.allWordPressPost.

Learn more about GraphQL queries

Querying collections

You will notice that some of the root fields in your schema are prefixed with all. Use them to get lists of nodes in a collection.

ArgumentDefaultDescription
sortBy"date"Sort by a node field.
orderDESCSort order (DESC or ASC).
sortSort by multiple node fields.
skip0How many nodes to skip.
limitHow many nodes to get.
pageWhich page to get.
perPageHow many nodes to show per page. Omitted if no page argument is provided.
filter{}Read more.

Find nodes sorted by title

query Posts {
  allPost(sortBy: "title", order: DESC) {
    edges {
      node {
        title
      }
    }
  }
}

Sort a collection by multiple fields

query Posts {
  allPost(sort: [{ by: "featured" }, { by: "date" }]) {
    edges {
      node {
        title
      }
    }
  }
}

Querying single nodes

The other fields that do not start with all are your single entries. They are typically used by templates to get data for the current page. You must provide either an id or a path as an argument to find the node.

Arguments

ArgumentDefaultDescription
idnullGet node by id.

Example query

query Post {
  post(id: "1") {
    title
  }
}

Query data in Page components

Every page can have a <page-query> block with a GraphQL query to fetch data from data sources. The results will be stored in a $page property inside the page component.

<template>
  <Layout>
    <h2>Latest blog posts</h2>
    <ul>
      <li v-for="edge in $page.posts.edges" :key="edge.node.id">
        {{ edge.node.title }}
      </li>
    </ul>
  </Layout>
</template>

<page-query>
query Blog {
  posts: allWordPressPost {
    edges {
      node {
        id
        title
      }
    }
  }
}
</page-query>

Query data in any component

Every Vue component can have a <static-query> block with a GraphQL query to fetch data from data sources. The results will be stored in a $static property inside the component. A <static-query> is named static as it can not accept any variables.

<template>
  <div v-html="$static.post.content" />
</template>

<static-query>
query Post {
  post(id: "1") {
    content
  }
}
</static-query>

Edit this page on GitHub