The List Widget: Variable Types beta feature is great but creates a bit of an issue in introducing dynamically selected content in Gatsby's static build environment. This is a problem when you're writing GraphQL queries for your templates, as your query needs to request any and all widgets used in the list, but there's no guarantee that the user will have selected them in the CMS.
One way this might be solved is by introducing an option to generate default data for any widget type that remains unused in the list. Then there will at least be something returned for each widget type in the corresponding query.
Yeah, that's a tough one, GraphQL is pretty strict that way. I feel like a a solution should be provided in Gatsby rather than here - are there other, similar issues on their repo about strict GraphQL requirements?
this is an issue for Gatsby...
@stbly do you have any ideas how to handle this?
I have found that as long as each widget type exists somewhere in your templates, the build will succeed. Of course, you can't know that the user will be using all of the selectable widgets at one time, so I've had to find a workaround.
To make sure each widget is represented at build time, I've piped into Gatsby's onPreBootstrap lifecycle hook to create a markdown file from a .yaml file (with fields for all of the selectable widgets populated with placeholder data) and place it in the proper page directory. When Gatsby runs createPages, it will find the placeholder markdown file we've generated with all of the widgets represented, guaranteeing the query will succeed. Then, after the build, I don't want that markdown file to exist anymore, so I use the onPostBootstrap hook to remove the generated markdown file so it doesn't show up in Netlify CMS.
This has worked for me so far, but there might be a better way of doing it.
@stbly good solution, can you share detailed example (code) ?
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
With this feature: https://www.gatsbyjs.org/docs/schema-customization/ this is no longer an issue. Just hard work ;)