Skip to content

Commit ae6b958

Browse files
authored
Add example component exporting a fragment
1 parent 5977f06 commit ae6b958

File tree

1 file changed

+111
-3
lines changed

1 file changed

+111
-3
lines changed

docs/docs/querying-with-graphql.md

Lines changed: 111 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,11 @@ If you wish to define your own fragments for use in your application, you can us
209209
210210
For example, I can put all of my helper fragments in `src/fragments/index.js`:
211211
212-
```javascript
212+
```jsx
213213
// src/fragments/index.js
214214
215215
export const markdownFrontmatterFragment = graphql`
216-
fragment MarkdownFrontmatterFragment on MarkdownRemark {
216+
fragment MarkdownFrontmatter on MarkdownRemark {
217217
frontmatter {
218218
path
219219
title
@@ -228,11 +228,119 @@ They can then be used in any GraphQL query after that!
228228
```graphql
229229
query PostByPath($path: String!) {
230230
markdownRemark(frontmatter: { path: { eq: $path } }) {
231-
...MarkdownMetadataFragment
231+
... MarkdownFrontmatter
232232
}
233233
}
234234
```
235235
236+
It’s good practice for your helper components to define and export a fragment for the data they need though. For example, on your index page might map over all of your posts to show them in a list.
237+
238+
```jsx{14-17,31-34}
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}{" "}
291+
<span>— {date}</span>
292+
</h3>
293+
</div>
294+
)
295+
296+
export const query = graphql`
297+
fragment IndexPostFragment on MarkdownRemark {
298+
frontmatter {
299+
title
300+
date(formatString: "MMMM DD, YYYY")
301+
}
302+
}
303+
`;
304+
```
305+
306+
Now, we can use the component together with the exported fragment in our index page.
307+
308+
```jsx{15}
309+
// src/pages/index.jsx
310+
311+
import React from "react";
312+
import IndexPost from "../components/IndexPost";
313+
314+
export default ({ data }) => {
315+
return (
316+
<div>
317+
<h1>
318+
Index page
319+
</h1>
320+
<h4>{data.allMarkdownRemark.totalCount} Posts</h4>
321+
{data.allMarkdownRemark.edges.map(({ node }) => (
322+
<div key={node.id}>
323+
<IndexPost frontmatter={node.frontmatter} />
324+
</div>
325+
))}
326+
</div>
327+
);
328+
};
329+
330+
export const query = graphql`
331+
query IndexQuery {
332+
allMarkdownRemark {
333+
totalCount
334+
edges {
335+
node {
336+
...IndexPostFragment
337+
}
338+
}
339+
}
340+
}
341+
`;
342+
```
343+
236344
## Further reading
237345
238346
### Getting started with GraphQL

0 commit comments

Comments
 (0)