import { gql } from '@apollo/client';
const GET_EXISTING_NODE = gql query { node(id: $existingNodeId) { id name children { id name } } }
;
import { gql } from '@apollo/client';
const ADD_NODE = gql mutation AddNode($parentId: ID!, $nodeName: String!) { addNode(parentId: $parentId, nodeName: $nodeName) { id name } }
;
import { useMutation } from '@apollo/client';
const [addNode] = useMutation(ADD_NODE);
const handleAddNode = async (existingNodeId, newNodeName) => { const { data } = await addNode({ variables: { parentId: existingNodeId, nodeName: newNodeName }, refetchQueries: [{ query: GET_EXISTING_NODE, variables: { existingNodeId } }], }); };
在上述代码示例中,handleAddNode
函数在现有节点中添加新节点。它使用useMutation
钩子获取addNode
mutation,在需要时调用该mutation。refetchQueries
选项将更新GET_EXISTING_NODE
查询以便包括新节点。