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
obj
Object | ArrayAn object belonging to a Node, or a Node object
predicate
FunctionOptional 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
args
Objecttype
string | GraphQLOutputTypeOptional type of the nodes
pageDependencies
ObjectOptional page dependency information.
path
stringThe path of the page that depends on the retrieved nodes’ data
connectionType
stringMark this dependency as a connection
Return value
Node[]
Get a node from the store by ID and optional type.
Parameters
args
Objectid
stringID of the requested node
type
string | GraphQLOutputTypeOptional type of the node
pageDependencies
ObjectOptional page dependency information.
path
stringThe path of the page that depends on the retrieved nodes’ data
connectionType
stringMark this dependency as a connection
Return value
Node | null
Get nodes from the store by IDs and optional type.
Parameters
args
Objectids
string[]IDs of the requested nodes
type
string | GraphQLOutputTypeOptional type of the nodes
pageDependencies
ObjectOptional page dependency information.
path
stringThe path of the page that depends on the retrieved nodes’ data
connectionType
stringMark this dependency as a connection
Return value
Node[]
Get nodes of a type matching the specified query.
Parameters
args
Objectquery
ObjectQuery arguments (
filter
,sort
,limit
,skip
)type
string | GraphQLOutputTypeType
firstOnly
booleanIf true, return only first match
pageDependencies
ObjectOptional page dependency information.
path
stringThe path of the page that depends on the retrieved nodes’ data
connectionType
stringMark 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
result
Node | Node[]pageDependencies
ObjectOptional page dependency information.
path
stringThe path of the page that depends on the retrieved nodes’ data
connectionType
stringMark this dependency as a connection