Node API Helpers
The first argument passed to each of Gatsby’s Node APIs is an object containing a set of helpers. Helpers shared by all Gatsby’s Node APIs are documented in Shared helpers section.
Common convention is to destructure helpers right in argument list:
Note
Some APIs provide additional helpers. For example createPages
provides graphql
function. Check documentation of specific APIs in Gatsby Node APIs for details.
Shared helpers
Fields
get
(key: string) => Promise<any>Retrieve cached value
Parameters
key
stringCache key
Return value
Promise<any>Promise resolving to cached value
Example
const value = await cache.get(`unique-key`)
set
(key: string, value: any) => Promise<any>Cache value
Parameters
key
stringCache key
value
anyValue to be cached
Return value
Promise<any>Promise resolving to cached value
Example
await cache.set(`unique-key`, value)
del
(key: string) => Promise<void>Deletes cached value
Parameters
key
stringCache key
Return value
Promise<void>Promise resolving once key is deleted from cache
Example
await cache.del(`unique-key`)
Fields
info
(message: string) => voidParameters
message
stringMessage to display
Return value
voidExample
reporter.info(`text`)
warn
(message: string) => voidParameters
message
stringMessage to display
Return value
voidExample
reporter.warn(`text`)
error
(message: string, error?: Error) => voidParameters
message
stringMessage to display
error
ErrorOptional error object
Return value
voidExample
reporter.error(`text`, new Error('something'))
panic
(message: string, error?: Error) => voidParameters
message
stringMessage to display
error
ErrorOptional error object
Return value
voidExample
reporter.panic(`text`, new Error('something'))
panicOnBuild
(message: string, error?: Error) => voidParameters
message
stringMessage to display
error
ErrorOptional error object
Return value
voidExample
reporter.panicOnBuild(`text`, new Error('something'))
verbose
(message: string) => voidNote that this method only works if the —verbose option has been passed to the CLI
Parameters
message
stringMessage to display
Return value
voidExample
reporter.verbose(`text`)
activityTimer
(message: string) => ITimerReporterCreates a new activity timer with the provided message. Check the full return type definition here.
Parameters
message
stringTimer message to display
Return value
ITimerReporterExample
const activity = reporter.activityTimer(`Timer text`) activity.start() activity.setStatus(`status text`) activity.end()
Fields
tracer
Opentracing.TracerGlobal tracer instance. Check opentracing Tracer documentation for more details.
parentSpan
Opentracing.SpanTracer span representing API run. Check opentracing Span documentation for more details.
startSpan
(spanName: string) => Opentracing.SpanStart a tracing span. The span will be created as a child of the currently running API span. This is a convenience wrapper for
tracing.tracer.startSpan(`span-name`, { childOf: tracing.parentSpan}).
Parameters
spanName
stringname of the span
Return value
Opentracing.SpanExample
exports.sourceNodes = async ({ actions, tracing }) => { const span = tracing.startSpan(`foo`) // Perform any span operations. E.g. add a tag to your span span.setTag(`bar`, `baz`) // Rest of your plugin code span.finish() }
basePath
string
basePath
stringThis is the same as pathPrefix
passed in gatsby-config.js
.
It’s an empty string if you don’t pass pathPrefix
.
When using assetPrefix, you can use this instead of pathPrefix to recieve the string you set in gatsby-config.js
.
It won’t include the assetPrefix
.
cache
GatsbyCache
cache
GatsbyCacheKey-value store used to persist results of time/memory/cpu intensive tasks. All functions are async and return promises.
Fields
get
(key: string) => Promise<any>Retrieve cached value
Parameters
key
stringCache key
Return value
Promise<any>Promise resolving to cached value
Example
const value = await cache.get(`unique-key`)
set
(key: string, value: any) => Promise<any>Cache value
Parameters
key
stringCache key
value
anyValue to be cached
Return value
Promise<any>Promise resolving to cached value
Example
await cache.set(`unique-key`, value)
del
(key: string) => Promise<void>Deletes cached value
Parameters
key
stringCache key
Return value
Promise<void>Promise resolving once key is deleted from cache
Example
await cache.del(`unique-key`)
createContentDigest
Function
(input: string | object) => stringcreateContentDigest
FunctionCreate a stable content digest from a string or object, you can use the
result of this function to set the internal.contentDigest
field
on nodes. Gatsby uses the value of this field to invalidate stale data
when your content changes.
Parameters
input
string | object
Return value
string
Hash string
Example
const node = {
...nodeData,
internal: {
type: `TypeOfNode`,
contentDigest: createContentDigest(nodeData)
}
}
createNodeId
Function
(input: string) => stringcreateNodeId
FunctionUtility function useful to generate globally unique and stable node IDs. It will generate different IDs for different plugins if they use same input.
Parameters
input
string
Return value
string
UUIDv5 ID string
Example
const node = {
id: createNodeId(`${backendData.type}${backendData.id}`),
...restOfNodeData
}
emitter
Emitter
emitter
EmitterInternal event emitter / listener. Do not use, unless you absolutely must. Emitter is considered a private API and can change with any version.
getNode
Function
(ID: string) => NodegetNode
FunctionGet single node by given ID.
Don’t use this in graphql resolvers - see
getNodeAndSavePathDependency
.
Parameters
ID
stringid of the node.
Return value
Node
Single node instance.
Example
const node = getNode(id)
getNodeAndSavePathDependency
Function
(ID: string, path: string) => NodegetNodeAndSavePathDependency
FunctionGet single node by given ID and create dependency for given path.
This should be used instead of getNode
in graphql resolvers to enable
tracking dependencies for query results. If it’s not used Gatsby will
not rerun query if node changes leading to stale query results. See
Page -> Node Dependency Tracking
for more details.
Parameters
ID
stringid of the node.
path
stringof the node.
Return value
Node
Single node instance.
getNodes
Function
() => Node[]getNodes
FunctionGet array of all nodes.
Return value
Node[]
Array of nodes.
Example
const allNodes = getNodes()
getNodesByType
Function
(Type: string) => Node[]getNodesByType
FunctionGet array of nodes of given type.
Parameters
Type
stringof nodes
Return value
Node[]
Array of nodes.
Example
const markdownNodes = getNodesByType(`MarkdownRemark`)
loadNodeContent
Function
(node: Node) => Promise<string>loadNodeContent
FunctionGet content for a node from the plugin that created it.
Parameters
node
Node
Return value
Promise<string>
Example
module.exports = async function onCreateNode(
{ node, loadNodeContent, actions, createNodeId }
) {
if (node.internal.mediaType === 'text/markdown') {
const { createNode, createParentChildLink } = actions
const textContent = await loadNodeContent(node)
// process textContent and create child nodes
}
}
pathPrefix
string
pathPrefix
stringUse to prefix resources URLs. pathPrefix
will be either empty string or
path that starts with slash and doesn’t end with slash. pathPrefix
also
becomes <assetPrefix>/<pathPrefix>
when you pass both assetPrefix
and
pathPrefix
in your gatsby-config.js
.
See Adding a Path Prefix page for details about path prefixing.
reporter
GatsbyReporter
reporter
GatsbyReporterSet of utilities to output information to user
Fields
info
(message: string) => voidParameters
message
stringMessage to display
Return value
voidExample
reporter.info(`text`)
warn
(message: string) => voidParameters
message
stringMessage to display
Return value
voidExample
reporter.warn(`text`)
error
(message: string, error?: Error) => voidParameters
message
stringMessage to display
error
ErrorOptional error object
Return value
voidExample
reporter.error(`text`, new Error('something'))
panic
(message: string, error?: Error) => voidParameters
message
stringMessage to display
error
ErrorOptional error object
Return value
voidExample
reporter.panic(`text`, new Error('something'))
panicOnBuild
(message: string, error?: Error) => voidParameters
message
stringMessage to display
error
ErrorOptional error object
Return value
voidExample
reporter.panicOnBuild(`text`, new Error('something'))
verbose
(message: string) => voidNote that this method only works if the —verbose option has been passed to the CLI
Parameters
message
stringMessage to display
Return value
voidExample
reporter.verbose(`text`)
activityTimer
(message: string) => ITimerReporterCreates a new activity timer with the provided message. Check the full return type definition here.
Parameters
message
stringTimer message to display
Return value
ITimerReporterExample
const activity = reporter.activityTimer(`Timer text`) activity.start() activity.setStatus(`status text`) activity.end()
store
Redux.Store
store
Redux.StoreInternal redux state used for application state. Do not use, unless you absolutely must. Store is considered a private API and can change with any version.
tracing
GatsbyTracing
tracing
GatsbyTracingSet of utilities that allow adding more detailed tracing for plugins. Check Performance tracing page for more details.
Fields
tracer
Opentracing.TracerGlobal tracer instance. Check opentracing Tracer documentation for more details.
parentSpan
Opentracing.SpanTracer span representing API run. Check opentracing Span documentation for more details.
startSpan
(spanName: string) => Opentracing.SpanStart a tracing span. The span will be created as a child of the currently running API span. This is a convenience wrapper for
tracing.tracer.startSpan(`span-name`, { childOf: tracing.parentSpan}).
Parameters
spanName
stringname of the span
Return value
Opentracing.SpanExample
exports.sourceNodes = async ({ actions, tracing }) => { const span = tracing.startSpan(`foo`) // Perform any span operations. E.g. add a tag to your span span.setTag(`bar`, `baz`) // Rest of your plugin code span.finish() }