{"data":{"markdownRemark":{"html":"<p>How can you use data from GraphQL queries in Gatsby layouts? You have data in Gatsby that you want to use inside templates. Is there a layout version of <code class=\"language-text\">pageQuery</code>?</p>\n<blockquote>\n<p><strong>tl;dr</strong> - Your query variable can be called anything. It must be exported. The query must be named. That name must be unique. Data arrives as <code class=\"language-text\">props.data</code>.</p>\n</blockquote>\n<h2><code class=\"language-text\">pageQuery</code> is not special</h2>\n<p>Every Gatsby example I saw called the query <code class=\"language-text\">pageQuery</code>. I thought that <code class=\"language-text\">pageQuery</code> was a \"magic\" name. I assumed it only worked in page components. I thought queries only worked in pages. I was wrong.</p>\n<p>I dug into the Gatsby code. I ran some experiments. I eventually figured it out. You can call the query anything you want. It can be <code class=\"language-text\">myQuery</code>, <code class=\"language-text\">componentQuery</code>, <code class=\"language-text\">fooBar</code> or anything else.</p>\n<h2>The query must be exported</h2>\n<p>My next thought was, does it need to be exported? Short answer, yes.</p>\n<p>I tried removing the <code class=\"language-text\">export</code>. Gatsby would build, but no data was reaching my components. I wasted a few hours. I added back the <code class=\"language-text\">export</code> and my data was back. Great.</p>\n<p>The export must be like this:</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">export</span> <span class=\"token keyword\">const</span> queryVariable <span class=\"token operator\">=</span> graphql<span class=\"token template-string\"><span class=\"token string\">`QUERY`</span></span></code></pre></div>\n<blockquote>\n<p>Aside. Queries are parsed <a href=\"https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby/src/internal-plugins/query-runner/file-parser.js#L81\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">here</a>. The parser looks for a named export, then a <code class=\"language-text\">graphql</code> tag. If you break that pattern, the query won't be found. For example you can't use <code class=\"language-text\">exports.query = ...</code>.</p>\n</blockquote>\n<h2>The query must be named</h2>\n<p>You must give each query a unique name. The query name is not the same as the <strong>variable name</strong>.</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">export</span> <span class=\"token keyword\">const</span> queryVariable <span class=\"token operator\">=</span> graphql<span class=\"token template-string\"><span class=\"token string\">`\n query QueryName {...}\n`</span></span></code></pre></div>\n<p>The variable is called <code class=\"language-text\">queryVariable</code> but the query is called <code class=\"language-text\">QueryName</code>. You must ensure that every query has a name.</p>\n<p>Every query name must be unique.</p>\n<p>Gatsby checks this during startup. If you make a mistake, it will complain. The error message should explain what you need to fix.</p>\n<p>NOTE: If you duplicate layouts, remember to change the query name.</p>\n<h2>Queries in templates</h2>\n<p>You can use queries in templates or page components. They work exactly the same way in both cases. You define your query. Remember to export it. Then the results of the query will arrive at <code class=\"language-text\">props.data</code>.</p>\n<h2>Wrap your query in graphql template tag</h2>\n<p>You must wrap your query in the <code class=\"language-text\">graphql</code> template tag like this:</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">export</span> <span class=\"token keyword\">const</span> myQuery <span class=\"token operator\">=</span> graphql<span class=\"token template-string\"><span class=\"token string\">` query goes here `</span></span></code></pre></div>\n<p>This uses a javascript feature called <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">tagged template literals</a>. You don't need to understand all the details of how it works. I've read the spec several times and I still don't get it. You just need to do it!</p>\n<h2>Don't <code class=\"language-text\">import graphql</code></h2>\n<p>You don't need to import <code class=\"language-text\">graphql</code>. It must be imported by Gatsby. I'm not sure, I haven't figured it out yet. But the <code class=\"language-text\">graphql</code> keyword \"just works\", so you can forget about it and move on!</p>\n<h2>Examples</h2>\n<p>You can put all this together like so.</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">export</span> <span class=\"token keyword\">const</span> templateQuery <span class=\"token operator\">=</span> graphql<span class=\"token template-string\"><span class=\"token string\">`\n  query DefaultTemplate {\n    site {\n     siteMetadata {\n       title\n       author\n     }\n   }\n  }\n`</span></span></code></pre></div>\n<p>Then inside your component's <code class=\"language-text\">render()</code> function you can do something like this:</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token operator\">&lt;</span>p<span class=\"token operator\">></span>Post by <span class=\"token punctuation\">{</span><span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>props<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>p<span class=\"token operator\">></span></code></pre></div>","excerpt":"How can you use data from GraphQL queries in Gatsby layouts? You have data in Gatsby that you want to use inside templates. Is there a…","timeToRead":3,"frontmatter":{"date":"February 28, 2018","path":"/graphql-queries-in-gatsby-layouts","title":"GraphQL queries in Gatsby layouts"}},"relatedPosts":null,"allCommentsJson":null,"allRatingsJson":{"totalCount":1,"edges":[{"node":{"id":"d92ff75f-61f3-5ee9-9554-9f7abf299d9e","rating":"5"}}]}},"pageContext":{"tags":[]}}