Skip to content

Commit f8f52ba

Browse files
jiahaogKyleAMathews
authored andcommitted
Document how to create custom fragments (#3914)
* Document how to create custom fragments * Fix ordering * Add example component exporting a fragment * Tweak language
1 parent 5160b03 commit f8f52ba

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed

docs/docs/querying-with-graphql.md

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,147 @@ See also the following blog posts:
199199
* [Making Website Building Fun](/blog/2017-10-16-making-website-building-fun/)
200200
* [Image Optimization Made Easy with Gatsby.js](https://medium.com/@kyle.robert.gill/ridiculously-easy-image-optimization-with-gatsby-js-59d48e15db6e)
201201
202+
## Advanced
203+
204+
### Fragments
205+
206+
Notice that in the above example for [querying images](#images), we used `...GatsbyImageSharpResolutions`, which is a GraphQL Fragment, a reusable set of fields for query composition. You can read more about them [here](http://graphql.org/learn/queries/#fragments).
207+
208+
If you wish to define your own fragments for use in your application, you can use named exports to export them in any Javascript file, and they will be automatically processed by Gatsby for use in your GraphQL queries.
209+
210+
For example if I put a fragment in a helper component, I can use that fragment in any other query:
211+
212+
```jsx
213+
// src/components/PostItem.js
214+
215+
export const markdownFrontmatterFragment = graphql`
216+
fragment MarkdownFrontmatter on MarkdownRemark {
217+
frontmatter {
218+
path
219+
title
220+
date(formatString: "MMMM DD, YYYY")
221+
}
222+
}
223+
`;
224+
```
225+
226+
They can then be used in any GraphQL query after that!
227+
228+
```graphql
229+
query PostByPath($path: String!) {
230+
markdownRemark(frontmatter: { path: { eq: $path } }) {
231+
...MarkdownFrontmatter
232+
}
233+
}
234+
```
235+
236+
It’s good practice for your helper components to define and export a fragment for the data they need. For example, on your index page might map over all of your posts to show them in a list.
237+
238+
```jsx
239+
// src/pages/index.jsx
240+
241+
import React from "react";
242+
243+
export default ({ data }) => {
244+
return (
245+
<div>
246+
<h1>
247+
Index page
248+
</h1>
249+
<h4>{data.allMarkdownRemark.totalCount} Posts</h4>
250+
{data.allMarkdownRemark.edges.map(({ node }) => (
251+
<div key={node.id}>
252+
<h3>
253+
{node.frontmatter.title}{" "}
254+
<span>— {node.frontmatter.date}</span>
255+
</h3>
256+
</div>
257+
))}
258+
</div>
259+
);
260+
};
261+
262+
export const query = graphql`
263+
query IndexQuery {
264+
allMarkdownRemark {
265+
totalCount
266+
edges {
267+
node {
268+
id
269+
frontmatter {
270+
title
271+
date(formatString: "DD MMMM, YYYY")
272+
}
273+
}
274+
}
275+
}
276+
}
277+
`;
278+
```
279+
280+
If the index component becomes too large, you might want to refactor it into smaller components.
281+
282+
```jsx
283+
// src/components/IndexPost.jsx
284+
285+
import React from "react";
286+
287+
export default ({ frontmatter: { title, date } }) => (
288+
<div>
289+
<h3>
290+
{title} <span>— {date}</span>
291+
</h3>
292+
</div>
293+
);
294+
295+
export const query = graphql`
296+
fragment IndexPostFragment on MarkdownRemark {
297+
frontmatter {
298+
title
299+
date(formatString: "MMMM DD, YYYY")
300+
}
301+
}
302+
`;
303+
```
304+
305+
Now, we can use the component together with the exported fragment in our index page.
306+
307+
```jsx{28}
308+
// src/pages/index.jsx
309+
310+
import React from "react";
311+
import IndexPost from "../components/IndexPost";
312+
313+
export default ({ data }) => {
314+
return (
315+
<div>
316+
<h1>
317+
Index page
318+
</h1>
319+
<h4>{data.allMarkdownRemark.totalCount} Posts</h4>
320+
{data.allMarkdownRemark.edges.map(({ node }) => (
321+
<div key={node.id}>
322+
<IndexPost frontmatter={node.frontmatter} />
323+
</div>
324+
))}
325+
</div>
326+
);
327+
};
328+
329+
export const query = graphql`
330+
query IndexQuery {
331+
allMarkdownRemark {
332+
totalCount
333+
edges {
334+
node {
335+
...IndexPostFragment
336+
}
337+
}
338+
}
339+
}
340+
`;
341+
```
342+
202343
## Further reading
203344
204345
### Getting started with GraphQL

0 commit comments

Comments
 (0)