即使存在目标对象,Apollo readQuery 也会失败? [英] Apollo readQuery Fails Even Though Target Object is Present?

查看:27
本文介绍了即使存在目标对象,Apollo readQuery 也会失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm working on a call to readQuery. I'm getting an error message:

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 的对象作为分辨率列表中的第二个条目似乎很明显.

The object with that id appears to be plainly visible as the second entry in the list of resolutions.

这是我的查询:

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

...我是这样称呼它的:

...and here's how I'm calling it:

<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 数据存储:

Per the GraphQL Dev Tools extension for Chrome, here's the whole GraphQL data store:

{
  "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"
    }
  }
}

推荐答案

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

Posted as answer for fellow apollo users with similar problems:

去掉Resolution:的前缀,查询应该只取id.

Remove the prefix of Resolution:, the query should only take the id.

那么问题来了,你的数据存储是如何填充的?要从缓存中读取查询,需要在远程 API 上使用完全相同的参数调用该查询.通过这种方式,阿波罗知道带有特定参数的字段的结果是什么.如果您从未使用要使用的参数调用远程端点,但知道结果是什么,您可以绕过它并通过实现缓存解析器在本地解析查询.查看文档中的示例.在这里,商店包含一个图书列表(在您的情况下为 resultions),并且可以通过简单的缓存查找来解决按 id 查询单个图书的问题.

Then the question arises how is your datastore filled? To read a query from cache, the query needs to have been called with exactly the same arguments on the remote API before. This way apollo knows what the result for a field is with specific arguments. If you never called the remote endpoint with the arguments you want to use but know what the result would be, you can circumvent that and resolve the query locally by implementing a cache resolver. Have a look at the example in the documentation. Here the store contains a list of books (in your case resultions) and the query for a single book by id can be resolved with a simple cache lookup.

这篇关于即使存在目标对象,Apollo readQuery 也会失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆