Skip to content

Latest commit

 

History

History
66 lines (51 loc) · 1.7 KB

use-mutation.md

File metadata and controls

66 lines (51 loc) · 1.7 KB
title isDefaultIndex generated
UseMutation
false
true

import MemberInfo from '@site/src/components/MemberInfo'; import GenerationInfo from '@site/src/components/GenerationInfo'; import MemberDescription from '@site/src/components/MemberDescription';

useMutation

A React hook which allows you to execute a GraphQL mutation.

Example

import { useMutation } from '@vendure/admin-ui/react';
import { gql } from 'graphql-tag';

const UPDATE_PRODUCT = gql`
  mutation UpdateProduct($input: UpdateProductInput!) {
    updateProduct(input: $input) {
    id
    name
  }
}`;

export const MyComponent = () => {
    const [updateProduct, { data, loading, error }] = useMutation(UPDATE_PRODUCT);

    const handleClick = () => {
        updateProduct({
            input: {
                id: '1',
                name: 'New name',
            },
        }).then(result => {
            // do something with the result
        });
    };

    if (loading) return <div>Loading...</div>;
    if (error) return <div>Error! { error }</div>;

    return (
    <div>
        <button onClick={handleClick}>Update product</button>
        {data && <div>Product updated!</div>}
    </div>
    );
};
function useMutation<T, V extends Record<string, any> = Record<string, any>>(mutation: DocumentNode | TypedDocumentNode<T, V>): void

Parameters

mutation

<MemberInfo kind="parameter" type={DocumentNode | TypedDocumentNode&#60;T, V&#62;} />