2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Gatsby 1つのページで複数のクエリを実行する方法

Posted at

1つのページでお知らせやブログなど複数のコンテンツを表示したい場合はクエリを複数作成することができる。

サイドバー用のカテゴリ一覧など複数ページで共通のデータを使う場合はstaticQueryを使ってコンポーネントごとにクエリを実行する。

複数クエリを1つのページで実行する

複数クエリの実行は頭に名前をつけるだけ。名前は自由。

post: allMarkdownRemark(){}
import React from "react"
import { graphql } from "gatsby"

const MultipleQuery = ({ data }) => {
  const post = data.post
  const portfolio = data.portfolio
  const siteTitle = data.siteMetaData.siteMetadata.title

  console.log(data)

  return (
    <h1>test</h1>
  )
}

export default MultipleQuery

export const query = graphql`
  query {
    post: allMarkdownRemark(
      limit: 3
      sort: { order: DESC, fields: [frontmatter___date] }
      filter: { frontmatter: { type: { eq: "blog" } } }
    ) {
      edges {
        node {
          frontmatter {
            title
            path
          }
        }
      }
    }
    portfolio: allMarkdownRemark(
      sort: { order: DESC, fields: [frontmatter___date] }
      filter: { frontmatter: { type: { eq: "portfolio" } } }
    ) {
      edges {
        node {
          frontmatter {
            title
            path
          }
        }
      }
    }
    siteMetaData: site {
      siteMetadata {
        title
      }
    }
  }
`;
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?