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

本文是小编为大家收集整理的关于即使目标对象存在,Apollo readQuery也会失败?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在致电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开发工具扩展,这是整个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"
    }
  }
}

推荐答案

以类似问题的阿波罗用户的答案发布:

删除Resolution:的前缀,查询应仅采用ID.

然后,问题出现了您的数据存储如何填充? 要读取从缓存中的查询,需要调用查询以前在远程API上与完全相同的参数进行调用.这样,阿波罗就知道特定参数的字段结果是什么.如果您从未使用要使用的参数来调用远程端点,而是知道结果是什么,则可以通过实现缓存解析器来规避并在本地解决查询.在文档中.在这里,商店包含一本书列表(在您的情况下resultions),可以通过简单的缓存查找来解决单一书籍的查询.

本文地址:https://www.itbaoku.cn/post/1938067.html

问题描述

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
  }
}.

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

Here's my query:

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
            },
        });
    }}
>

What am I missing?

Update

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:

Remove the prefix of Resolution:, the query should only take the 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.