@sparklink-pro/apant
Version:
Apollo & Antd tools
35 lines • 1.51 kB
JavaScript
import { __rest } from "tslib";
import { useMutation } from '@apollo/client/react';
import { isArray } from 'lodash-es';
import { OPERATION_UPDATE, OPERATION_LIST } from '../definitions';
import { useRegistry } from './useRegistry';
/**
* This hook executes a mutation for a given type. It use `useMutation` to perform a GraphQL mutation.
* It will use the default mutation operation defined in the registry (OPERATION_CREATE, OPERATION_DELETE, OPERATION_UPDATE) for the type.
* The mutation is expected to return a valid payload of the form `{ item: {} }`.
*/
export const useTypeMutation = (_a) => {
var { type, operation = OPERATION_UPDATE, refreshList = true } = _a, options = __rest(_a, ["type", "operation", "refreshList"]);
const types = useRegistry();
const mutation = types.getOperation(type, operation);
const mutationOptions = Object.assign({}, options);
if (refreshList) {
const queryList = types.getOperation(type, OPERATION_LIST);
if (mutationOptions.refetchQueries) {
if (isArray(mutationOptions.refetchQueries)) {
mutationOptions.refetchQueries.push({ query: queryList });
}
}
else {
mutationOptions.refetchQueries = [{ query: queryList }];
}
}
const [mutate, { data, loading }] = useMutation(mutation, mutationOptions);
return {
data,
loading,
mutate,
};
};
export default useTypeMutation;
//# sourceMappingURL=useTypeMutation.js.map