Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore TVariables to useQuery return type #8801

Merged

Conversation

keithlayne
Copy link
Contributor

Checklist:

  • If this PR is a new feature, please reference an issue where a consensus about the design was reached (not necessary for small changes)
  • Make sure all of the significant new logic is covered by tests

Problem

useQuery was clearly rewritten completely from 3.4 to 3.5, but its signature changed from

function useQuery<TData = any, TVariables = OperationVariables>(/*...*/): QueryResult<TData, TVariables>;

to

function useQuery<TData = any, TVariables = OperationVariables>(/*...*/): QueryResult<TData>;

This causes the TVariables type arg to be thrown away, and then defaulted to OperationVariables in QueryResult. This causes assignability issues in TS in some cases. The issue can be demonstrated with a trivial generic hook that wraps a query hook:

type DumbQuery = { foo: string };
type DumbVariables = { bar: number };
const DumbDocument = gql`
  query Dumb($bar: Int!) {
    dumb(bar: $bar) {
      foo
    }
  }
`;

function useDumbQuery(baseOptions: QueryHookOptions<DumbQuery, DumbVariables>) {
  const options = { ...baseOptions };
  return useQuery<DumbQuery, DumbVariables>(DumbDocument, options);
}

// Trivially wrapping a query hook
const useDumbWrappedQuery = <Q, V>(
  hook: (options: QueryHookOptions<Q, V>) => QueryResult<Q, V>,
  options: QueryHookOptions<Q, V> = {},
) => hook(options);

const Test = () => {
  useDumbWrappedQuery(useDumbQuery);
  /*
  Argument of type '(baseOptions: QueryHookOptions<DumbQuery, DumbVariables>) => QueryResult<DumbQuery, OperationVariables>' is not assignable to parameter of type '(options: QueryHookOptions<DumbQuery, DumbVariables>) => QueryResult<DumbQuery, DumbVariables>'.
  Call signature return types 'QueryResult<DumbQuery, OperationVariables>' and 'QueryResult<DumbQuery, DumbVariables>' are incompatible.
    The types of 'variables' are incompatible between these types.
      Type 'OperationVariables | undefined' is not assignable to type 'DumbVariables | undefined'.
        Type 'OperationVariables' is not assignable to type 'DumbVariables'.
  */

  return null;
};

Notes

  • This problem doesn't appear when all of the properties of TVariables are optional.
  • The "testing" box isn't checked because I don't know if you have any test infrastructure for types, and this is a type-only problem. I didn't look too hard, so if I missed it, let me know.
  • npm run build and npm run test worked fine with this change.

@brainkim 🚎 👀

@apollo-cla
Copy link

@keithlayne: Thank you for submitting a pull request! Before we can merge it, you'll need to sign the Apollo Contributor License Agreement here: https://contribute.apollographql.com/

@brainkim
Copy link
Contributor

Keith, have I ever told you you are my fourth favorite cat among cats that I know? Thank you!

@brainkim brainkim self-requested a review September 17, 2021 15:02
@brainkim brainkim merged commit 4bb0349 into apollographql:release-3.5 Sep 17, 2021
@keithlayne keithlayne deleted the 3.5-useQuery-return-type branch September 17, 2021 15:31
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants