Skip to main content

Node Model

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

Example usage

gatsby-node.js

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

Methods

Reference

Finds top most ancestor of node that contains passed Object or Array

Parameters

  • obj Object | Array

    Object/Array belonging to Node object or Node object

  • predicate nodePredicate

    Optional 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 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[]

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

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[]

Get the names of all node types in the store.

Return value

string[]

Get nodes of a type matching the specified query.

Parameters

  • args Object
    • query Object

      Query arguments (filter and sort)

    • 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[]>

Adds link between inline objects/arrays contained in Node object and that Node object.

Parameters

  • node Node

    Root 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 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
Tutorials
Plugins
Blog
Showcase