Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
176 views
in Technique[技术] by (71.8m points)

Updating Data in Gatsby Source Plugin

I have created a simple gatsby source plugin that pulls in api data. So far, so good -- it pulls in the data exactly as it should.

However, there is one problem. The api data gets updated on a regular basis. That is to say, new articles get added to the api. I am wondering if it is possible to update the graphql data in Gatsby with the new articles without losing the data that was already fetched.

If so, how?

Thanks.

P.S. In case it is relevant, here is the code of the plugin:

const fetch = require("node-fetch")

exports.sourceNodes = (
  { actions, createNodeId, createContentDigest },
  config
) => {
  const { createNode } = actions

  delete config.plugins

  const processFeed = item => {
    const nodeId = createNodeId(`[NODE_NAME]-${item.id}`)
    const nodeContent = JSON.stringify(item)
    const nodeData = Object.assign({}, item, {
      id: nodeId,
      parent: null,
      children: [],
      internal: {
        type: `[TYPE_NAME]`,
        content: nodeContent,
        contentDigest: createContentDigest(item),
      },
    })
    return nodeData
  }

  ...

  return fetch([FETCH_URL], [FETCH_HEADERS])
    .then(response => response.json())
    .then(data => {
      data.items.forEach(item => {
        const nodeData = processFeed(item)
        createNode(nodeData)
      })
    })
}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Since the GraphQL server in Gatsby only runs during development (or while the build is being created), there's no way for a source plugin to push data on a regular basis out of the box. What you could do instead is trigger a new build on a recurring schedule (every N minutes/hours/etc) to fetch updated data.

If your builds take a while to complete and you want to speed up this process, you could also consider using Gatsby Cloud which has support for doing incremental builds (note: Gatsby Cloud is a paid SaaS product from the Gatsby core team).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...