apollographql / apollo-client-nextjs

Apollo Client support for the Next.js App Router
https://www.npmjs.com/package/@apollo/experimental-nextjs-app-support
MIT License
352 stars 25 forks source link

Type checking issues with getClient.mutate() #222

Open jeffstephens opened 1 month ago

jeffstephens commented 1 month ago

Hi there,

We've been using this project and it has been excellent for the most part - greatly appreciated.

There is one issue we've been running into - getClient().mutate() calls don't automatically type-check input variables. getClient() is the RSC implementation from the docs:

export const { getClient } = registerApolloClient(() => {
// ...

With our graphql-codegen-generated types, the return types all Just Work, but variables are only type-checked if we explicitly set both types in the mutate<T, T> generic arguments. Otherwise, it infers the type from the provided variables irrespective of the GraphQL schema, causing run-time errors rather than build-time.

Here's an example. The return type is correctly set.

return getClient().mutate({
  mutation: MESSAGE_MARK_READ,
  variables: {
    input: {
      messageId,
      test: true, // <-- this is not in the mutation input in GQL, but no type error
    },
  },
});

After adding types as mutate<MyMutation, MyMutationVariables({, an error is thrown for test.

However, if I then move input into a new variable and pass it into the mutation, the type checking breaks down again, even with the explicit types specified. I'm not sure if these issues are related, but it seems likely.

Based on the hook-based useMutation, I'd expect all this to work automatically in both of the cases above. Please let me know if I can provide any more information or help.

Thank you for your time!