Movatterモバイル変換


[0]ホーム

URL:


Migrate to Netlify Today

Netlify announces the next evolution of Gatsby Cloud.Learn more

SupportLog In

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

Example Usage

Start building today on Netlify!

Methods

Get all nodes in the store, or all nodes of a specified type (optionally with limit/skip).Returns slice of result as iterable and total count of nodes.

You can directly return itsentries result in your resolver.

Parameters

  • args
    • queryObject

      Query arguments (e.g.limit andskip)

    • typestring |GraphQLOutputType

      Type

  • pageDependenciesObject

    Optional page dependency information.

    • pathstring

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

    • connectionTypestring

      Mark this dependency as a connection

Return value

Promise<Object>

Object containing{ entries: GatsbyIterable, totalCount: () => Promise<number> }

Example

// Get all nodes of a typeconst{ entries, totalCount}=awaitfindAll({type:`MyType`})// Get all nodes of a type while filtering and sortingconst{ entries, totalCount}=awaitfindAll({type:`MyType`,query:{sort:{date:`desc`},filter:{published:{eq:false}},},})// The `entries` return value is a `GatsbyIterable` (check its TypeScript definition for more details) and allows you to execute array like methods like filter, map, slice, forEach. Calling these methods is more performant than first turning the iterable into an array and then calling the array methods.const{ entries, totalCount}=awaitfindAll({type:`MyType`})const count=awaittotalCount()const filteredEntries= entries.filter(entry=> entry.published)// However, if a method is not available on the `GatsbyIterable` you can turn it into an array first.const filteredEntries= entries.filter(entry=> entry.published)return Array.from(posts).length

Get one node in the store. Only returns the first result. When possible, always use this method instead of fetching all nodes and then filtering them.findOne is more performant in that regard.

Parameters

  • args
    • queryObject

      Query arguments (e.g.filter). Doesn’t supportsort,limit,skip.

    • typestring |GraphQLOutputType

      Type

  • pageDependenciesObject

    Optional page dependency information.

    • pathstring

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

    • connectionTypestring

      Mark this dependency as a connection

Return value

Promise<Node>

Example

// Get one node of type `MyType` by its titleconst node=awaitfindOne({type:`MyType`,query:{filter:{title:{eq:`My Title`}}},})

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

Parameters

  • objObject |Array

    Object/Array belonging to Node object or Node object

  • predicatenodePredicate

    Optional callback to check if ancestor meets defined conditions

Return value

Node

Top most ancestor if predicate is not specifiedor first node that meet predicate conditions if predicate is specified


Utility to get a field value from a node, even when that value needs to be materialized first (e.g. nested field that was connected via @link directive)

Parameters

  • nodeNode
  • fieldPathstring

Return value

any

Example

// Example: Via schema customization the author ID is linked to the Author typeconst blogPostNode={author:'author-id-1',// Rest of node fields...}getFieldValue(blogPostNode,'author.name')

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

Parameters

  • argsObject
    • idstring

      ID of the requested node

    • typestring |GraphQLOutputType

      Optional type of the node

  • pageDependenciesObject

    Optional page dependency information.

    • pathstring

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

    • connectionTypestring

      Mark this dependency as a connection

Return value

Node |null

Example

// Using only the idgetNodeById({id:`123`})// Using id and typegetNodeById({id:`123`,type:`MyType`})// Providing page dependenciesgetNodeById({id:`123`},{path:`/`})

Get nodes from the store by IDs and optional type.

Parameters

  • argsObject
    • idsstring[]

      IDs of the requested nodes

    • typestring |GraphQLOutputType

      Optional type of the nodes

  • pageDependenciesObject

    Optional page dependency information.

    • pathstring

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

    • connectionTypestring

      Mark this dependency as a connection

Return value

Node[]

Example

// Using only the idgetNodesByIds({ids:[`123`,`456`]})// Using id and typegetNodesByIds({ids:[`123`,`456`],type:`MyType`})// Providing page dependenciesgetNodesByIds({ids:[`123`,`456`]},{path:`/`})

Get the names of all node types in the store.

Return value

string[]

Replace the cache either with the value passed on (mainly for tests) oran empty new Map.

Parameters

  • mapundefined |null |FiltersCache

    This cache caches a set of buckets (Sets) of Nodes based on filter and tracks this for each set of types which areactually queried. If the filter targetsid directly, only one Node iscached instead of a Set of Nodes. If null, don’t create or use a cache.


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

Parameters

  • nodeNode

    Root Node


Given a result, that’s either a single node or an array of them, track themusing pageDependencies. Defaults to tracking according to current resolverpath. Returns the result back.

Parameters

  • resultNode |Node[]
  • pageDependenciesObject

    Optional page dependency information.

    • pathstring

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

    • connectionTypestring

      Mark this dependency as a connection

Return value

Node |Node[]

Gatsby is powered by the amazing Gatsby
community and Gatsby, the company.


[8]ページ先頭

©2009-2025 Movatter.jp