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

查看:127
本文介绍了即使存在目标对象,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开发工具扩展,以下是整个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天全站免登陆