Created
April 15, 2018 13:11
-
-
Save clarkdave/53cc050fa58d9a70418f8a76982dd6c8 to your computer and use it in GitHub Desktop.
TypeScript + Gatsby node API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | |
}, | |
}) | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
require('source-map-support').install() | |
require('ts-node').register({ | |
compilerOptions: { | |
module: 'commonjs', | |
target: 'es2017', | |
}, | |
}) | |
exports.createPages = require('./createPages') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Thanks @JohnAlbin! I've linked to your gist from this Gatsby issue.
@crhistianramirez same - it fixes the
TypeError: gatsbyNode[api] is not a function
problems
@crhistianramirez please, could you explain why your change fixed this error?
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
@clarkdave, Thanks for sharing this and getting the conversation started.
We can simplify our Gatsby JavaScript files even more, since that JS file can re-export all the exports of a TypeScript file in one line with:
That means we can have a single TypeScript file with all of our Gatsby API exports. And if we add new API calls to our TypeScript file, they get automatically exported by the JavaScript file without needing to modify it.
Also,
ts-node
now includessource-map-support
. https://github.com/TypeStrong/ts-node/blob/be2c899ca796bd6dc54e074fdb2c5a32df51213d/src/index.ts#L407With:
module.exports
andsource-map-support
,boundActionCreators
are now deprecated,require('ts-node').register()
can be put ingatsby-config.js
and it will apply to all other gatsby API files, likegatsby-node.js
.gatsby-node.ts
instead ofgatsby-node.js
.I went ahead and made all those updates in this forked gist: https://gist.github.com/JohnAlbin/2fc05966624dffb20f4b06b4305280f9