1

我看到 ApolloProvider 和 MockedProvider 之间的行为存在一些差异,并且在测试中引发了错误。

假设我有以下查询:

query {
  Author {
    id: authorID
    name
  }
}

在 ApolloProvider 中,此查询使用字段别名作为键在 Apollo 缓存中创建条目,缓存中的每个条目Author都有一个id. 因此,Apollo 可以自动合并实体。

使用 MockedProvider 时,情况并非如此。当我模拟以下响应时:

const mockResponse = {
  data: {
    Author: {
      id: 'test!!',
      name: 'test'
    },
  },
}

我收到以下错误:

console.warn
      Cache data may be lost when replacing the Author field of a Query object.
      
      To address this problem (which is not a bug in Apollo Client), define a custom merge function for the Query.Author field, so InMemoryCache can safely merge these objects:
      
        existing: {"authorID":"test!!"...

因此,ApolloProvider 中完全相同的查询使用id(字段别名)作为键,而在 MockedProvider 中它只是添加authorID为另一个字段条目。它忽略字段别名并且没有键。

显然,现在没有任何东西能够合并。我的第一个猜测是,这是因为 MockedProvider 无权访问架构,所以它不知道 authorID 是 ID 类型?还是我走远了?

对我来说真的很奇怪的一件事是 mymockResponse甚至没有提供authorID. 我的 mockResponse 是 { id: "test!!" } 但缓存显示了一个条目{"authorID":"test!!"},因此它本身以某种方式“无别名”。

我真的很难理解这里发生了什么。任何洞察力都将非常有用。

4

0 回答 0