greed-optimist
v0.2.1
Published
Greed Optimistic Updates
Downloads
6
Maintainers
Readme
Greed-Optimist
A Greed Plugin for Optimistically Updating Data
This modules purpose is to provide a solution for optimistically updating data and the views that depend on it. This module should be placed on the greed options object.
Install
npm install --save greed-optimist
Usage
import Greed from 'greed';
import optimist from 'greed-optimist';
var gql = Greed({
url: 'https://localhost:3000/',
token: 'someCrazySuperSecretToken',
plugins: {
pre: [
optimist,
],
post: [
]
}
});
gql(`{
count(id: 2){
count
}
}`, optimistic, success, fail);
You simply add the greed-optimist
to the options plugin field and the plugin adds
three parameters to the returned function call. The parameters takes three functions
one is handler for what to update immediately when you make the request, this is the "optimistic"
update, then success and failure handlers, the handle those 2 respective outcomes. You may be wondering whats the point of using this module if I'm still covering these cases. Great question!
What this module enables is the ability to automatically rollback to the previous correct state.
Meaning when a network request fails we automatically rollback the state/data to the
previous correct state, and in turn updates all the views that depend on that data. Allowing
your fail handler to show a notification, try again, etc. in response to the failure.
License
MIT