Node Model
Gatsby exposes its internal data store and query capabilities to GraphQL field resolvers on context.nodeModel.
createResolvers({
Query: {
mood: {
type: `String`,
resolve(source, args, context, info) {
const coffee = context.nodeModel.getAllNodes({ type: `Coffee` })
if (!coffee.length) {
return 😞
}
return 😊
},
},
},
})
Methods
Reference
Get the root ancestor node for an object’s parent node, or its first ancestor matching a specified condition.
Parameters
objObject | ArrayAn object belonging to a Node, or a Node object
predicateFunctionOptional condition to match
Return value
Node | null
Get all nodes in the store, or all nodes of a specified type. Note that this doesn’t add tracking to all the nodes, unless pageDependencies are passed.
Parameters
argsObjecttypestring | GraphQLOutputTypeOptional type of the nodes
pageDependenciesObjectOptional page dependency information.
pathstringThe path of the page that depends on the retrieved nodes’ data
connectionTypestringMark this dependency as a connection
Return value
Node[]
Get a node from the store by ID and optional type.
Parameters
argsObjectidstringID of the requested node
typestring | GraphQLOutputTypeOptional type of the node
pageDependenciesObjectOptional page dependency information.
pathstringThe path of the page that depends on the retrieved nodes’ data
connectionTypestringMark this dependency as a connection
Return value
Node | null
Get nodes from the store by IDs and optional type.
Parameters
argsObjectidsstring[]IDs of the requested nodes
typestring | GraphQLOutputTypeOptional type of the nodes
pageDependenciesObjectOptional page dependency information.
pathstringThe path of the page that depends on the retrieved nodes’ data
connectionTypestringMark this dependency as a connection
Return value
Node[]
Get nodes of a type matching the specified query.
Parameters
argsObjectqueryObjectQuery arguments (
filter,sort,limit,skip)typestring | GraphQLOutputTypeType
firstOnlybooleanIf true, return only first match
pageDependenciesObjectOptional page dependency information.
pathstringThe path of the page that depends on the retrieved nodes’ data
connectionTypestringMark this dependency as a connection
Return value
Promise<Node[]>
Given a result, that’s either a single node or an array of them, track them using pageDependencies. Defaults to tracking according to current resolver path. Returns the result back.
Parameters
resultNode | Node[]pageDependenciesObjectOptional page dependency information.
pathstringThe path of the page that depends on the retrieved nodes’ data
connectionTypestringMark this dependency as a connection