{"data":{"markdownRemark":{"html":"<p>You want to use the same query in multiple components. Or maybe you want to use queries outside of pages / layouts. Well, you can't, but you can use fragments!</p>\n<blockquote>\n<p><strong>tl;dr</strong> - Use fragments. Export fragments from any component. Use them anywhere.</p>\n</blockquote>\n<h2>Sharing queries</h2>\n<p>You have a blog. You want to have a list of recent posts in the sidebar. That sidebar is a separate component. You can only use queries in pages or layouts. You need to pass data to components from layouts or pages.</p>\n<p>You cannot share queries. But you can do something similar with GraphQL fragments.</p>\n<h2>Introducing fragments</h2>\n<p>A fragment defines some fields that you want to retrieve from a type. GraphQL organises data according to types. You can start by just using fragments. They will make more sense once you've used them a few times.</p>\n<p>For example, if you want the site title, your query might look like this:</p>\n<div class=\"gatsby-highlight\" data-language=\"graphql\"><pre class=\"language-graphql\"><code class=\"language-graphql\"><span class=\"token keyword\">query</span> IndexQuery <span class=\"token punctuation\">{</span>\n  site <span class=\"token punctuation\">{</span>\n    siteMetadata <span class=\"token punctuation\">{</span>\n      title\n    <span class=\"token punctuation\">}</span>\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>You could make this a fragment. Then whenever you need the site title, you use the fragment. Your fragment might look like this:</p>\n<div class=\"gatsby-highlight\" data-language=\"graphql\"><pre class=\"language-graphql\"><code class=\"language-graphql\"><span class=\"token keyword\">fragment</span> SiteTitle on Site <span class=\"token punctuation\">{</span>\n  siteMetadata <span class=\"token punctuation\">{</span>\n    title\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>The fragment name is <code class=\"language-text\">SiteTitle</code>. It's a fragment on the type <code class=\"language-text\">Site</code>.</p>\n<p>Then you could change your query. Now it could look like this:</p>\n<div class=\"gatsby-highlight\" data-language=\"graphql\"><pre class=\"language-graphql\"><code class=\"language-graphql\"><span class=\"token keyword\">query</span> IndexQuery <span class=\"token punctuation\">{</span>\n  site <span class=\"token punctuation\">{</span>\n    <span class=\"token operator\">...</span>SiteTitle\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>You use fragments with the spread operator <code class=\"language-text\">...</code>. The effect of this query is exactly the same as the first example. Your site's title will be <code class=\"language-text\">props.data.site.siteMetadata.title</code>.</p>\n<h2>Exporting fragments</h2>\n<p>You can define fragments in any of your components. You just export the fragment as a \"named export\". If you export it, Gatsby will find it. Then you can use it anywhere.</p>\n<blockquote>\n<p>Gatsby searches all your files for fragments first. Then it builds your site. You don't need to think about defining fragments before you use them. Gatsby takes care of that for you. Define anywhere, use anywhere.</p>\n</blockquote>\n<p>Your fragment must follow the export syntax 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> variableName <span class=\"token operator\">=</span> graphql<span class=\"token template-string\"><span class=\"token string\">`\nfragment FragName on TypeName { ... }\n`</span></span></code></pre></div>\n<p>The <code class=\"language-text\">variableName</code> can be anything you want.</p>\n<h2>Define once, use many</h2>\n<p>You have one component that you want to use in different places. You must pass the data either from a layout or a page. If you use the same component in multiple pages you will have multiple queries. Those queries must always fetch the same data.</p>\n<p>You can define the fragment in the same file as your component. Then use the fragment to fetch the data in your page or layout query. Pass the data from the layout or page to the component.</p>\n<p>You want to fetch an extra field. Instead of changing your queries, just change the fragment. Then the data will automatically flow from query to page / layout to your component.</p>","excerpt":"You want to use the same query in multiple components. Or maybe you want to use queries outside of pages / layouts. Well, you can't, but you…","timeToRead":2,"frontmatter":{"date":"February 27, 2018","path":"/reusable-graphql-queries-in-gatsby","title":"Reusable GraphQL queries in Gatsby"}},"relatedPosts":null,"allCommentsJson":{"edges":[{"node":{"id":"78124f9d-53f8-5eca-be1c-43b1599e3ad7","name":"Kevin McDonald","date":1536205054,"fields":{"messageHtml":"<p>This post was super helpful. I’ve been hacking around learning gatsby and graphql wondering what the starters where doing with fragments. This post explained it.</p>"}}}]},"allRatingsJson":{"totalCount":4,"edges":[{"node":{"id":"aebd86ab-4ab7-540c-92a9-2538d78888bc","rating":"5"}},{"node":{"id":"7c680db4-bbef-583a-be8d-db13cf1f63f5","rating":"5"}},{"node":{"id":"75d9868d-46eb-54fc-8bc0-72bf5449aa34","rating":"5"}},{"node":{"id":"f03ac5a5-cf54-544f-9744-3f10b06ebecc","rating":"5"}}]}},"pageContext":{"tags":[]}}