apollographql / apollographql/apollo-client-integrations

Type checking issues with getClient.mutate()

Open
#222 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
556
Forks
53
PR merge metrics
No merged PRs in 30d

Description

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:

```ts
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` 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.**

```ts
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

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by comparing the RSC getClient().mutate() typing described in the issue with the hook-based useMutation and the graphql-codegen-generated types. Reproduce both cases: an extra input field and an input object assigned to a variable; done means invalid variables produce build-time errors without requiring explicit generic types.

Written by the indexing model from the issue text.

Assessment

Tech stack
graphql, typescript
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.