question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[v2][RFC] Deprecate page queries completely and replace with StaticQuery?

See original GitHub issue

Several people have suggested this and it’s started feeling better and better over time.

It’d definitely better to have one pattern for queries rather than two. And the reaction to <StaticQuery> seems positive.

We’d have to add support for <StaticQueries> getting variables passed to them when they’re on pages. Which felt weird as that’s “magical” but… it’s no less magical than the existing page queries. Less since it has a component tied to it.

Oh hmmm… just had a thought — we could create a <PageQuery> component that’s identical to <StaticQuery> except it takes variables 🤔

Thoughts?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:7
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

8reactions
nakediblecommented, Jul 23, 2018

FWIW, I personally would find it much more intuitive if there were just a single query component, and that the entire pageQuery magic would be removed.

As I am used to Apollo, I would absolutely love it if the query component was similar. I will show this with a complete example to clarify my thoughts.

Old style:

export default ({data}) => {
    return (
        <div>
            <div dangerouslySetInnerHTML={{ __html: data.markdownRemark.html }} />
        </div>
    );
}

export const pageQuery = graphql`
query PageQuery($fileAbsolutePath: String!) {
  markdownRemark(fileAbsolutePath: { eq: $fileAbsolutePath }) {
    html
    frontmatter {
      path
      title
    }
  }
}
`;

Suggested style:

const query = gql`
query PageQuery($fileAbsolutePath: String!) {
  markdownRemark(fileAbsolutePath: { eq: $fileAbsolutePath }) {
    html
    frontmatter {
      path
      title
    }
  }
}
`;

export default ({pageContext}) => (
    <Query query={query} variables={pageContext.context}>
        {({ loading, error, data }) => {
            return (
                <div>
                    <div dangerouslySetInnerHTML={{ __html: data.markdownRemark.html }} />
                </div>
            );
        }}
    </Query>
);

Obviously code could be restructured with the query being at the bottom, if desired. It is a little more boilerplate on each page that uses a query - but it would remove all the magic and allow for much more options on how to query.

Also, loading state in Gatsby is now hardcoded to <div>Loading (StaticQuery)</div>, and probably error state causes some error handler to be invoked. The Apollo way lets the page handle both as it sees fit.

0reactions
gatsbot[bot]commented, Jan 24, 2019

This issue is being closed due to inactivity. Is this a mistake? Please re-open this issue or create a new issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Loading (Static Query) · Issue #6350 · gatsbyjs/gatsby - GitHub
Right now I am doing a page-level query the old way and then have imported components with static queries being performed in them....
Read more >
Querying Data in Components using StaticQuery - Gatsby
Please note: As of Gatsby 5 the <StaticQuery /> component is deprecated. Use the useStaticQuery hook instead. <StaticQuery /> will be removed in...
Read more >
How to reduce static query code duplication Gatsby JS
I was wondering if it is possible to remove this duplication by using the querying capabilities more efficiently? I've read that Gatsby static ......
Read more >
Storybook-addon-gatsby: How to Override Options in babel ...
The addon provides the stage and staticQueryDir options to babel-plugin-remove-graphql-queries , just like your example. It also configures ...
Read more >
babel-plugin-remove-graphql-queries | Yarn - Package Manager
Fast, reliable, and secure dependency management.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found