gatsby#Node TypeScript Examples

The following examples show how to use gatsby#Node. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: schema-customization.ts    From gatsby-project-kb with MIT License 5 votes vote down vote up
setFieldsOnGraphQLNodeType = (
  { cache, type, getNode }: SetFieldsOnGraphQLNodeTypeArgs,
  _options?: PluginOptions
) => {
  const options = resolveOptions(_options)

  // if we shouldn't process this node, then return
  if (!options.types.includes(type.name)) {
    return {}
  }

  return {
    outboundReferences: {
      type: `[NodeReference!]!`,
      resolve: async (node: Node) => {
        let cachedNode = await getCachedNode(cache, node.id, getNode)

        if (!cachedNode || !cachedNode.resolvedOutboundReferences) {
          await generateData(cache, getNode)
          cachedNode = await getCachedNode(cache, node.id, getNode)
        }

        if (cachedNode && cachedNode.resolvedOutboundReferences) {
          return cachedNode.resolvedOutboundReferences
            .map((refNode) => {
              const { target } = refNode
              const targetNode = getNode(target.id)
              if (!targetNode) return null
              return {
                ...refNode,
                target: targetNode,
              } as NodeReference
            })
            .filter(nonNullable)
        }

        return []
      },
    },
    inboundReferences: {
      type: `[NodeReference!]!`,
      resolve: async (node: Node) => {
        let data = await getInboundReferences(cache)

        if (!data) {
          await generateData(cache, getNode)
          data = await getInboundReferences(cache)
        }

        if (data) {
          return (data[node.id] || [])
            .map(({ target, referrer, contextLine }) => {
              const targetNode = getNode(target.id)
              if (!targetNode) return null
              return {
                target: targetNode,
                contextLine,
                referrer,
              } as NodeReference
            })
            .filter(nonNullable)
        }

        return []
      },
    },
  }
}
Example #2
Source File: onCreateNode.ts    From gatsby-plugin-react-i18next with MIT License 5 votes vote down vote up
export function unstable_shouldOnCreateNode({node}: {node: Node}) {
  // We only care about JSON content.
  return node.internal.mediaType === `application/json`;
}