1.0.4 • Published 5 years ago

use-add-mutation-update v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

use-add-mutation-update

Very simple and small hook for managing Apollo client add mutation with cache update

NPM JavaScript Style Guide

Install

npm install --save use-add-mutation-update

Usage

import React from 'react'
import { useAddMutation, Loading } from 'use-add-mutation-update'
import { gql } from 'apollo-boost';

const GET_TODOS = gql`
  query GetTodos {
    todos
  }
`;

const ADD_TODO = gql`
  mutation AddTodo($type: String!) {
    addTodo(type: $type) {
      id
      type
    }
  }
`;

const App = () => {
  const [addTodo, { error, loading, data }] = useAddMutation(
    ADD_TODO,
    GET_TODOS,
    'add_todo'/* mutation method name */,
    'get_todos' /* query method name */
  )
  if(loading) return <Loading>
  let input
  return (
    <div>
      <div>
        <form
          onSubmit={e => {
            e.preventDefault()
            addTodo({ variables: { type: input.value } })
            input.value = ''
          }}
        >
          <input
            ref={node => {
              input = node
            }}
          />
          <button type='submit'>Add Todo</button>
        </form>
      </div>
    </div>
  )
}
export default App

License

MIT © ephremdeme


This hook is created using create-react-hook.

1.0.2

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago