ConrabOpto / mst-query

Query library for mobx-state-tree
MIT License
110 stars 7 forks source link

v2 #33

Closed k-ode closed 1 year ago

k-ode commented 2 years ago

New helper functions that gives a you default run action: createQueryWithRun, createMutationWithRun.

export const ChatQuery = createQueryWithRun('ChatQuery', {
    request: types.model({ id: types.string }),
    pagination: types.model({ offset: types.number }),
    data: MstQueryRef(ChatModel),
    queryFn: api.getChat,
});

Infinite queries are easier with onQueryMore. After pagination changes, queryMore will be called with the new arguments. onQueryMore is used to determine how the new data should be appended.

const { data } = useQuery(ListQuery, {
  request: { id: 'test' },
  pagination: { offset: offsetProp },
  onQueryMore(data, self) {
     self.data?.items.push(...data.items);
  },
});