Skip to content

(v4) Be able to overwrite variables in mutate #946

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

Merged
merged 1 commit into from
Jul 27, 2020

Conversation

kawamataryo
Copy link
Contributor

@kawamataryo kawamataryo commented Apr 4, 2020

Thank you for a very useful library.

I couldn't overwrite the variable specified by the initial value in mutate, so I fixed it.

When I wrote the following code, I couldn't overwrite a variable.
I think it would be better to make it overwritable, how about that?

   const DELETE_PRODUCT = gql`
    mutation DeleteProduct($id: Int!) {
        delete_products(where: {id: {
            _eq: $id
        }}){
            returning {
                id
                name
            }
        }
    }
   `
    const { mutate: deleteProduct } = useMutation<
      DeleteProductMutation,
      DeleteProductMutationVariables
    >(DELETE_PRODUCT, () => ({
      variables: {
        id: 0
      },
      refetchQueries: [{ query: FETCH_PRODUCTS }]
    }));

    const onDeleteProduct = (productId: number) => {
      deleteProduct({ id: productId }); 
      // At the time of request, the id of the variable will be the '0' specified at the time of useMutation instead of the value of this productId.
    };

I could do the following, but I'd have to use any to avoid type errors.

    const { mutate: deleteProduct } = useMutation<
      DeleteProductMutation
    >(DELETE_PRODUCT, () => ({
      refetchQueries: [{ query: FETCH_PRODUCTS }]
    }));

    const onDeleteProduct = (productId: number) => {
      deleteProduct({ id: productId } as any); 
    };

@kawamataryo kawamataryo changed the title Be able to overwrite variables in mutate (v4) :Be able to overwrite variables in mutate Apr 4, 2020
@kawamataryo kawamataryo changed the title (v4) :Be able to overwrite variables in mutate (v4) Be able to overwrite variables in mutate Apr 4, 2020
@bbugh
Copy link
Contributor

bbugh commented Apr 24, 2020

This looks great to me, I can see how that would be a problem. 👍

@kawamataryo
Copy link
Contributor Author

kawamataryo commented Jun 9, 2020

@Akryum

Could you please review it.
If you have any problems with merging, let me know and I'll fix it.

@Akryum Akryum merged commit 1867e73 into vuejs:v4 Jul 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants