relay-tools / relay-hooks

Use Relay as React hooks
https://relay-tools.github.io/relay-hooks/docs/relay-hooks.html
MIT License
542 stars 56 forks source link

Typescript for useQuery if props returns {} or undefined #69

Closed melounek closed 4 years ago

melounek commented 4 years ago

There are some TS clashes, when useQuery returns empty results.

props will return

I feel like these two options should be unified into undefined/null (null when is loading and undefined for skip or not found in store)

And in any case, the empty result should be handled by Typescript. So instead of

export interface RenderProps<T extends OperationType> {
    props: T['response'];

there should be

export interface RenderProps<T extends OperationType> {
    props: T['response'] | null | undefined;
morrys commented 4 years ago

hi @melounek, it seems to me correct to foresee the same result for skip and for the fetchPolicy "store-only". the fix is ​​quite simple https://github.com/relay-tools/relay-hooks/blob/master/src/QueryFetcher.ts#L108

As for typing, I wonder if this change is also necessary for official useLazyLoadQuery: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react-relay/lib/relay-experimental/useLazyLoadQuery.d.ts#L13

@renanmav, what do you think about it?

morrys commented 4 years ago

typing the QueryRenderer https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react-relay/index.d.ts#L117 also provides null.

so, the behavior that @melounek suggests:

null when is loading and undefined for skip or not found in store

I find it correct :)

renanmav commented 4 years ago

Hmm, I don’t think this should be done by us.

If your query returns empty results, relay-compiler-language-typescript will detect that (based on your schema) and this would be reflected by T['response'].

I don’t know If I was clear...

melounek commented 4 years ago

@renanmav that's something else. If the schema returns null the props will contain something like

{ queryName: null }

and this null is provided by the T['response'], but during the loading time when we are waiting for the result, the props contains just null and this should be explicitly defined in the QueryFetcher.ts

renanmav commented 4 years ago

Got it

morrys commented 4 years ago

I have just released version 3.3.0 https://github.com/relay-tools/relay-hooks/releases/tag/v3.3.0

Feel free to open new issues if you find problems :)