中继 prisma graphql 更新存储
relay prisma graphql update store
我正在尝试让 prisma 和中继正常工作。这是我的回购协议:
https://github.com/jamesmbowler/prisma-relay-todo
这是一个简单的待办事项列表。我可以添加待办事项,但 ui 不会更新。当我刷新时,待办事项就在那里。
我能找到的所有更新商店的示例,对正在更新/创建的 object 使用 "parent"。
见https://facebook.github.io/relay/docs/en/mutations.html#using-updater-and-optimisticupdater
另外,"updater configs" 也需要uires a "parentID"。 https://facebook.github.io/relay/docs/en/mutations.html#updater-configs
来自 relay-runtime 的 RelayConnectionHandler.js 评论:
https://github.com/facebook/relay/blob/master/packages/relay-runtime/handlers/connection/RelayConnectionHandler.js#L232
* store => {
* const user = store.get('<id>');
* const friends = RelayConnectionHandler.getConnection(user, 'FriendsFragment_friends');
* const edge = store.create('<edge-id>', 'FriendsEdge');
* RelayConnectionHandler.insertEdgeAfter(friends, edge);
* }
是否可以在没有 "parent" 的情况下更新商店?我只有待办事项,没有 parent.
同样,创建记录有效,并给出以下响应:
{ "data" : {
"createTodo" : {
"id" : "cjpdbivhc00050903ud6bkl3x",
"name" : "testing",
"complete" : false
} } }
这是我的更新函数
updater: store => {
const payload = store.getRootField('createTodo');
const conn = ConnectionHandler.getConnection(store.get(payload.getDataID()), 'Todos_todoesConnection');
ConnectionHandler.insertEdgeAfter(conn, payload, cursor);
},
我做了一个console.log(conn),它是未定义的。
请帮忙。
----编辑----
多亏了 Denis,我认为一个问题已经解决了——ConnectionHandler 的问题。
但是,我仍然无法让 ui 更新。这是我在更新程序功能中尝试过的:
const payload = store.getRootField('createTodo');
const clientRoot = store.get('client:root');
const conn = ConnectionHandler.getConnection(clientRoot, 'Todos_todoesConnection');
ConnectionHandler.createEdge(store, conn, payload, 'TodoEdge');
我也试过这个:
const payload = store.getRootField('createTodo');
const clientRoot = store.get('client:root');
const conn = ConnectionHandler.getConnection(clientRoot, 'Todos_todoesConnection');
ConnectionHandler.insertEdgeAfter(conn, payload);
我的数据形状与他们的示例不同,因为我返回的数据中没有 'todoEdge' 和 'node'(见上文)。
todoEdge {
cursor
node {
complete
id
text
}
}
像这样如何获取LinkedRecord?
const newEdge = payload.getLinkedRecord('todoEdge');
如果 query
是父级,则 parentID
将是 client:root
。
也在这个问题上:https://github.com/facebook/relay/issues/2157#issuecomment-385009482
创建一个 const ROOT_ID = 'client:root';
并将 ROOT_ID
作为您的 parentID
传递。另外,检查更新程序上的连接名称,它必须与您声明查询的名称完全相同。
更新:
实际上,您可以导入 ROOT_ID
of relay-runtime
import { ROOT_ID } from 'relay-runtime';
更新 2:
你的编辑对我来说不是很清楚,但我会为你提供一个例子,应该行得通吗?在你的变异是 运行 之后,你首先像你正在做的那样使用 getRootField
访问它的数据。所以,如果我有一个像这样的突变:
mutation UserAddMutation($input: UserAddInput!) {
UserAdd(input: $input) {
userEdge {
node {
name
id
age
}
}
error
}
}
你会做:
const newEdge = store.getRootField('UserAdd').getLinkedRecord('userEdge');
connectionUpdater({
store,
parentId: ROOT_ID,
connectionName: 'UserAdd_users',
edge: newEdge,
before: true,
});
这个 connectionUpdater
是一个辅助函数,看起来像这样:
export function connectionUpdater({ store, parentId, connectionName, edge, before, filters }) {
if (edge) {
if (!parentId) {
// eslint-disable-next-line
console.log('maybe you forgot to pass a parentId: ');
return;
}
const parentProxy = store.get(parentId);
const connection = ConnectionHandler.getConnection(parentProxy, connectionName, filters);
if (!connection) {
// eslint-disable-next-line
console.log('maybe this connection is not in relay store yet:', connectionName);
return;
}
const newEndCursorOffset = connection.getValue('endCursorOffset');
connection.setValue(newEndCursorOffset + 1, 'endCursorOffset');
const newCount = connection.getValue('count');
connection.setValue(newCount + 1, 'count');
if (before) {
ConnectionHandler.insertEdgeBefore(connection, edge);
} else {
ConnectionHandler.insertEdgeAfter(connection, edge);
}
}
}
希望对您有所帮助:)
我正在尝试让 prisma 和中继正常工作。这是我的回购协议:
https://github.com/jamesmbowler/prisma-relay-todo
这是一个简单的待办事项列表。我可以添加待办事项,但 ui 不会更新。当我刷新时,待办事项就在那里。
我能找到的所有更新商店的示例,对正在更新/创建的 object 使用 "parent"。
见https://facebook.github.io/relay/docs/en/mutations.html#using-updater-and-optimisticupdater
另外,"updater configs" 也需要uires a "parentID"。 https://facebook.github.io/relay/docs/en/mutations.html#updater-configs
来自 relay-runtime 的 RelayConnectionHandler.js 评论: https://github.com/facebook/relay/blob/master/packages/relay-runtime/handlers/connection/RelayConnectionHandler.js#L232
* store => {
* const user = store.get('<id>');
* const friends = RelayConnectionHandler.getConnection(user, 'FriendsFragment_friends');
* const edge = store.create('<edge-id>', 'FriendsEdge');
* RelayConnectionHandler.insertEdgeAfter(friends, edge);
* }
是否可以在没有 "parent" 的情况下更新商店?我只有待办事项,没有 parent.
同样,创建记录有效,并给出以下响应:
{ "data" : { "createTodo" : { "id" : "cjpdbivhc00050903ud6bkl3x", "name" : "testing", "complete" : false } } }
这是我的更新函数
updater: store => {
const payload = store.getRootField('createTodo');
const conn = ConnectionHandler.getConnection(store.get(payload.getDataID()), 'Todos_todoesConnection');
ConnectionHandler.insertEdgeAfter(conn, payload, cursor);
},
我做了一个console.log(conn),它是未定义的。
请帮忙。
----编辑---- 多亏了 Denis,我认为一个问题已经解决了——ConnectionHandler 的问题。
但是,我仍然无法让 ui 更新。这是我在更新程序功能中尝试过的:
const payload = store.getRootField('createTodo');
const clientRoot = store.get('client:root');
const conn = ConnectionHandler.getConnection(clientRoot, 'Todos_todoesConnection');
ConnectionHandler.createEdge(store, conn, payload, 'TodoEdge');
我也试过这个:
const payload = store.getRootField('createTodo');
const clientRoot = store.get('client:root');
const conn = ConnectionHandler.getConnection(clientRoot, 'Todos_todoesConnection');
ConnectionHandler.insertEdgeAfter(conn, payload);
我的数据形状与他们的示例不同,因为我返回的数据中没有 'todoEdge' 和 'node'(见上文)。
todoEdge { cursor node { complete id text } }
像这样如何获取LinkedRecord?
const newEdge = payload.getLinkedRecord('todoEdge');
如果 query
是父级,则 parentID
将是 client:root
。
也在这个问题上:https://github.com/facebook/relay/issues/2157#issuecomment-385009482
创建一个 const ROOT_ID = 'client:root';
并将 ROOT_ID
作为您的 parentID
传递。另外,检查更新程序上的连接名称,它必须与您声明查询的名称完全相同。
更新:
实际上,您可以导入 ROOT_ID
of relay-runtime
import { ROOT_ID } from 'relay-runtime';
更新 2:
你的编辑对我来说不是很清楚,但我会为你提供一个例子,应该行得通吗?在你的变异是 运行 之后,你首先像你正在做的那样使用 getRootField
访问它的数据。所以,如果我有一个像这样的突变:
mutation UserAddMutation($input: UserAddInput!) {
UserAdd(input: $input) {
userEdge {
node {
name
id
age
}
}
error
}
}
你会做:
const newEdge = store.getRootField('UserAdd').getLinkedRecord('userEdge');
connectionUpdater({
store,
parentId: ROOT_ID,
connectionName: 'UserAdd_users',
edge: newEdge,
before: true,
});
这个 connectionUpdater
是一个辅助函数,看起来像这样:
export function connectionUpdater({ store, parentId, connectionName, edge, before, filters }) {
if (edge) {
if (!parentId) {
// eslint-disable-next-line
console.log('maybe you forgot to pass a parentId: ');
return;
}
const parentProxy = store.get(parentId);
const connection = ConnectionHandler.getConnection(parentProxy, connectionName, filters);
if (!connection) {
// eslint-disable-next-line
console.log('maybe this connection is not in relay store yet:', connectionName);
return;
}
const newEndCursorOffset = connection.getValue('endCursorOffset');
connection.setValue(newEndCursorOffset + 1, 'endCursorOffset');
const newCount = connection.getValue('count');
connection.setValue(newCount + 1, 'count');
if (before) {
ConnectionHandler.insertEdgeBefore(connection, edge);
} else {
ConnectionHandler.insertEdgeAfter(connection, edge);
}
}
}
希望对您有所帮助:)