dotansimha / dotansimha/graphql-code-generator-community

react-apollo option to invalidate query on each mutation

Open
#220 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
TypeScript
Stars
137
Forks
195
Avg merge
6h 20m
Merged PRs (30d)
16

Description

Apollos `refetchQueries` are very powerful but hard to maintain (This might be a duplicate or at least similar to dotansimha/graphql-code-generator-community#166).
Therefore this feature request tries to colocate mutations and refetchQueries.

Today GraphQL Code Generator compiles mutations into react hooks with a refetchQueries option:

```js
const [addProductToCart] = useAddProductToCartMutation();
addProductToCart({
variables: {
productId: 1234
},
refetchQueries: ["GET_CART_COUNT"],
})
```

However if you use the `useAddProductToCartMutation()` on multiple places in your code base you have to make sure that you keep the refetchQueries in sync.

What if GraphQL Code Generator would do that for us?

```ts
export function useAddProductToCartMutation(
baseOptions?: Apollo.MutationHookOptions<
AddProductToCartMutation,
AddProductToCartMutationVariables
>
) {
const options = { ...defaultOptions, ...baseOptions };
options.refetchQueries = ["GET_CART_COUNT"].concat(options.refetchQueries); // <--- Add the refetch here
return Apollo.useMutation<
AddProductToCartMutation,
AddProductToCartMutationVariables
>(Operations.ADD_PRODUCT_TO_CART, options);
}
```

But how could GrahQL Code Generator know about `"GET_CART_COUNT"`?

We could introduce some way to specify it directly inside the grahql definition e.g.:

```grahql
# @refetchQuery: GET_CART_COUNT
mutation ADD_PRODUCT_TO_CART
```

That way the mutation and refetches would be colocated and easier to maintain.

In case you think that would be a good idea I would start preparing on a PR :)

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.