Skip to main content

Node Model

Gatsby exposes its internal data store and query capabilities to GraphQL field resolvers on context.nodeModel.

Example usage

createResolvers({
  Query: {
    mood: {
      type: `String`,
      resolve(source, args, context, info) {
        const coffee = context.nodeModel.getAllNodes({ type: `Coffee` })
        if (!coffee.length) {
          return 😞
        }
        return 😊
      },
    },
  },
})

Methods


Reference

findRootNodeAncestor

Get the root ancestor node for an object’s parent node, or its first ancestor matching a specified condition.

Parameters

  • obj Object | Array

    An object belonging to a Node, or a Node object

  • predicate Function

    Optional condition to match

Return value

Node | null

getAllNodes

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 Object
    • type string | GraphQLOutputType

      Optional type of the nodes

  • pageDependencies Object

    Optional page dependency information.

    • path string

      The path of the page that depends on the retrieved nodes’ data

    • connectionType string

      Mark this dependency as a connection

Return value

Node[]

getNodeById

Get a node from the store by ID and optional type.

Parameters

  • args Object
    • id string

      ID of the requested node

    • type string | GraphQLOutputType

      Optional type of the node

  • pageDependencies Object

    Optional page dependency information.

    • path string

      The path of the page that depends on the retrieved nodes’ data

    • connectionType string

      Mark this dependency as a connection

Return value

Node | null

getNodesByIds

Get nodes from the store by IDs and optional type.

Parameters

  • args Object
    • ids string[]

      IDs of the requested nodes

    • type string | GraphQLOutputType

      Optional type of the nodes

  • pageDependencies Object

    Optional page dependency information.

    • path string

      The path of the page that depends on the retrieved nodes’ data

    • connectionType string

      Mark this dependency as a connection

Return value

Node[]

getTypes

Get the names of all node types in the store.

Return value

string[]

runQuery

Get nodes of a type matching the specified query.

Parameters

  • args Object
    • query Object

      Query arguments (filter, sort, limit, skip)

    • type string | GraphQLOutputType

      Type

    • firstOnly boolean

      If true, return only first match

  • pageDependencies Object

    Optional page dependency information.

    • path string

      The path of the page that depends on the retrieved nodes’ data

    • connectionType string

      Mark this dependency as a connection

Return value

Promise<Node[]>

trackPageDependencies

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 Object

    Optional page dependency information.

    • path string

      The path of the page that depends on the retrieved nodes’ data

    • connectionType string

      Mark this dependency as a connection

Return value

Node | Node[]
Docs
Tutorial
Plugins
Blog
Showcase