datocms / gatsby-source-datocms

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

plugin breaks if you prefix all instances #205

Closed joernroeder closed 1 year ago

joernroeder commented 1 year ago

To achieve clear separation between multiple connected instances (and being able to query them separately via graphql) in a gatsby project we'd love to prefix all our connected datoCMS instances with instancePrefix but this breaks during sourcing. The reason for that that some models lack the generateType wrapper for their naming.

Clear separation is for us more important as we're looking into other tooling like https://www.gatsbyjs.com/products/valhalla-content-hub/ where we only expose certain instance assets for example but not others.

Some occurrences are:

https://github.com/datocms/gatsby-source-datocms/blob/master/src/hooks/sourceNodes/createTypes/site/DatoCmsSite.js#L55

https://github.com/datocms/gatsby-source-datocms/blob/master/src/hooks/sourceNodes/createTypes/site/DatoCmsSite.js#L79

https://github.com/datocms/gatsby-source-datocms/blob/master/src/hooks/sourceNodes/createTypes/fieldTypes/DatoCmsVideoField.js#L4

just to highlight a view, but there are more.


In addition, the gatsby cloud preview instance breaks with multiple connected dato instances here in the switch https://github.com/datocms/gatsby-source-datocms/blob/master/src/hooks/sourceNodes/index.js#L101

Given the case that instance B is sending an update, instance A receives the update, won't find the item and panic breaks the build with a 404.

my solution is to patch this with a try catch surrounding the switch.

try {
  switch (entityType) {
    // nothing changed inside the switch
    ...
  }
  if (instancePrefix) {
    reporter.info(`Updated ${entityType} ${entityId} in ${instancePrefix}`);
  }
} catch (err) {
  if (instancePrefix) {
    reporter.info(`Could not find ${entityType} ${entityId} in ${instancePrefix}. The item might belong to another DatoCMS instance.`);
  } else {
    reporter.panicBuild(err);
  }
}

Breaking the preview builds with 404s is particular frustrating for editors because they sometimes won't be able to see a preview at all and it seems to force gatsby cloud to do colder builds next time which take much longer.

stefanoverna commented 1 year ago

v5.0.3 should fix this issue, thanks @joernroeder!

joernroeder commented 1 year ago

@stefanoverna thanks for looking into this and yes, on my test instance it fixes the issue! In the graphql explorer I still see one model without prefix which is datoCmsTextNode.