trojanowski / react-apollo-hooks

Use Apollo Client as React hooks
MIT License
2.41k stars 109 forks source link

Reconsider useMutation API #203

Open gajus opened 5 years ago

gajus commented 5 years ago

The current API looks like this:

const [
  scrapeVenuesResult,
  {
    error,
    loading,
  },
] = useMutation(SCRAPE_VENUES_MUTATION);

This looks neat on its own, but becomes a problem when there are many mutations since all of the variables now need to be aliased and end up looking more like:

const [
  scrapeVenues,
  {
    error: scrapeVenuesMutationError,
    loading: scrapeVenuesMutationLoading,
  },
] = useMutation(SCRAPE_VENUES_MUTATION);

A neater API would return a single object describing the entire state of the mutation as a property the mutation function, e.g.

const scrapeVenues = useMutation(SCRAPE_VENUES_MUTATION);

scrapeVenues();
scrapeVenues.error;
scrapeVenues.loading;
scrapeVenues.result;

This would automatically provide a namespace for all properties of a mutation.

zsolt-dev commented 5 years ago

Both projects (this and official apollo-hooks) are in beta and it makes sense to go with the best possible solution now instead of everyone writing their own wrappers.

This would also make it more like the useQuery hook.

sijad commented 5 years ago

isn't it still possible?

const [doMutateScrape, scrapeVenues] = useMutation(SCRAPE_VENUES_MUTATION);

doMutateScrape();
scrapeVenues.error;
scrapeVenues.loading;
scrapeVenues.result;
gajus commented 5 years ago

See https://github.com/apollographql/react-apollo/issues/3303 discussion.