0.5.1 • Published 8 months ago
@graph-state/plugin-extend v0.5.1
@graph-state/plugin-ws
Plugin for extend Graph. May use like computed.
Installation
npm i @graph-state/core @graph-state/plugin-extend
or
yarn add @graph-state/core @graph-state/plugin-extend
Get started
import { createState } from '@graph-state/core';
import extendPlugin from '@graph-state/plugin-extend';
export const graphState = createState({
plugins: [
extendPlugin({
User: (cache, graph) => {
return {
...graph,
someProp: 'test'
}
}
}),
],
});
graphState.mutate({
_type: 'User',
_id: 'id'
})
graphState.resolve('User:id')
/**
* _type: 'User',
* _id: 'id'
* someProp: 'test'
*/
Extend method
Plugin append new method to GraphState instance
export const graphState = createState({
plugins: [extendPlugin()],
});
graphState.extendGraph('User:test', (cache, graph) => graph)
Lazy extends
You can add lazy extender.
export const graphState = createState({
plugins: [extendPlugin()],
});
setTimeout(() => {
// Extend all User graphs and add extender for next updates.
graphState.declareExtendGraph('User', (cache, graph) => graph)
}, 1000)