mst-gql icon indicating copy to clipboard operation
mst-gql copied to clipboard

How to use a mst-gql Query in a generator action

Open special-character opened this issue 4 years ago • 3 comments

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?

special-character avatar May 07 '20 22:05 special-character

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 avatar May 18 '20 08:05 tadeaspetak

@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

special-character avatar May 19 '20 20:05 special-character

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 })

sashaozz avatar Nov 08 '20 20:11 sashaozz