Node Model
Gatsby exposes its internal data store and query capabilities to GraphQL field resolvers on context.nodeModel
.
Example Usage
Methods
Get all nodes in the store, or all nodes of a specified type (optionally with limit/skip). Returns slice of result as iterable.
Parameters
args
query
ObjectQuery arguments (e.g.
limit
andskip
)type
string | GraphQLOutputTypeType
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
Object
Object containing { entries: GatsbyIterable, totalCount: () => Promise<number> }
Get one node in the store. Only returns the first result.
Parameters
args
query
ObjectQuery arguments (e.g.
filter
). Doesn’t supportsort
,limit
,skip
.type
string | GraphQLOutputTypeType
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>
Finds top most ancestor of node that contains passed Object or Array
Parameters
obj
Object | ArrayObject/Array belonging to Node object or Node object
predicate
nodePredicateOptional callback to check if ancestor meets defined conditions
Return value
Node
Top most ancestor if predicate is not specified or first node that meet predicate conditions if predicate is specified
Get all nodes in the store, or all nodes of a specified type. Note that this adds connectionType tracking by default if type is passed.
Since version 4.0 - Use nodeModel.findAll() instead
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[]
Replace the cache either with the value passed on (mainly for tests) or an empty new Map.
Parameters
map
undefined | null | FiltersCache(This cached is used in redux/nodes.js and caches a set of buckets (Sets) of Nodes based on filter and tracks this for each set of types which are actually queried. If the filter targets
id
directly, only one Node is cached instead of a Set of Nodes. If null, don’t create or use a cache.
Get nodes of a type matching the specified query.
Since version 4.0 - Use nodeModel.findAll() or nodeModel.findOne() instead
Parameters
args
Objectquery
ObjectQuery arguments (
filter
andsort
)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[]>
Adds link between inline objects/arrays contained in Node object and that Node object.
Parameters
node
NodeRoot 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