der*_*ery 22 javascript graphql apollo-server react-apollo
我有一个突变
mutation deleteRecord($id: ID) {
deleteRecord(id: $id) {
id
}
}
Run Code Online (Sandbox Code Playgroud)
在另一个位置我有一个元素列表.
我可以从服务器返回更好的东西,我该如何更新列表?
更一般地说,在apollo/graphql中处理删除的最佳做法是什么?
vwr*_*bel 15
我不确定它是不是很好的练习风格,但这里是我如何使用updateQueries处理react-apollo中项目的删除:
import { graphql, compose } from 'react-apollo';
import gql from 'graphql-tag';
import update from 'react-addons-update';
import _ from 'underscore';
const SceneCollectionsQuery = gql `
query SceneCollections {
myScenes: selectedScenes (excludeOwner: false, first: 24) {
edges {
node {
...SceneCollectionScene
}
}
}
}`;
const DeleteSceneMutation = gql `
mutation DeleteScene($sceneId: String!) {
deleteScene(sceneId: $sceneId) {
ok
scene {
id
active
}
}
}`;
const SceneModifierWithStateAndData = compose(
...,
graphql(DeleteSceneMutation, {
props: ({ mutate }) => ({
deleteScene: (sceneId) => mutate({
variables: { sceneId },
updateQueries: {
SceneCollections: (prev, { mutationResult }) => {
const myScenesList = prev.myScenes.edges.map((item) => item.node);
const deleteIndex = _.findIndex(myScenesList, (item) => item.id === sceneId);
if (deleteIndex < 0) {
return prev;
}
return update(prev, {
myScenes: {
edges: {
$splice: [[deleteIndex, 1]]
}
}
});
}
}
})
})
})
)(SceneModifierWithState);
Run Code Online (Sandbox Code Playgroud)
sin*_*ned 11
这是一个类似的解决方案,无需underscore.js.它react-apollo
在2.1.1版中进行了测试.并为删除按钮创建一个组件:
import React from "react";
import { Mutation } from "react-apollo";
const GET_TODOS = gql`
{
allTodos {
id
name
}
}
`;
const DELETE_TODO = gql`
mutation deleteTodo(
$id: ID!
) {
deleteTodo(
id: $id
) {
id
}
}
`;
const DeleteTodo = ({id}) => {
return (
<Mutation
mutation={DELETE_TODO}
update={(cache, { data: { deleteTodo } }) => {
const { allTodos } = cache.readQuery({ query: GET_TODOS });
cache.writeQuery({
query: GET_TODOS,
data: { allTodos: allTodos.filter(e => e.id !== id)}
});
}}
>
{(deleteTodo, { data }) => (
<button
onClick={e => {
deleteTodo({
variables: {
id
}
});
}}
>Delete</button>
)}
</Mutation>
);
};
export default DeleteTodo;
Run Code Online (Sandbox Code Playgroud)
所有这些答案都假定面向查询的缓存管理。
如果我user
使用 id删除1
并且该用户在整个应用程序的 20 个查询中被引用怎么办?阅读上面的答案,我不得不假设我将不得不编写代码来更新所有这些的缓存。这对于代码库的长期可维护性来说是很糟糕的,并且会使任何重构都变成一场噩梦。
我认为最好的解决方案apolloClient.removeItem({__typeName: "User", id: "1"})
是这样的:
null
[User]
在任何查询的任何列表中过滤掉此项但它不存在(还)
这可能是个好主意,也可能更糟(例如,它可能会破坏分页)
有关于它的有趣讨论:https : //github.com/apollographql/apollo-client/issues/899
我会小心那些手动查询更新。一开始看起来很开胃,但如果你的应用程序增长就不会了。至少在其顶部创建一个可靠的抽象层,例如:
const MY_QUERY = gql``;
// it's local 'cleaner' - relatively easy to maintain as you can require proper cleaner updates during code review when query will change
export function removeUserFromMyQuery(apolloClient, userId) {
// clean here
}
Run Code Online (Sandbox Code Playgroud)
然后,收集所有这些更新并在最终更新中调用它们
function handleUserDeleted(userId, client) {
removeUserFromMyQuery(userId, client)
removeUserFromSearchQuery(userId, client)
removeIdFrom20MoreQueries(userId, client)
}
Run Code Online (Sandbox Code Playgroud)
对于 Apollo v3,这对我有用:
const [deleteExpressHelp] = useDeleteExpressHelpMutation({
update: (cache, {data}) => {
cache.evict({
id: cache.identify({
__typename: 'express_help',
id: data?.delete_express_help_by_pk?.id,
}),
});
},
});
Run Code Online (Sandbox Code Playgroud)
从全新的文档:
从缓存的数组字段中过滤悬空引用(如上面的 Deity.offspring 示例)非常普遍,以至于 Apollo Client 会自动为未定义读取函数的数组字段执行此过滤。
归档时间: |
|
查看次数: |
13398 次 |
最近记录: |