KEMBAR78
fix(gatsby-source-contentful): fix downloadLocal for LMDB by pieh · Pull Request #33715 · gatsbyjs/gatsby · GitHub
Skip to content

Conversation

@pieh
Copy link
Contributor

@pieh pieh commented Oct 28, 2021

Description

In v4 (or with LMDB rather as this does apply to v3 with LMDB/PQR experiments enabled) mutating nodes after they are created is no longer working (even before while it was "working" it often caused a lot of problems and was antipattern that could cause difficult to debug problems).

Right now when downloadLocal option is used in Contentful plugin, it actually does try to mutate node after it was created resulting in localFile just not being there anymore (even tho files are still downloaded).

This PR instead of mutating node directly, use createNodeField to add data in Gatsby friendly way to a node (that works with LMDB). And then use schema customization so the localFile remains on root level of the "node" in GraphQL (so no breaking changes)

We do have note about this problem in migration https://v4.gatsbyjs.com/docs/reference/release-notes/migrating-from-v3-to-v4/#dont-mutate-nodes-outside-of-expected-apis but

Unfortunately it is hard to detect it automatically (without sacrificing performance), so we recommend you to check your code to ensure you don’t mutate nodes directly.

Makes it really hard for users or plugin authors to actually catch that. This will likely have follow ups to actually do add ways of automatic detection (maybe in diagnostic mode, or maybe some heuristics that checks just subset of nodes or combination of both). https://github.com/gatsbyjs/gatsby/compare/node-mutations-are-a-no-no has potential detection, but not benchmarked yet (but at least it allowed me to catch the problem here)

@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Oct 28, 2021
@wardpeet wardpeet requested a review from axe312ger October 28, 2021 13:08
@pieh
Copy link
Contributor Author

pieh commented Oct 28, 2021

There are failing unit tests here, that I can fix, but this PR is mostly "concept" - I don't know if it's best way to implement downloading assets before creating nodes, I just did "massage" code to fit it

@pieh pieh added topic: source-contentful Related to Gatsby's integration with Contentful and removed status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer labels Oct 28, 2021
@pieh
Copy link
Contributor Author

pieh commented Oct 28, 2021

Other alternative than this approach is to keep most of it as is and just not mutate node in

and instead use createNodeField + schema customization so that localFile seems to be working the same to users - will explore that in a bit

@axe312ger
Copy link
Contributor

I'll have a look tomorrow

@pieh
Copy link
Contributor Author

pieh commented Oct 28, 2021

Not sure if this will be helpful, but the change in this PR now is very similar to updated docs (not yet merged in #33720 ) that is meant for easier migration than what I initially had (which was also "correct" in sense that would work, just shuffled so much code around that it was making review much more difficult)

@axe312ger axe312ger force-pushed the fix/contentful-download-local branch from a348995 to 9e56216 Compare October 29, 2021 11:23
@pieh pieh merged commit a8745ea into master Oct 29, 2021
@pieh pieh deleted the fix/contentful-download-local branch October 29, 2021 15:46
axe312ger added a commit that referenced this pull request Nov 9, 2021
* fix(gatsby-source-contentful): fix downloadLocal for LMDB

* Revert "fix(gatsby-source-contentful): fix downloadLocal for LMDB"

This reverts commit 5bacca7.

* use createNodeField + schemaCustomization

* tests: add downloadLocal to e2e tests

* fix unit tests after removal of pre bootstrap

Co-authored-by: axe312ger <opensource@axe312.dev>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic: source-contentful Related to Gatsby's integration with Contentful

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants