datocms / gatsby-source-datocms

Official GatsbyJS source plugin to pull content from DatoCMS
MIT License
140 stars 51 forks source link

allDatoCms[Page] breaks gatsby develop when saving changes in Dato #12

Closed samirfors closed 6 years ago

samirfors commented 6 years ago

I have this in my gatsby-node.js to generate pages from my Page model in Dato, and it works great.

exports.createPages = ({ graphql, boundActionCreators }) => {
  const { createLayout, createPage, createRedirect } = boundActionCreators;

  return new Promise( (resolve, reject) => {
    graphql(
      `
        {
          allDatoCmsPage {
            edges {
              node {
                id
                slug
              }
            }
          }
        }
      `
    )
    .then((result) => {
      if (result.errors) {
        reject(result.errors);
      }

      const pageTemplate = path.resolve(`./src/templates/pageTemplate.jsx`);

      console.log('Log data', result.data);

      _.each(result.data.allDatoCmsPage.edges, ({ node }) => {
        createPage({
          path: node.slug === 'index' ? '/' : `/${node.slug}/`,
          component: slash(pageTemplate),
          context: {
            id: node.id,
          },
        })
      });

      resolve();
    })
  })
}

However, whenever I save any change in Dato (in any model), gatsby develop fails since allDatoCmsPage is null:

info Detected DatoCMS data change!
Log data { allDatoCmsPage: null }
error Cannot read property 'edges' of null

  TypeError: Cannot read property 'edges' of null

  - gatsby-node.js:30 graphql.then
    /gatsby-node.js:30:41

  - util.js:16 tryCatcher
    /[bluebird]/js/release/util.js:16:23

  - promise.js:512 Promise._settlePromiseFromHandler
    /[bluebird]/js/release/promise.js:512:31

  - promise.js:569 Promise._settlePromise
    /[bluebird]/js/release/promise.js:569:18

  - promise.js:614 Promise._settlePromise0
    /[bluebird]/js/release/promise.js:614:10

  - promise.js:693 Promise._settlePromises
    /[bluebird]/js/release/promise.js:693:18

  - promise.js:638 Promise._fulfill
    /[bluebird]/js/release/promise.js:638:18

  - promise.js:432 Promise._resolveCallback
    /[bluebird]/js/release/promise.js:432:57

  - promise.js:524 Promise._settlePromiseFromHandler
    /[bluebird]/js/release/promise.js:524:17

  - promise.js:569 Promise._settlePromise
    /[bluebird]/js/release/promise.js:569:18

  - promise.js:614 Promise._settlePromise0
    /[bluebird]/js/release/promise.js:614:10

  - promise.js:693 Promise._settlePromises
    /[bluebird]/js/release/promise.js:693:18

  - promise.js:638 Promise._fulfill
    /[bluebird]/js/release/promise.js:638:18

  - promise.js:582 Promise._settlePromise
    /[bluebird]/js/release/promise.js:582:21

  - promise.js:614 Promise._settlePromise0
    /[bluebird]/js/release/promise.js:614:10

  - promise.js:693 Promise._settlePromises
    /[bluebird]/js/release/promise.js:693:18

error UNHANDLED REJECTION

package.json

{
  "dependencies": {
    "gatsby": "^1.9.232",
    "gatsby-source-datocms": "^1.0.26",
  }
}

Am I missing somethings obvious here?

drobinhood commented 6 years ago

Not sure if it's helpful to you, but I ran into a similar issue, turned out I had a Links field that had no entries, when I removed it the issue went away.

marbiano commented 6 years ago

Having the same issue right now. Data coming from DatoCMS is always null whenever I update something on it.

stefanoverna commented 6 years ago

Should be fixed in v1.1.1

samirfors commented 6 years ago

Updated the plugin and it works great. Thank you!