{"data":{"markdownRemark":{"html":"<blockquote>\n<p>Familiar with GraphQL already? If not, <a href=\"https://graphql.org/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">start here</a>. Seriously.</p>\n</blockquote>\n<p>You know GraphQL. You're learning Gatsby. How does GraphQL work inside Gatsby?</p>\n<blockquote>\n<p><strong>tl;dr</strong> - Gatsby uses GraphQL in 3 ways. In <code class=\"language-text\">gatsby-node.js</code> via <code class=\"language-text\">graphql().then()</code>. In pages via <code class=\"language-text\">const query = graphql`...`</code>. In any component via <code class=\"language-text\">StaticQuery</code>.</p>\n</blockquote>\n<h2>What data is available?</h2>\n<p>It's really helpful to understand <a href=\"/introduction-to-gatsby-data-flow\">Gatsby data flows</a>. In short, Gatsby ingests data into its nodes system, then you pull data from Gatsby's nodes into your components.</p>\n<p>The easiest way to explore the data is via the GraphiQL. While <code class=\"language-text\">gatsby develop</code> is running you can find it at <a href=\"http://localhost:8000/___graphql\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">http://localhost:8000/___graphql</a>.</p>\n<p>Exactly what your data looks like depends on a few things:</p>\n<ul>\n<li>Which source plugins you have configured</li>\n<li>Any transformations you're making in <code class=\"language-text\">gatsby-node.js</code></li>\n<li>What data is actually found by your source plugins</li>\n</ul>\n<p>Gatsby generates the GraphQL schema automatically from the data it finds. So if a field is always empty, it will not exist.</p>\n<blockquote>\n<p><strong>NOTE</strong>: Watch out for empty fields. Gatsby needs to see your data to be able to determine its shape. Only then is it made available.</p>\n</blockquote>\n<h2>GraphQL in <code class=\"language-text\">gatsby-node.js</code></h2>\n<p>In the Gatsby flow, this is probably the first place you'll use GraphQL. You've installed a source plugin. Data is being loaded. You've explored that data in GraphiQL. You want to use that data to create pages.</p>\n<p>You can use GraphQL queries inside the <code class=\"language-text\">createPages</code> API. You get a <code class=\"language-text\">graphql</code> API. Confusingly, this is not the same as when you <code class=\"language-text\">import { graphql } from &#39;gatsby&#39;;</code>. It's a function which accepts a string and returns a promise. Here's an example of how you might use it.</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\">exports<span class=\"token punctuation\">.</span><span class=\"token function-variable function\">createPages</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> actions<span class=\"token punctuation\">,</span> graphql <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n  <span class=\"token comment\">// Fetch a list of posts</span>\n  <span class=\"token function\">graphql</span><span class=\"token punctuation\">(</span><span class=\"token template-string\"><span class=\"token string\">`query { ... }`</span></span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">then</span><span class=\"token punctuation\">(</span>result <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>result<span class=\"token punctuation\">.</span>errors<span class=\"token punctuation\">)</span> <span class=\"token operator\">...</span>\n\n    <span class=\"token comment\">// Iterate over `result.data` and call `actions.createPage()`</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>This requires some familiarity with Promises to understand. Unfortunately there's no easy way around that. Promises are awesome though, and definitely worth learning a little about.</p>\n<h2>GraphQL in pages</h2>\n<p>Next up, your pages. Of course you want to get data into your pages. One common mistake is to pass all the data for the page to the <code class=\"language-text\">createPage()</code> call. This is a mistake. Use GraphQL page queries instead.</p>\n<p>When you call <code class=\"language-text\">createPage()</code> you pass the path to a React component as the variable <code class=\"language-text\">component</code>. Gatsby then loads that component and supplies it with some context.</p>\n<p>If you have defined a GraphQL query in that component's file, Gatsby will find it. There's some magic going on here. You must export the component like <code class=\"language-text\">export const query = graphql`query { ... }`</code>. The name of the export does not matter.</p>\n<p>Bottom line, create it, make sure it's exported, and Gatsby will magically link it to your component for you.</p>\n<p>The results of your query will be given to your component as <code class=\"language-text\">props.data</code>.</p>\n<p>Here's an abbreviated example.</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><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> graphql <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">\"gatsby\"</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">const</span> <span class=\"token function-variable function\">Page</span> <span class=\"token operator\">=</span> props <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">return</span> <span class=\"token operator\">&lt;</span>div<span class=\"token operator\">></span><span class=\"token punctuation\">{</span><span class=\"token constant\">JSON</span><span class=\"token punctuation\">.</span><span class=\"token function\">stringify</span><span class=\"token punctuation\">(</span>props<span class=\"token punctuation\">.</span>data<span class=\"token punctuation\">)</span><span class=\"token punctuation\">}</span><span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">const</span> query <span class=\"token operator\">=</span> graphql<span class=\"token template-string\"><span class=\"token string\">`\n  query { ... }\n`</span></span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">default</span> Page<span class=\"token punctuation\">;</span></code></pre></div>\n<p>The query is magically linked to the component. No action required.</p>\n<h2>GraphQL everywhere</h2>\n<p>Starting with Gatsby v2, you can now use GraphQL to pass data to any React component anywhere. This is done via <code class=\"language-text\">StaticQuery</code>. <a href=\"/staticquery-in-gatsby-v2\">Read all about <code class=\"language-text\">StaticQuery</code> here</a>.</p>\n<p>A really simple example of how you might use this, just for kicks.</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><span class=\"token punctuation\">;</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><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">const</span> <span class=\"token function-variable function\">Header</span> <span class=\"token operator\">=</span> props <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">return</span> <span class=\"token punctuation\">(</span>\n    <span class=\"token operator\">&lt;</span>div<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>div<span class=\"token operator\">></span>\n  <span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">const</span> <span class=\"token function-variable function\">HeaderWithStaticData</span> <span class=\"token operator\">=</span> props <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">return</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 operator\">&lt;</span>Header data<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>data<span class=\"token punctuation\">}</span> <span class=\"token punctuation\">{</span><span class=\"token operator\">...</span>props<span class=\"token punctuation\">}</span> <span class=\"token operator\">/</span><span class=\"token operator\">></span><span class=\"token punctuation\">}</span>\n    <span class=\"token operator\">/</span><span class=\"token operator\">></span>\n  <span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">default</span> HeaderWithStaticData<span class=\"token punctuation\">;</span></code></pre></div>\n<p>There are a few things to note in this example. Check out the <a href=\"/staticquery-in-gatsby-v2\">article on <code class=\"language-text\">StaticQuery</code></a> to understand them.</p>\n<h2>GraphQL in Gatsby</h2>\n<p>I hope this has given you a useful introduction to how GraphQL is used in Gatsby. Feel free to reach out in the comments or on twitter if you have questions or feedback. Subscribe below if you want to receive Gatsby awesomeness directly to your inbox.</p>","excerpt":"Familiar with GraphQL already? If not,  start here . Seriously. You know GraphQL. You're learning Gatsby. How does GraphQL work inside…","timeToRead":3,"frontmatter":{"date":"July 20, 2018","path":"/gatsby-graphql-an-introduction","title":"Gatsby GraphQL - An Introduction"}},"relatedPosts":null,"allCommentsJson":null,"allRatingsJson":{"totalCount":2,"edges":[{"node":{"id":"985f2ef2-5ffb-5476-83df-06367345846d","rating":"4"}},{"node":{"id":"40515d69-e3f2-5877-ba66-5dfeeb602e41","rating":"5"}}]}},"pageContext":{"tags":[]}}