Skip to content

Allow custom functions for mutationΒ #453

@ziimakc

Description

@ziimakc

Maybe we can have useLazyMutation similar to react-query.

function AddTodo() {
	const [addTodo, { data, loading, error }] = useMutation(ADD_TODO);

	return (
		<div>
			<form
				onSubmit={(e) => {
					e.preventDefault();

					// this is a problem because
					// button is not disabled because `useMutation loading` is not part of this function
					// error thrown will not end up in `useMutation error` so you need to handle it separately
					const formData = new FormData(e.target as HTMLFormElement);
					if (formData.title.length < 10) {
						throw new Error("invalid input");
					}

					addTodo({ variables: { type: formData.title } });

					// would be nice to do something like this instead, similart to react query
					addTodo((mutationFn) => {
						const formData = new FormData(
							e.target as HTMLFormElement,
						);
						if (formData.title.length < 10) {
							throw new Error("invalid input");
						}

						return mutationFn({
							variables: {
								type: formData.title,
							},
						});
					});
				}}
			>
				<input name="title" />
				<button type="submit" disabled={loading}>
					Add Todo
				</button>
			</form>
		</div>
	);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    coreFeature requests related to core functionality

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions