No ability to pass types into refetchQueries

Hey team.

As I can see, QueryOptions type is used in InternalRefetchQueryDescriptor that we pass to refetchQueries but we’re not able to pass any generics to InternalRefetchQueryDescriptor and functions that operate this type, e.g. useMutation.

What if we add a new generic TRefetchQueryOptions to useMutation, that will be passed down to refetchQueries’s InternalRefetchQueryDescriptor so that we have a good ts support for refetchQueries?

Please see the example below :point_down:

type TRefetchQueryOptions = QueryOptions<Vars1, Data1> | QueryOptions<Vars2, Data2>

const [callMutation] = useMutation<
  any,
  OperationVariables,
  DefaultContext,
  ApolloCache<any>,
  TRefetchQueryOptions
>(mutation, {
  refetchQueries: [
    {
      query: query1,
      variables: vars1 // OK
    },
    {
      query: query2,
      variables: vars2 // OK
    },
    {
      query: query1,
      variables: variables2 // Error - query1 should be used with vars1 variables
    }
  ],
})

3rd party packages such as graphql codegen can then use this advantage to generate TRefetchQueryOptions and pass it into all useMutation calls so that all the consumers will have type checking for query/variables in refetchQueries

Please let me know what do you think. Cheers!

1 Like

Bumping on this. What contributors think about it?

1 Like

This seems like a good idea to me @igrlk. I think we’d also want to take a fresh look at the type annotations for the core ApolloClient#refetchQueries method. Does that make sense?

That sounds good to me, @JeffAuriemma. Do you have a sense of how this whole process will look like, and will the open source contributors (like me :slight_smile:) be able to participate in the changes?

@igrlk we welcome contributions! The typical workflow is this:

  1. Open an issue describing the changes to be made. The more detail, the better. Ideally the maintainers will give feedback but we do have a lot of open issues and PRs to review so I can’t promise a specific timeframe for this
  2. If you’re interested in implementing the changes, fork the repo and open a PR. You don’t have to wait for feedback in order to submit a PR, please feel free to move forward with what you believe are the right implementation choices
  3. We’ll review the PR. If it’s approved, we’ll merge it either into main or a release branch. I would expect a change like this to be included in a minor release but we can’t say for sure without having a diff to review

See CONTRIBUTING.md for more details :slight_smile:

Thanks for engaging with us and have a good rest of your week!

Great, thanks a lot @JeffAuriemma! One more thing to clarify - can you please elaborate more on this:

I think we’d also want to take a fresh look at the type annotations for the core ApolloClient#refetchQueries method

Are there any particular ideas that Apollo team has, that can be possibly implemented?

@igrlk thanks for checking in - I don’t think we have any specific requirements here, I just know that the types are similar and reused in a few place so if we’re changing the hook we will probably also need to make some changes elsewhere. Here are a couple of snippets to note: