Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[question] Querying allMarkdownRemark for multiple datasets #4650

Closed
martynhoyer opened this issue Mar 21, 2018 · 2 comments
Closed

[question] Querying allMarkdownRemark for multiple datasets #4650

martynhoyer opened this issue Mar 21, 2018 · 2 comments

Comments

@martynhoyer
Copy link
Contributor

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!

@LekoArts
Copy link
Contributor

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

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

@martynhoyer
Copy link
Contributor Author

Oh that's awesome! Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants