How to use the urql.useMutation function in urql

To help you get started, we’ve selected a few urql examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github FormidableLabs / urql / examples / 3-ssr-with-nextjs / components / Submit.js View on Github external
export default function Submit() {
  const [_, executeMutation] = useMutation(createPostMutation);

  const handleSubmit = useCallback(
    event => {
      event.preventDefault();
      const form = event.target;
      const formData = new window.FormData(form);
      const title = formData.get('title');
      const url = formData.get('url');
      form.reset();

      executeMutation({
        title, 
        url 
      });
    },
    [executeMutation]
github FormidableLabs / urql / examples / 1-getting-started / src / app / Home.tsx View on Github external
export const Home: FC = () => {
  const [res, executeQuery] = useQuery({ query: TodoQuery });
  const refetch = useCallback(
    () => executeQuery({ requestPolicy: 'network-only' }),
    [executeQuery]
  );

  const [toggleTodoMutation, executeToggleTodoMutation] = useMutation(
    ToggleTodo
  );
  const [deleteTodoMutation, executeDeleteTodoMutation] = useMutation(
    DeleteTodo
  );

  const todos = useMemo(() => {
    if (res.fetching || res.data === undefined) {
      return ;
    }

    if (res.error) {
      return {res.error.message};
    }

    return (
      <ul>
        {res.data.todos.map((todo: ITodo) =&gt; (
          </ul>
github FormidableLabs / urql / examples / 1-getting-started / src / app / components / Todo.tsx View on Github external
export const Todo: FC = props =&gt; {
  const [mutation, executeMutation] = useMutation(RemoveTodo);

  const handleToggle = () =&gt; executeMutation({ id: props.id });

  return (
    <li>
      <p>{props.text}</p>
      {mutation.fetching &amp;&amp; <span>(updating)</span>}
    </li>
  );
};
github FormidableLabs / urql / examples / 3-ssr-with-nextjs / components / PostUpvoter.js View on Github external
export default function PostUpvoter({ votes, id }) {
  const [_, executeMutation] = useMutation(updatePostMutation);

  const upvotePost = useCallback(() =&gt; {
    executeMutation({
      id,
      votes: votes + 1,
    });
  }, [votes, id, executeMutation]);

  return (
    <button>
      {votes}

      <style>{`
        button {
          background-color: transparent;
          border: 1px solid #e4e4e4;</style></button>
github carlos-kelly / publications / src / contexts / documents-provider.tsx View on Github external
export default function DocumentsProvider(props: Props) {
  const [
    { data: currentUserData, fetching: userFetching },
    refetchCurrentUser,
  ] = useQuery({
    query: currentUserQuery,
    requestPolicy: "network-only",
  });
  const [{ data: docsData }, refreshDocsData] = useQuery({
    query: documentsQuery,
    requestPolicy: "network-only",
  });
  const [, saveDocumentAction] = useMutation&lt;
    SaveDocumentMutationResponse,
    SaveDocumentMutation
  &gt;(saveDocumentMutation);
  const [, deleteDocumentAction] = useMutation&lt;
    DeleteDocumentMutationResponse,
    DeleteDocumentMutation
  &gt;(deleteDocumentMutation);
  const [, login] = useMutation(
    loginMutation
  );
  const [, createUser] = useMutation&lt;
    CreateUserMutationResponse,
    CreateUserMutation
  &gt;(createUserMutation);

  const documents: PubDocument[] | null = React.useMemo(
github carlos-kelly / publications / src / contexts / documents-provider.tsx View on Github external
query: currentUserQuery,
    requestPolicy: "network-only",
  });
  const [{ data: docsData }, refreshDocsData] = useQuery({
    query: documentsQuery,
    requestPolicy: "network-only",
  });
  const [, saveDocumentAction] = useMutation&lt;
    SaveDocumentMutationResponse,
    SaveDocumentMutation
  &gt;(saveDocumentMutation);
  const [, deleteDocumentAction] = useMutation&lt;
    DeleteDocumentMutationResponse,
    DeleteDocumentMutation
  &gt;(deleteDocumentMutation);
  const [, login] = useMutation(
    loginMutation
  );
  const [, createUser] = useMutation&lt;
    CreateUserMutationResponse,
    CreateUserMutation
  &gt;(createUserMutation);

  const documents: PubDocument[] | null = React.useMemo(
    () =&gt; documentsWithEditorState(get("documents")(docsData)),
    [docsData]
  );

  const user: PubUser | null = get("currentUser")(currentUserData) || null;
  const dataLoaded = !!documents;
  const [
    currentDocument,
github carlos-kelly / publications / src / contexts / documents-provider.tsx View on Github external
const [
    { data: currentUserData, fetching: userFetching },
    refetchCurrentUser,
  ] = useQuery({
    query: currentUserQuery,
    requestPolicy: "network-only",
  });
  const [{ data: docsData }, refreshDocsData] = useQuery({
    query: documentsQuery,
    requestPolicy: "network-only",
  });
  const [, saveDocumentAction] = useMutation&lt;
    SaveDocumentMutationResponse,
    SaveDocumentMutation
  &gt;(saveDocumentMutation);
  const [, deleteDocumentAction] = useMutation&lt;
    DeleteDocumentMutationResponse,
    DeleteDocumentMutation
  &gt;(deleteDocumentMutation);
  const [, login] = useMutation(
    loginMutation
  );
  const [, createUser] = useMutation&lt;
    CreateUserMutationResponse,
    CreateUserMutation
  &gt;(createUserMutation);

  const documents: PubDocument[] | null = React.useMemo(
    () =&gt; documentsWithEditorState(get("documents")(docsData)),
    [docsData]
  );
github carlos-kelly / publications / src / contexts / documents-provider.tsx View on Github external
const [{ data: docsData }, refreshDocsData] = useQuery({
    query: documentsQuery,
    requestPolicy: "network-only",
  });
  const [, saveDocumentAction] = useMutation&lt;
    SaveDocumentMutationResponse,
    SaveDocumentMutation
  &gt;(saveDocumentMutation);
  const [, deleteDocumentAction] = useMutation&lt;
    DeleteDocumentMutationResponse,
    DeleteDocumentMutation
  &gt;(deleteDocumentMutation);
  const [, login] = useMutation(
    loginMutation
  );
  const [, createUser] = useMutation&lt;
    CreateUserMutationResponse,
    CreateUserMutation
  &gt;(createUserMutation);

  const documents: PubDocument[] | null = React.useMemo(
    () =&gt; documentsWithEditorState(get("documents")(docsData)),
    [docsData]
  );

  const user: PubUser | null = get("currentUser")(currentUserData) || null;
  const dataLoaded = !!documents;
  const [
    currentDocument,
    setCurrentDocument,
  ] = React.useState(null);
  const [
github FormidableLabs / urql / examples / 1-getting-started / src / app / components / NewTodo.tsx View on Github external
export const NewTodo: FC = () =&gt; {
  const [value, setValue] = useState('');

  const [addTodoMutation, executeAddTodoMutation] = useMutation(AddTodo);

  function addTodo() {
    executeAddTodoMutation({ text: value });
    setValue('');
  }

  return (
    <div>
      <input value="{value}" required="" type="text"> setValue(e.target.value)}
      /&gt;
      </div>
github FormidableLabs / urql / examples / 1-getting-started / src / app / Home.tsx View on Github external
export const Home: FC = () =&gt; {
  const [res, executeQuery] = useQuery({ query: TodoQuery });
  const refetch = useCallback(
    () =&gt; executeQuery({ requestPolicy: 'network-only' }),
    [executeQuery]
  );

  const [toggleTodoMutation, executeToggleTodoMutation] = useMutation(
    ToggleTodo
  );
  const [deleteTodoMutation, executeDeleteTodoMutation] = useMutation(
    DeleteTodo
  );

  const todos = useMemo(() =&gt; {
    if (res.fetching || res.data === undefined) {
      return ;
    }

    if (res.error) {
      return {res.error.message};
    }

    return (

urql

A highly customizable and versatile GraphQL client for React

MIT
Latest version published 2 months ago

Package Health Score

98 / 100
Full package analysis