Skip to content

Commit 54abc5f

Browse files
browniebrokegillkyle
authored andcommitted
chore(example): Migrate gatsbygram's createPages hook to async/await (gatsbyjs#15608)
1 parent 8f44086 commit 54abc5f

File tree

1 file changed

+27
-27
lines changed

1 file changed

+27
-27
lines changed

examples/gatsbygram/gatsby-node.js

+27-27
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const slash = require(`slash`)
77
// called after the Gatsby bootstrap is finished so you have
88
// access to any information necessary to programmatically
99
// create pages.
10-
exports.createPages = ({ graphql, actions }) => {
10+
exports.createPages = async ({ graphql, actions }) => {
1111
const { createPage } = actions
1212

1313
// The “graphql” function allows us to run arbitrary
@@ -20,7 +20,7 @@ exports.createPages = ({ graphql, actions }) => {
2020
// is a "connection" (a GraphQL convention for accessing
2121
// a list of nodes) gives us an easy way to query all
2222
// Post nodes.
23-
return graphql(
23+
const result = await graphql(
2424
`
2525
{
2626
allPostsJson(limit: 1000) {
@@ -32,32 +32,32 @@ exports.createPages = ({ graphql, actions }) => {
3232
}
3333
}
3434
`
35-
).then(result => {
36-
if (result.errors) {
37-
throw result.errors
38-
}
35+
)
3936

40-
// Create image post pages.
41-
const postTemplate = path.resolve(`src/templates/post-page.js`)
42-
// We want to create a detailed page for each
43-
// Instagram post. Since the scraped Instagram data
44-
// already includes an ID field, we just use that for
45-
// each page's path.
46-
_.each(result.data.allPostsJson.edges, edge => {
47-
// Gatsby uses Redux to manage its internal state.
48-
// Plugins and sites can use functions like "createPage"
49-
// to interact with Gatsby.
50-
createPage({
51-
// Each page is required to have a `path` as well
52-
// as a template component. The `context` is
53-
// optional but is often necessary so the template
54-
// can query data specific to each page.
55-
path: `/${slug(edge.node.id)}/`,
56-
component: slash(postTemplate),
57-
context: {
58-
id: edge.node.id,
59-
},
60-
})
37+
if (result.errors) {
38+
throw new Error(result.errors)
39+
}
40+
41+
// Create image post pages.
42+
const postTemplate = path.resolve(`src/templates/post-page.js`)
43+
// We want to create a detailed page for each
44+
// Instagram post. Since the scraped Instagram data
45+
// already includes an ID field, we just use that for
46+
// each page's path.
47+
_.each(result.data.allPostsJson.edges, edge => {
48+
// Gatsby uses Redux to manage its internal state.
49+
// Plugins and sites can use functions like "createPage"
50+
// to interact with Gatsby.
51+
createPage({
52+
// Each page is required to have a `path` as well
53+
// as a template component. The `context` is
54+
// optional but is often necessary so the template
55+
// can query data specific to each page.
56+
path: `/${slug(edge.node.id)}/`,
57+
component: slash(postTemplate),
58+
context: {
59+
id: edge.node.id,
60+
},
6161
})
6262
})
6363
}

0 commit comments

Comments
 (0)