You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用apollo-link-state保存@client数据时出现“selectionSet is null”错误

嘿,我看到你在调用本地状态mutation时碰到了Network error: selectionSet is null这个报错,这问题其实挺常见的,大概率是你在cache.writeData的参数格式上出了问题,咱们来快速修复它。

问题根源

你当前的cache.writeData调用直接把navigationState作为顶级参数传进去了,但Apollo的writeData方法要求必须把要写入的数据放在data属性里。缺少这个data包裹的话,Apollo内部无法找到对应的selectionSet,就会抛出这个TypeError。

修正后的代码

把你的resolver改成下面这样,核心就是给writeData加上data字段包裹:

const stateLink = withClientState({
  cache,
  resolvers: {
    Mutation: {
      setNavigationsState: (_: any, { path }: any, { cache }: any): any => {
        cache.writeData({
          data: { // 这里必须加上data包裹
            navigationState: {
              __typename: 'NavigationState',
              path
            }
          }
        });
        return null;
      },
    },
  },
  defaults: {
    navigationState: { // 记得补全默认值的__typename和字段哦
      __typename: 'NavigationState',
      path: '' // 替换成你的默认路径值
    }
  }
});

额外注意事项

  • 确保defaults里的navigationState包含正确的__typename和默认字段值,不然缓存初始化阶段也可能出现异常。
  • 调用mutation时,你的GraphQL操作定义最好保持正确的结构(哪怕返回null),比如:
mutation SetNavigationsState($path: String!) {
  setNavigationsState(path: $path) {
    path
  }
}

这样调整后,Apollo就能正确识别要写入的缓存数据,那个selectionSet is null的错误应该就消失啦。

内容的提问来源于stack exchange,提问作者somerandomusername

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:37:57