Skip to content
This repository has been archived by the owner on Sep 27, 2023. It is now read-only.

[experimental] Cannot infer fragment type from query #157

Open
janicduplessis opened this issue Nov 5, 2019 · 3 comments
Open

[experimental] Cannot infer fragment type from query #157

janicduplessis opened this issue Nov 5, 2019 · 3 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@janicduplessis
Copy link

Not sure if this is a limitation of typescript but it seems like it cannot infer fragment types from a query response.

const data = useLazyLoadQuery<SubmitTagsScreenQuery>(graphql`
    query SubmitTagsScreenQuery {
      viewer {
        ...SubmitTagsScreen_viewer
      }
    }
  `);
  const viewer = useFragment(
    graphql`
      fragment SubmitTagsScreen_viewer on Viewer {
        ...
      }
    `,
    data.viewer,
  );
(property) viewer: {
    readonly " $fragmentRefs": FragmentRefs<"SubmitTagsScreen_viewer">;
}
No overload matches this call.
  The last overload gave the following error.
    Argument of type '{ readonly " $fragmentRefs": FragmentRefs<"SubmitTagsScreen_viewer">; }' is not assignable to parameter of type 'readonly { readonly ' $data'?: unknown; }[]'.
      Type '{ readonly " $fragmentRefs": FragmentRefs<"SubmitTagsScreen_viewer">; }' is missing the following properties from type 'readonly { readonly ' $data'?: unknown; }[]': length, concat, join, slice, and 18 more.ts(2769)
useFragment.d.ts(36, 17): The last overload is declared here.

Adding an annotation to useFragment does fix it

const viewer = useFragment<SubmitTagsScreen_viewer$key>(
...
@sibelius sibelius added bug Something isn't working help wanted Extra attention is needed labels Nov 5, 2019
@renanmav
Copy link
Contributor

renanmav commented Nov 5, 2019

I think this issue is aimed to @types/react-relay on DefinitelyTyped

@janicduplessis
Copy link
Author

The problem is that the generated type for queries only contains $fragmentRefs and the one for the generic parameter of useFragment only $data meaning there is no overlap between the types which causes the error.

@alloy
Copy link
Member

alloy commented Dec 19, 2019

Off-hand, I'd say we need to expand the typing emitted from this plugin for queries to include those new keys.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants