即使目标对象存在,Apollo readQuery也会失败?

时间:2018-04-15 19:20:20

标签: graphql apollo graphql-js apollo-client

我正在调用readQuery。我收到一条错误消息:

modules.js?hash=2d0033b4773d9cb6f118946043f7a3d4385825fe:25847 
Error: Can't find field resolutions({"id":"Resolution:DHSzPa8bvPCDjuAac"}) 
on object (ROOT_QUERY) {
  "resolutions": [
    {
      "type": "id",
      "id": "Resolution:AepgCCio9KWGkwyMC",
      "generated": false
    },
    {
      "type": "id",
      "id": "Resolution:DHSzPa8bvPCDjuAac",  // <==ID I'M SEEKING
      "generated": false
    }
  ],
  "user": {
    "type": "id",
    "id": "User:WWv57KsvqWeAoBNHY",
    "generated": false
  }
}.

具有该id的对象在分辨率列表中看起来明显可见为第二个条目。

这是我的疑问:

const GET_CURRENT_RESOLUTION_AND_GOALS = gql`
  query Resolutions($id: String!) {
    resolutions(id: $id) {
      _id
      name
      completed
      goals {
        _id
        name
        completed
      }
    }
  }
`;

......这就是我如何称呼它:

<Mutation
    mutation={CREATE_GOAL}
    update={(cache, {data: {createGoal}}) => {
        let id = 'Resolution:' + resolutionId;
        const {resolutions} = cache.readQuery({
            query: GET_CURRENT_RESOLUTION_AND_GOALS,
            variables: {
                id
            },
        });
    }}
>

我错过了什么?

更新

根据Chrome的GraphQL Dev Tools扩展,这是整个GraphQL数据存储:

{
  "data": {
    "resolutions": [
      {
        "_id": "AepgCCio9KWGkwyMC",
        "name": "testing 123",
        "completed": false,
        "goals": [
          {
            "_id": "TXq4nvukpLcqQhMRL",
            "name": "test goal abc",
            "completed": false,
            "__typename": "Goal"
          },
        ],
        "__typename": "Resolution"
      },
      {
        "_id": "DHSzPa8bvPCDjuAac",
        "name": "testing 345",
        "completed": false,
        "goals": [
          {
            "_id": "PEkg5oEEi2tJ6i8LH",
            "name": "goal abc",
            "completed": false,
            "__typename": "Goal"
          },
          {
            "_id": "X4H4dFzGm5gkq5bPE",
            "name": "goal bcd",
            "completed": false,
            "__typename": "Goal"
          },
          {
            "_id": "hYunrXsMq7Gme7Xck",
            "name": "goal cde",
            "completed": false,
            "__typename": "Goal"
          }
        "__typename": "Resolution"
      }
    ],
    "user": {
      "_id": "WWv57KsvqWeAoBNHY",
      "__typename": "User"
    }
  }
}

1 个答案:

答案 0 :(得分:0)

发布作为具有类似问题的阿波罗用户的答案:

删除Resolution:的前缀,查询应该只带id。

然后问题出现了您的数据存储区是如何填充的? 要从缓存中读取查询,需要先在远程API上使用完全相同的参数调用查询。这种方式apollo知道字段的结果是什么具有特定参数。如果您从未使用要使用的参数调用远程端点但知道结果是什么,则可以通过实现缓存解析器来规避并在本地解析查询。看一下documentation中的示例。这里的商店包含一个书籍列表(在您的情况下为resultions),并且可以通过简单的缓存查找来解析按ID的单个书籍的查询。