Client usage #516
Replies: 5 comments 1 reply
-
Optimistic updateconst client = createClient({
QueryBuilder: SomeQueryBuilder,
network: new Network.Promise(),
store: new Collection(),
options: {
optimistic: true,
},
}) Client should not be responsible for optimistic update behaviour. Optimistic update is a View layer and should be handled in the user land. Optimistic update is more complex than just some flag. client.from(Todo).id(1).optimisticUpdate({ open: false }); // explicit
client.from(Todo).id(1).pesimisticUpdate({ open: false }); // explicit We need some API for rolling back in case of error. const { data, mutate } = useQuery(queryTodo);
const [create, { status }] = useMutation(createTodo, {
onMutate: ({ input }) => {
// get current data from the cache
// do optimistic update and trigger re-render
// rollback
return () => {
// return to previous state
};
},
onFailure: ({ rollback }) => {
rollback();
}
}); Chaining:client.from(User).id(1).chain(
(user) => client.from(Todo).match({ owner: user.id }),
).fetch(); Chain functions should be client.from(User).id(1).chain(
(client, user) => client.from(Todo).match({ owner: user.id }),
); so we can decouple it like this const queryTodoByUser = (client, { id }) => client.from(Todo).match({ owner: id });
const queryUser = (client, { id }) => client.from(User).id(id).chain(queryTodoByUser);
const data = queryUser(client, { id: 1 }).fetch(); Only problem with this example is that we don't have a way to avoid waterfalls. const userFetcher = queryUser(client, { id: 1 }).fetch();
const todoFetcher = queryTodoByUser(client, { id: 1 }).fetch();
const [user, todo] => await Promise.all([userFetcher, todoFetcher]); JSON:API relationshipFor JSON:API (or any RESTful api) we can extend QueryBuilder with client.from(User).id(1).relationship('todos').id(1).fetch(); This way you can fetch todos of specific user. |
Beta Was this translation helpful? Give feedback.
-
Optimistic updateWhy would it need to be in user land? If you call an update function with new data, the lib knows when an API call fails (how else would it call onFailure?) and it already has the ability to roll back changes (using patches). I'm not sure what exactly is the issue? If you really need something custom, you could always use pessimistic update and implement your own logic. ChainingI agree on the client parameter. I was thinking about it, but didn't include it in the initial example... client.from(User).chain(
(client, users) => client.from(Todo).match({ owner: users.map(({ id }) => id).join(',') }), // Get todos owned by any of the fetched users
).fetchList(); // Get the paginated users list JSON:API relationshipYes, I guess so, but this would depend on the specific QueryBuilder implementation |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
We also need to think about SSR mode: const client = createClient({
ssr: typeof window === 'undefined',
}) This way we can bypass storing data in the collection and just return raw responses from the API, and we don't have to create an entirely new instance of Datx Client for each request. |
Beta Was this translation helpful? Give feedback.
-
Makes sense, but I wouldn't call this Edit - actually, why would you need that? The point is to create a new client with a new collection for each request. |
Beta Was this translation helpful? Give feedback.
-
Initialization:
Usage:
Chaining:
Beta Was this translation helpful? Give feedback.
All reactions