Give records a reference to the model manager which loaded them #273
+184
−102
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
We have a few upcoming things that require records to be able to do stuff to themselves -- reload, select new fields from the backend, maybe
record.save
or similar, and I suspect more in the future. I still think they should act mostly as DTOs that don't have a lot of business logic on them, but if we want to be able to even know what model they are for, we need a reference to the thing that produced them! This passes along the model manager which instantiated a record to the record in both imperative API land and in React land.In order to make this change work in React land, we need a new little bit of metadata exported from the generated client. Our React hooks get passed functions from the api client like
useAction(api.post.create)
, so we need to be able to hop back from thecreate
function that we get by reference to theapi.post
object. The generated client will need to decorate thecreate
function with a reference, which is not super hard, but means that we have a backwards compatibility issue.@gadgetinc/react
can't assume that it is upgraded at the same time as the api client, so it can't assume it is working against a newly generated api client that has this metadata.For this reason, the model manager property on
GadgetRecord
is optional, which reflects the way it will be used in the real world without necessarily having that metadata available. When we go to build things likerecord.reload()
, we can make that fail at runtime with a message saying "regenerate your client to get this to work!", instead of just assuming it is present.