Skip to content

Instantly share code, notes, and snippets.

@clarkdave
Created April 15, 2018 13:11
Show Gist options
  • Star 44 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save clarkdave/53cc050fa58d9a70418f8a76982dd6c8 to your computer and use it in GitHub Desktop.
Save clarkdave/53cc050fa58d9a70418f8a76982dd6c8 to your computer and use it in GitHub Desktop.
TypeScript + Gatsby node API
import { resolve } from 'path'
import { GatsbyCreatePages } from './types'
const createPages: GatsbyCreatePages = async ({
graphql,
boundActionCreators,
}) => {
const { createPage } = boundActionCreators
const allMarkdown = await graphql(`
{
allMarkdownRemark(limit: 1000) {
edges {
node {
fields {
slug
}
}
}
}
}
`)
allMarkdown.data.allMarkdownRemark.edges.forEach(edge => {
const { slug } = edge.node.fields
if (!slug) return
// type safe `createPage` call
createPage({
path: slug,
component: resolve(__dirname, '../src/templates/index.tsx'),
context: {
slug,
},
})
})
}
'use strict'
require('source-map-support').install()
require('ts-node').register({
compilerOptions: {
module: 'commonjs',
target: 'es2017',
},
})
exports.createPages = require('./createPages')
interface PageInput {
path: string
component: string
layout?: string
context?: any
}
interface BoundActionCreators {
createPage: (page: PageInput) => void
deletePage: (page: PageInput) => void
createRedirect: (
opts: {
fromPath: string
isPermanent?: boolean
redirectInBrowser?: boolean
toPath: string
}
) => void
}
export type GatsbyCreatePages = (
fns: { graphql: any; boundActionCreators: BoundActionCreators }
) => void
@dillionmegida
Copy link

@crhistianramirez same - it fixes the TypeError: gatsbyNode[api] is not a function problems

@crhistianramirez please, could you explain why your change fixed this error?

@fzyzcjy
Copy link

fzyzcjy commented Feb 4, 2022

Hi, shall we write types.ts or types.d.ts? IMHO maybe that suits the definition of d.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment