Replies: 4 comments 7 replies
-
Hi @derekyau! The process you outline in your last code snippet is generally how I go about doing it. It's a bit of tedious manual work, but it makes it obvious what's happening if you are working with references. I know it's just an example in your question, but I'm wondering if your tasks are strongly tied to a project if you need to model the tasks as references? Maybe it would be easier to just have the const ProjectModel = types.model('ProjectModel', {
id: types.identifier,
name: types.maybeNull(types.string),
tasks: types.array(TaskModel)
}); |
Beta Was this translation helpful? Give feedback.
-
One thing I'd like to recommend is https://github.com/infinitered/mst-reference-pool Note that it does not support import { resolveIdentifier } from "mobx-state-tree"
...
const task = resolveIdentifier(Task, store, taskId)
// could also be a more specific MST node than just the generic `store`
const task = resolveIdentifier(Task, project.tasks, taskId) Between these tools, you might get closer to what you're looking to do. Beyond that, I think |
Beta Was this translation helpful? Give feedback.
-
Here's roughly how you'd setup mst-query for this: const RootStore = createRootStore({
projectStore: types.optional(createModelStore({ projects: types.map(ProjectModel) }), {}),
taskStore: types.optional(createModelStore({ tasks: types.map(TaskModel) }), {}),
});
// in the next version of mst-query this will be less verbose
const ProjectQuery = createQuery('ProjectQuery', {
data: MstQueryRef(ProjectModel),
request: types.model({ projectId: types.string })
}).actions((self) => ({
run: flow(function* () {
const next = yield* self.query(getProjectFn);
const { result, error, data } = next();
}),
}));
// And then in your component:
const { data, error, isLoading } = useQuery(ProjectQuery, {
request: { projectId: id }
});
// Models can then be accessed from the store as normalized entities
const { projectStore } = useRootStore();
const project = projectStore.projects.get(projectId); mst-query will automatically infer any level of nested identifier models and normalize these into the root store. If a model already exists it only updates the data actually fetched from the server, so partial updates work great. |
Beta Was this translation helpful? Give feedback.
-
There is a very useful normalize tool you can try using it with mst |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
When fetching data from an external REST API, its fairly typical to have nested models in the response. To set up the context for my question I'll define a few things here
GET /projects/:id
might return something likeIf we have a MST models defined as such:
ProjectModel
TaskModel
In the above example, fetching a
project
from the API also fetches nestedtask
objects. Say I have two additionalstores
that each contain a MST map (where we store the data from the API), something like:Later, when we use a
flow
function to fetch the data from the API, we need to remember to do something like:We can make the above code a bit better by using
preProcessor
and other mixins that take care of merging / updating data, but in general this becomes pretty tedious especially when there are multiple nested objects within one API call. Keeping the data up to date in these cases gets pretty gnarly as we have tomergeUpdate
many models and their related models.This works, but seems highly inefficient (and causes significant lag) as various models are transformed / updated / and put into their various
maps
. So ultimately my question is... is there a better way / generally accepted pattern to do this?I've been seeing the popular
useQuery
hook that seems to solve this problem under the hood. I'm also aware there is amst-query
library that seems? like might help with something like this?Outside of switching our codebase to use libraries like that, I'm curious - how are people handling this right now? Or is the way we are fetching / updating data in our stores flawed?
Beta Was this translation helpful? Give feedback.
All reactions