Created
March 26, 2019 21:02
-
-
Save fakenickels/883240cf7198483d32a2d10506a90fc9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @flow | |
import { ConnectionHandler } from 'relay-runtime'; | |
import { isObject, isArray } from 'lodash/fp'; | |
export function listRecordRemoveUpdater({ parentId, itemId, parentFieldName, store }) { | |
const parentProxy = store.get(parentId); | |
const items = parentProxy.getLinkedRecords(parentFieldName); | |
parentProxy.setLinkedRecords(items.filter(record => record._dataID !== itemId), parentFieldName); | |
} | |
export function listRecordAddUpdater({ parentId, item, type, parentFieldName, store }) { | |
const node = store.create(item.id, type); | |
Object.keys(item).forEach(key => { | |
node.setValue(item[key], key); | |
}); | |
const parentProxy = store.get(parentId); | |
const items = parentProxy.getLinkedRecords(parentFieldName); | |
parentProxy.setLinkedRecords([...items, node], parentFieldName); | |
} | |
export function connectionUpdater({ parentId, store, connectionName, edge }) { | |
const parentProxy = store.get(parentId); | |
const conn = ConnectionHandler.getConnection(parentProxy, connectionName); | |
ConnectionHandler.insertEdgeAfter(conn, edge); | |
} | |
export function connectionEdgeUpdater({ store, edge }) { | |
ConnectionHandler.update(store, edge); | |
} | |
export function optimisticConnectionUpdater({ parentId, store, connectionName, item, customNode, itemType }) { | |
const node = customNode || store.create(item.id, itemType); | |
!customNode && | |
Object.keys(item).forEach(key => { | |
node.setValue(item[key], key); | |
}); | |
const edge = store.create('client:newEdge:' + node._dataID.match(/[^:]+$/)[0], `${itemType}Edge`); | |
edge.setLinkedRecord(node, 'node'); | |
connectionUpdater({ edge, parentId, store, connectionName }); | |
} | |
export function connectionDeleteEdgeUpdater({ parentId, connectionName, nodeId, store }) { | |
const parentProxy = store.get(parentId); | |
const conn = ConnectionHandler.getConnection(parentProxy, connectionName); | |
if (!conn) { | |
console.warn(`Connection ${connectionName} not found on ${parentId}`); | |
return; | |
} | |
ConnectionHandler.deleteNode(conn, nodeId); | |
} | |
export function copyObjScalarsToProxy({ object, proxy }) { | |
Object.keys(object).forEach(key => { | |
if (isObject(object[key]) || isArray(object[key])) return; | |
proxy.setValue(object[key], key); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment