{"data":{"markdownRemark":{"html":"<p><span style=\"text-align: center; display: block;\">Part of <a href=\"/gatsby-v2\">a series on Gatsby v2</a></span></p>\n<p>You've heard about <code class=\"language-text\">StaticQuery</code> as part of Gatsby v2. GraphQL everywhere. Sounds great, but how does it work?</p>\n<blockquote>\n<p><strong>tl;dr</strong> - Wrap any component in <code class=\"language-text\">StaticQuery</code> and pass two props <code class=\"language-text\">query</code> and <code class=\"language-text\">render</code>. These queries do not get any data about the current page, they're static for the entire build.</p>\n</blockquote>\n<h2>What is <code class=\"language-text\">StaticQuery</code>?</h2>\n<p>In Gatsby v1 there was only 2 places you could use GraphQL. Layouts and pages. Layouts have disappeared in Gatsby v2. Pages still use GraphQL.</p>\n<p>Imagine you want to pass the <code class=\"language-text\">title</code> from you <code class=\"language-text\">gatsby-config.js</code> file into a component. In Gatsby v1 you would have to get that data via the layout or the page. With <code class=\"language-text\">StaticQuery</code>, you can get that data anywhere.</p>\n<p>Or put another way, <code class=\"language-text\">StaticQuery</code> lets you write GraphQL queries which are parsed <strong>once</strong> during the build process. Think of it like this:</p>\n<ul>\n<li>Gatsby boots and loads all your files</li>\n<li>All GraphQL queries are extracted</li>\n<li>All <code class=\"language-text\">StaticQuery</code> instances are parsed, their queries run, and the data bound to their components</li>\n<li>All pages are built and page specific GraphQL queries run per page</li>\n</ul>\n<h2>Where to use <code class=\"language-text\">StaticQuery</code>?</h2>\n<p>Short answer, anywhere. You just import and it works. A few examples where <code class=\"language-text\">StaticQuery</code> would be useful:</p>\n<ul>\n<li>Passing the global site metadata to Helmet</li>\n<li>Showing a list of the 5 most recent posts</li>\n<li>Displaying the 5 best selling products in a store</li>\n</ul>\n<h2>Example</h2>\n<p>Enough theory, time to dive into some code. Here's the example from the <a href=\"https://next.gatsbyjs.org/docs/static-query/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><code class=\"language-text\">StaticQuery</code> docs</a>:</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">import</span> React <span class=\"token keyword\">from</span> <span class=\"token string\">\"react\"</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> StaticQuery<span class=\"token punctuation\">,</span> graphql <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">\"gatsby\"</span>\n\n<span class=\"token keyword\">const</span> <span class=\"token function-variable function\">Header</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">(</span>\n  <span class=\"token operator\">&lt;</span>StaticQuery\n    query<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>graphql<span class=\"token template-string\"><span class=\"token string\">`\n      query {\n        site {\n          siteMetadata {\n            title\n          }\n        }\n      }\n    `</span></span><span class=\"token punctuation\">}</span>\n    render<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>data <span class=\"token operator\">=></span> <span class=\"token punctuation\">(</span>\n      <span class=\"token operator\">&lt;</span>header<span class=\"token operator\">></span>\n        <span class=\"token operator\">&lt;</span>h1<span class=\"token operator\">></span><span class=\"token punctuation\">{</span>data<span class=\"token punctuation\">.</span>site<span class=\"token punctuation\">.</span>siteMetadata<span class=\"token punctuation\">.</span>title<span class=\"token punctuation\">}</span><span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>h1<span class=\"token operator\">></span>\n      <span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>header<span class=\"token operator\">></span>\n    <span class=\"token punctuation\">)</span><span class=\"token punctuation\">}</span>\n  <span class=\"token operator\">/</span><span class=\"token operator\">></span>\n<span class=\"token punctuation\">)</span>\n\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">default</span> Header</code></pre></div>\n<p>Let's unpack that a bit.</p>\n<ul>\n<li>First we import <code class=\"language-text\">StaticQuery</code> from <code class=\"language-text\">gatsby</code>. It's a named export.</li>\n<li>Then we render <code class=\"language-text\">&lt;StaticQuery</code> just like any other React component</li>\n<li>It accepts two props, <code class=\"language-text\">query</code> and <code class=\"language-text\">render</code></li>\n<li>\n<p>The <code class=\"language-text\">query</code> prop takes a <code class=\"language-text\">graphql</code> template string</p>\n<ul>\n<li>NOTE: <code class=\"language-text\">graphql</code> must be imported from <code class=\"language-text\">gatsby</code> in Gatsby v2</li>\n</ul>\n</li>\n<li>\n<p>The <code class=\"language-text\">render</code> prop takes a function</p>\n<ul>\n<li>That function accepts a single argument <code class=\"language-text\">data</code></li>\n<li>The <code class=\"language-text\">data</code> argument is filled with the result of the GraphQL query</li>\n<li>We render an <code class=\"language-text\">h1</code> tag containing <code class=\"language-text\">data.site.siteMetadata.title</code></li>\n</ul>\n</li>\n</ul>\n<blockquote>\n<p>NOTE: The <code class=\"language-text\">data</code> argument passed to the <code class=\"language-text\">render</code> function is not <code class=\"language-text\">props</code>, it is <strong>only the data</strong>. It's more like <code class=\"language-text\">props.data</code>, and it should not be destructured like <code class=\"language-text\">({ data }) =&gt; ...</code>.</p>\n</blockquote>\n<p>If you haven't seen it before, the <code class=\"language-text\">siteMetadata</code> object is filled from <code class=\"language-text\">gatsby-config.js</code>. It's a convenient way to pass config values into your Gatsby site without hard coding them.</p>","excerpt":"Part of  a series on Gatsby v2 You've heard about   as part of Gatsby v2. GraphQL everywhere. Sounds great, but how does it work? tl;dr…","timeToRead":2,"frontmatter":{"date":"July 18, 2018","path":"/staticquery-in-gatsby-v2","title":"StaticQuery in Gatsby v2"}},"relatedPosts":{"edges":[{"node":{"id":"88e68246-cc05-58c4-9cb0-9fa48bc65259","frontmatter":{"title":"Gatsby v2 refactoring cheat sheet","path":"/gatsby-v2-refactoring-cheat-sheet","tags":["v2","cheat sheet"]}}},{"node":{"id":"1aef1790-d364-5a28-ade8-d6196727902c","frontmatter":{"title":"Getting started with Gatsby v2","path":"/getting-started-with-gatsby-v2","tags":["v2"]}}},{"node":{"id":"c8ef7f7d-dffc-5aae-b6a0-0084762b39f0","frontmatter":{"title":"Gatsby v2","path":"/gatsby-v2","tags":["v2","beta"]}}},{"node":{"id":"43eb2d0b-dabd-5c3d-8f96-41eeffd1c003","frontmatter":{"title":"How do layouts work in Gatsby v2","path":"/how-do-layouts-work-in-gatsby-v2","tags":["v2"]}}},{"node":{"id":"53048efa-8e60-5988-8f54-73bf441a1ab2","frontmatter":{"title":"Should I rebuild my site for Gatsby v2?","path":"/should-i-rebuild-my-site-for-gatsby-v2","tags":["v2"]}}},{"node":{"id":"3a89bcb1-591a-5b78-bb39-6a8488d0ce69","frontmatter":{"title":"What's new in Gatsby v2","path":"/whats-new-in-gatsby-v2","tags":["v2","beta"]}}},{"node":{"id":"9a16f7fc-6fa5-5328-816c-35542c1c38ec","frontmatter":{"title":"When should I upgrade to Gatsby v2?","path":"/when-should-i-upgrade-to-gatsby-v2","tags":["v2"]}}},{"node":{"id":"979a29c5-06ed-5582-b0be-4c37f26b65d4","frontmatter":{"title":"Enable absolute imports for Gatsby v2","path":"/enable-absolute-imports-for-gatsby-v2","tags":["v2"]}}},{"node":{"id":"5eb98a7e-3fd2-5edf-a9f8-44f5e25da39d","frontmatter":{"title":"GatsbyJS source plugin for eventbrite.com","path":"/gatsby-source-eventbrite","tags":["v2","source","eventbrite"]}}},{"node":{"id":"374bf18f-0f86-54c8-bdb1-f051527d8210","frontmatter":{"title":"WordPress starter for GatsbyJS","path":"/gatsby-starter-wordpress","tags":["v2","starter","wordpress"]}}},{"node":{"id":"737d2352-faae-5e91-b298-07399aaf3b1c","frontmatter":{"title":"Starter project for GatsbyJS","path":"/gatsby-central-starter","tags":["v2","starter"]}}}]},"allCommentsJson":null,"allRatingsJson":{"totalCount":6,"edges":[{"node":{"id":"bba73309-2d45-5b5f-af60-c055afc49c95","rating":"5"}},{"node":{"id":"dd3d6a6d-ecae-5aec-9e25-b042f3f63333","rating":"1"}},{"node":{"id":"6b0099fd-f0d6-51b5-87a4-d9d71bcd3e6e","rating":"3"}},{"node":{"id":"cdcdc253-3296-554a-aa99-390b70f2ceef","rating":"5"}},{"node":{"id":"2fa0bc67-e5c9-5203-8f1e-d2d9d66311a9","rating":"5"}},{"node":{"id":"969a4f88-4762-508d-96bf-1baf8d16260b","rating":"1"}}]}},"pageContext":{"tags":["v2"]}}