mst-gql
mst-gql copied to clipboard
How to use a mst-gql Query in a generator action
I am working on using one of my RootStore.base.ts
queries inside an action and I want to make it a generator action. I am getting a type error because the returned Query
from the RootStore.base.ts
function is not compatible with a Promise
(type wise).
ex:
Argument of type '() => Generator<Query<{ me: UserModelType; }>, void, any>' is not assignable to parameter of type '() => Generator<Promise<any>, void, any>'.
The offending code in RootStore.ts
fetchMe: flow(function* fetchMe() {
const me = yield self.queryMe(undefined, FULL_USER_FRAGMENT)
}),
Has anyone tried to do this yet?
Having the exact same problem since updating some dependencies last week. A simple @ts-ignore
solves it for now, but that's far from ideal, of course.
I suspect it might have something to do with typescript being a little bit stricter or something along those lines?
@tadeaspetak I think you are right. Although they have the same api TS doesn't seem to like it b/c of the it being a Query
instead of a Promise
. Not sure of the best way to fix this yet
In mst-gql Query
implements PromiseLike<T>
, not Promise<T>
hence it is not directly compatible with these flow definitions and TypeScript tells you exactly that.
But Query
type has properties promise
and currentPromise()
which will return promise for ongoing request which are perfectly awaitable as those returns Promise<T>
So just add .promise
to your initial variant and TypeScript will not complain any more, e.g.
fetchMe: flow(function* fetchMe() { const me = yield self.queryMe(undefined, FULL_USER_FRAGMENT).promise })