Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: remove Promise clutter from createPages #10791

Merged
merged 1 commit into from
Jan 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 44 additions & 51 deletions examples/gatsbygram/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const _ = require(`lodash`)
const Promise = require(`bluebird`)
const path = require(`path`)
const slug = require(`slug`)
const slash = require(`slash`)
Expand All @@ -11,60 +10,54 @@ const slash = require(`slash`)
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions

return new Promise((resolve, reject) => {
// The “graphql” function allows us to run arbitrary
// queries against this Gatsbygram's graphql schema. Think of
// it like Gatsbygram has a built-in database constructed
// from static data that you can run queries against.
//
// Post is a data node type derived from data/posts.json
// which is created when scraping Instagram. “allPostsJson”
// is a "connection" (a GraphQL convention for accessing
// a list of nodes) gives us an easy way to query all
// Post nodes.
resolve(
graphql(
`
{
allPostsJson(limit: 1000) {
edges {
node {
id
}
}
// The “graphql” function allows us to run arbitrary
// queries against this Gatsbygram's graphql schema. Think of
// it like Gatsbygram has a built-in database constructed
// from static data that you can run queries against.
//
// Post is a data node type derived from data/posts.json
// which is created when scraping Instagram. “allPostsJson”
// is a "connection" (a GraphQL convention for accessing
// a list of nodes) gives us an easy way to query all
// Post nodes.
return graphql(
`
{
allPostsJson(limit: 1000) {
edges {
node {
id
}
}
`
).then(result => {
if (result.errors) {
reject(new Error(result.errors))
}
}
`
).then(result => {
if (result.errors) {
throw result.errors
}

// Create image post pages.
const postTemplate = path.resolve(`src/templates/post-page.js`)
// We want to create a detailed page for each
// Instagram post. Since the scrapped Instagram data
// already includes an ID field, we just use that for
// each page's path.
_.each(result.data.allPostsJson.edges, edge => {
// Gatsby uses Redux to manage its internal state.
// Plugins and sites can use functions like "createPage"
// to interact with Gatsby.
createPage({
// Each page is required to have a `path` as well
// as a template component. The `context` is
// optional but is often necessary so the template
// can query data specific to each page.
path: `/${slug(edge.node.id)}/`,
component: slash(postTemplate),
context: {
id: edge.node.id,
},
})
})

return
// Create image post pages.
const postTemplate = path.resolve(`src/templates/post-page.js`)
// We want to create a detailed page for each
// Instagram post. Since the scrapped Instagram data
// already includes an ID field, we just use that for
// each page's path.
_.each(result.data.allPostsJson.edges, edge => {
// Gatsby uses Redux to manage its internal state.
// Plugins and sites can use functions like "createPage"
// to interact with Gatsby.
createPage({
// Each page is required to have a `path` as well
// as a template component. The `context` is
// optional but is often necessary so the template
// can query data specific to each page.
path: `/${slug(edge.node.id)}/`,
component: slash(postTemplate),
context: {
id: edge.node.id,
},
})
)
})
})
}
79 changes: 37 additions & 42 deletions examples/hn/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const _ = require(`lodash`)
const Promise = require(`bluebird`)
const path = require(`path`)
const slash = require(`slash`)

Expand All @@ -9,54 +8,50 @@ const slash = require(`slash`)
// create pages.
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions
return new Promise((resolve, reject) => {
// The “graphql” function allows us to run arbitrary
// queries against local Hacker News graphql schema. Think of
// it like the site has a built-in database constructed
// from the fetched data that you can run queries against.
// The “graphql” function allows us to run arbitrary
// queries against local Hacker News graphql schema. Think of
// it like the site has a built-in database constructed
// from the fetched data that you can run queries against.

// HnStory is a data node type created from the HN API “allHnStory” is a
// "connection" (a GraphQL convention for accessing a list of nodes) gives
// us an easy way to query all HnStory nodes.
graphql(
`
{
allHnStory(limit: 1000) {
edges {
node {
id
}
// HnStory is a data node type created from the HN API “allHnStory” is a
// "connection" (a GraphQL convention for accessing a list of nodes) gives
// us an easy way to query all HnStory nodes.
return graphql(
`
{
allHnStory(limit: 1000) {
edges {
node {
id
}
}
}
`
).then(result => {
if (result.errors) {
reject(result.errors)
}
`
).then(result => {
if (result.errors) {
throw result.errors
}

// Create HN story pages.
const pageTemplate = path.resolve(`./src/templates/story.js`)
// We want to create a detailed page for each
// story page. We'll just use the HN story ID for the slug.
_.each(result.data.allHnStory.edges, edge => {
// Gatsby uses Redux to manage its internal state.
// Plugins and sites can use functions like "createPage"
// to interact with Gatsby.
createPage({
// Each page is required to have a `path` as well
// as a template component. The `context` is
// optional but is often necessary so the template
// can query data specific to each page.
path: `/item/${edge.node.id}/`,
component: slash(pageTemplate),
context: {
id: edge.node.id,
},
})
// Create HN story pages.
const pageTemplate = path.resolve(`./src/templates/story.js`)
// We want to create a detailed page for each
// story page. We'll just use the HN story ID for the slug.
_.each(result.data.allHnStory.edges, edge => {
// Gatsby uses Redux to manage its internal state.
// Plugins and sites can use functions like "createPage"
// to interact with Gatsby.
createPage({
// Each page is required to have a `path` as well
// as a template component. The `context` is
// optional but is often necessary so the template
// can query data specific to each page.
path: `/item/${edge.node.id}/`,
component: slash(pageTemplate),
context: {
id: edge.node.id,
},
})

resolve()
})
})
}
55 changes: 25 additions & 30 deletions examples/sitemap/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,38 @@ const slash = require(`slash`)
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions

return new Promise((resolve, reject) => {
const blogPostTemplate = path.resolve(`src/templates/template-blog-post.js`)
graphql(
`
{
allMarkdownRemark(
limit: 1000
filter: { frontmatter: { draft: { ne: true } } }
) {
edges {
node {
fields {
slug
}
const blogPostTemplate = path.resolve(`src/templates/template-blog-post.js`)
return graphql(
`
{
allMarkdownRemark(
limit: 1000
filter: { frontmatter: { draft: { ne: true } } }
) {
edges {
node {
fields {
slug
}
}
}
}
`
).then(result => {
if (result.errors) {
console.log(result.errors)
reject(result.errors)
}
`
).then(result => {
if (result.errors) {
throw result.errors
}

// Create blog posts pages.
result.data.allMarkdownRemark.edges.forEach(edge => {
createPage({
path: edge.node.fields.slug, // required
component: slash(blogPostTemplate),
context: {
slug: edge.node.fields.slug,
},
})
// Create blog posts pages.
result.data.allMarkdownRemark.edges.forEach(edge => {
createPage({
path: edge.node.fields.slug, // required
component: slash(blogPostTemplate),
context: {
slug: edge.node.fields.slug,
},
})

resolve()
})
})
}
Expand Down
74 changes: 34 additions & 40 deletions examples/using-asciidoc/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const _ = require(`lodash`)
const Promise = require(`bluebird`)
const path = require(`path`)
const slash = require(`slash`)
const { createFilePath } = require(`gatsby-source-filesystem`)
Expand All @@ -10,52 +9,47 @@ const { createFilePath } = require(`gatsby-source-filesystem`)
// create pages.
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions
return new Promise((resolve, reject) => {
// The “graphql” function allows us to run arbitrary
// queries against the local Drupal graphql schema. Think of
// it like the site has a built-in database constructed
// from the fetched data that you can run queries against.
graphql(
`
{
allAsciidoc(limit: 1000) {
edges {
node {
id
fields {
slug
}
// The “graphql” function allows us to run arbitrary
// queries against the local Drupal graphql schema. Think of
// it like the site has a built-in database constructed
// from the fetched data that you can run queries against.
return graphql(
`
{
allAsciidoc(limit: 1000) {
edges {
node {
id
fields {
slug
}
}
}
}
`
).then(result => {
if (result.errors) {
console.log(result)
reject(result.errors)
}
`
).then(result => {
if (result.errors) {
throw result.errors
}

// Create Asciidoc pages.
const articleTemplate = path.resolve(`./src/templates/article.js`)
_.each(result.data.allAsciidoc.edges, edge => {
// Gatsby uses Redux to manage its internal state.
// Plugins and sites can use functions like "createPage"
// to interact with Gatsby.
createPage({
// Each page is required to have a `path` as well
// as a template component. The `context` is
// optional but is often necessary so the template
// can query data specific to each page.
path: edge.node.fields.slug,
component: slash(articleTemplate),
context: {
id: edge.node.id,
},
})
// Create Asciidoc pages.
const articleTemplate = path.resolve(`./src/templates/article.js`)
_.each(result.data.allAsciidoc.edges, edge => {
// Gatsby uses Redux to manage its internal state.
// Plugins and sites can use functions like "createPage"
// to interact with Gatsby.
createPage({
// Each page is required to have a `path` as well
// as a template component. The `context` is
// optional but is often necessary so the template
// can query data specific to each page.
path: edge.node.fields.slug,
component: slash(articleTemplate),
context: {
id: edge.node.id,
},
})

resolve()
})
})
}
Expand Down
Loading