npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

apollo-mutation-state

v0.0.7

Published

A hoc to track an apollo mutation state

Downloads

1,153

Readme

Apollo mutation state

Apollo mutation state is a hoc to track an apollo mutation state.

Installation

With npm

$ npm install --save apollo-mutation-state

With yarn

$ yarn add apollo-mutation-state

Usage

withMutationState(Options)(TargetComponent)

Options

All options that can be passed to the hoc

| Option | Default value | Type | Description | | ------ | ------ | ------ | ------ | | mutationName | mutate | string | The name of the method "mutate" from the graphql hoc | | propName | mutation | string | The name of the prop that will be used to pass the mutation state object | | propagateError | false | boolean | Tells the hoc to propagate the error of the "mutate" method. If false, the hoc will not propagate the error from the mutate and you will not be able to ".catch" that error | | wrapper | false | boolean | Should be true if the hoc is the parent of a graphql hoc component. False if it is the child. | | wrapName | wrapMutate | string | The name of the method to wrap the mutation call. This method is only passed if wrapper is true |

MutateState object api

| Prop | Type | Description | | ------ | ------ | ------ | | loading | boolean | Indicates if the mutation is loading | | error | boolean | Indicates if there was an error with the mutation | | success | boolean | Indicates if the mutation was completed | | clearState() | Function | A function that can be called to clear the MutationState {loading: false, error: null, success: false} |

Example (Using the MutateState)

import React from 'react'

// You can change the prop "mutation" to another name using the "propName" option
const CreateUserButton = ({ mutation: { loading, error, success, clearState } }) => (
    <Button loading={loading}>Create User</Button>
);

export default CreateUserButton;

Example (withMutationState as a child of a graphql hoc)

import { graphql, gql, compose } from 'react-apollo';
import withMutationState from 'apollo-mutation-state';
import TargetComponent from './TargetComponent';

const CREATE_USER = /* gql mutation */;
const withMutation = graphql(CREATE_USER);

const enhance = compose(
    withMutation,
    withMutationState(), // this is a child of withMutation
);

export default enhance(TargetComponent);

Example (withMutationState as a parent of a graphql hoc)

import { graphql, gql, compose } from 'react-apollo';
import withMutationState from 'apollo-mutation-state';
import TargetComponent from './TargetComponent';

const CREATE_USER = /* gql mutation */;
const withMutation = graphql(CREATE_USER, {
    props: ({ mutate, ownProps: { wrapMutate } }) => ({
        createUser(user) {
            // You must use wrapMutate, so the hoc can track the mutation state
            wrapMutate(mutate({
                variables: { user },
            }))
            .then(response => console.log(response))
            .catch(err => console.error(err)); // You must set propagateError if you want to catch errors
        },
    }),
});

const enhance = compose(
    withMutationState({ wrapper: true, propagateError: true }), // this is a parent of withMutation
    withMutation,
);

export default enhance(TargetComponent);

Example (withMutationState as a parent of a graphql hoc and multiple mutations)

import { graphql, gql, compose } from 'react-apollo';
import withMutationState from 'apollo-mutation-state';
import TargetComponent from './TargetComponent';

const CREATE_USER = /* gql mutation */;
const DELETE_USER = /* gql mutation */;

const withCreateUser = graphql(CREATE_USER, {
    name: 'createUser', // You must set the name of the mutation if using multiple mutations <https://www.learnapollo.com/tutorial-react/react-06/>.
    props: ({ createUser, ownProps: { wrapMutate } }) => ({
        createUser(user) {
            // You must use wrapMutate, so the hoc can track the mutation state
            wrapMutate(createUser({
                variables: { user },
            }))
            .then(response => console.log(response))
            .catch(err => console.error(err)); // You must set propagateError if you want to catch errors
        },
    }),
});

const withDeleteUser = graphql(DELETE_USER, {
    name: 'deleteUser', // You must set the name of the mutation if using multiple mutations <https://www.learnapollo.com/tutorial-react/react-06/>.
    props: ({ deleteUser, ownProps: { wrapMutate } }) => ({
        deleteUser(id) {
            // You must use wrapMutate, so the hoc can track the mutation state
            wrapMutate(deleteUser({
                variables: { id },
            }))
            .then(response => console.log(response))
            .catch(err => console.error(err)); // You must set propagateError if you want to catch errors
        },
    }),
});

const enhance = compose(
    withMutationState({ wrapper: true, propagateError: true, propName: 'createUserState' }), // this is a parent of withMutation
    withCreateUser,
    withMutationState({ wrapper: true, propagateError: true, propName: 'deleteUserState' }),
    withDeleteUser,
);

export default enhance(TargetComponent);

Caveats

  • You should use this hoc as a wrapper if you want to implement custom logic after the mutation completes
  • You should use one withMutationState for each mutation
  • This hoc only works if using a mutation with one call at a time. If calling the same mutation multiple times in parallel, the hoc can't keep track of all mutation instances.

License

MIT