ApolloGraphQL - ReadQuery`Field Not Found`错误?

时间:2018-06-09 23:20:11

标签: graphql apollo apollo-client apollostack

我的readQuery返回field not found错误,即使该字段似乎存在于缓存中。

QUERY

const GETIMSFROMCACHE_QUERY = gql`
query getIMsFromCache($fromID: String!){
  instant_message(fromID:$fromID){
    id,
    fromID,
    toID,
    msgText
  }
}  `;

CACHE RESOLVER

client.cache.cacheResolvers = {
    Query: {
        instant_message: (_, args) => toIdValue(client.dataIdFromObject({
            __typename: 'instant_message',
            id: args.id
        })),
    },
};

READQUERY CALL,IN UPDATE

let instant_message = cache.readQuery({ query: GETIMSFROMCACHE_QUERY, variables: {"fromID": fromID} });

错误

Error: Can't find field instant_message({"fromID":"ayqNLcA7c6r8vip3i"}) on object (ROOT_QUERY) {
  "getMyUserData({\"id\":\"ayqNLcA7c6r8vip3i\"})": [
    {
      "type": "id",
      "generated": false,
      "id": "MyUserData:ayqNLcA7c6r8vip3i",
      "typename": "MyUserData"
    }
  ],
  "getMyUserData({\"id\":\"9W95z8A7Y6i34buk7\"})": [
    {
      "type": "id",
      "generated": false,
      "id": "MyUserData:9W95z8A7Y6i34buk7",
      "typename": "MyUserData"
    }
  ],
  "Appts({\"originatingUserID\":\"ayqNLcA7c6r8vip3i\"})": [],
  "instant_message({\"fromID\":\"ayqNLcA7c6r8vip3i\",\"toID\":\"9W95z8A7Y6i34buk7\"})": [
    {
      "type": "id",
      "generated": false,
      "id": "instant_message:126",
      "typename": "instant_message"
    },
    {
      "type": "id",
      "generated": false,
      "id": "instant_message:127",
      "typename": "instant_message"
    },
    {
      "type": "id",
      "generated": false,
      "id": "instant_message:128",
      "typename": "instant_message"
    }
  ]
}.

查看错误消息,ROOT_QUERY对象上似乎有一个instant_message对象,其中包含目标用户ID,但我收到此错误。

我该如何纠正?

提前致谢所有信息。

1 个答案:

答案 0 :(得分:0)

解决!这很棘手,因为原始查询的常规解析器会从两个用户中的任何一个带回任何来自的IM。解析器会返回任何来自fromIDtoID或反之亦然的instant_messages。

所以我认为在查询缓存时我需要某种缓存解析器重复此操作。

最终我意识到缓存并不关心解析器中发生的事情 - 它会将instant_message对象存储为fromIDtoID,无论解析器中发生了什么

一旦我意识到这一点,我就删除了特殊的缓存解析器查询,并且只使用了首先检索instant_messages的原始查询,并且首先使用与它相同的查询变量,并且它起作用。 :)