Gatsby: [question] Querying allMarkdownRemark for multiple datasets

Created on 21 Mar 2018  路  2Comments  路  Source: gatsbyjs/gatsby

I'm just after some advice or an idea of the best practice to achieve something. I have these two queries working in GraphiQL:

  query LegalLinks {
    allMarkdownRemark(
      filter: { frontmatter: { templateKey: { eq: "legal" } } }
    ) {
      edges {
        node {
          frontmatter {
            title
          }
          fields {
            slug
          }
        }
      }
    }
  }

  query NavData {
    allMarkdownRemark(filter: { frontmatter: { mainNav: { eq: true } } }) {
      edges {
        node {
          frontmatter {
            title
            subtitle
          }
          fields {
            slug
          }
        }
      }
    }
  }

The first is looking for all pages that match on the legal template, for mapping over and putting in the footer (privacy policy, cookies, etc.).

The second is getting all the pages that have been marked as appearing on the main navigation, so I can build a dynamic main navbar.

I can't put both queries in the layout component, so not sure of the best course of action. I could do the filtering in the render method of the layout and build the two datasets that way, but it feels a bit weird to use GraphQL to get _all_ the pages and then filter them at the component level, after all, isn't the main attraction of GraphQL to just query for the data you need?

Assistance greatly appreciated!

Most helpful comment

You can use aliasing:
http://graphql.org/learn/queries/#aliases

query Name {
 legalLinks: allMarkdownRemark()
 NavData: allMarkdownRemark()
}

All 2 comments

You can use aliasing:
http://graphql.org/learn/queries/#aliases

query Name {
 legalLinks: allMarkdownRemark()
 NavData: allMarkdownRemark()
}

Oh that's awesome! Thank you so much!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kalinchernev picture kalinchernev  路  3Comments

3CordGuy picture 3CordGuy  路  3Comments

hobochild picture hobochild  路  3Comments

andykais picture andykais  路  3Comments

magicly picture magicly  路  3Comments