我正在用apollo客户端开发一个react native应用程序,我需要有2个屏幕: 屏幕A:显示配置文件列表 屏幕B:显示一些必须应用于屏幕A的过滤器
所以我的想法是使用apollo客户端缓存将过滤器的状态保存在屏幕B中,然后返回到屏幕A并以某种方式应用新的过滤器进行重新提取。
由于每个请求都需要向我的服务器发送几个过滤器,因此我也在考虑使用输入类型,以便我可以以对象的形式而不是逗号列表的形式发送过滤器。分隔的参数。
因此,从apollo客户端查看文档,有一个部分可以管理本地阶段。在其中,我找到了一个名为将@client字段用作变量的小节。基本上,这部分告诉您如何获取缓存中存储的所有过滤器,并将其作为查询的一部分发送。
但是,我总是收到以下错误: 不变违反:缺少为查询字段profileParameters返回的ProfileParameters类型的对象的选择集
一些代码:
这是我初始化缓存存储区的方式,目前,它仅包含page和pageSize,但将包含更多参数。
const cache = new InMemoryCache({
cacheRedirects: {
Query: {
profile: (_, {id}, {getCacheKey}) =>
getCacheKey({__typename: 'Profile', id: id}),
},
},
});
cache.writeData({
data: {
profileParameters: {
__typename: 'ProfileParameters',
page: 0,
pageSize: 25,
},
},
});
const client = new ApolloClient({
uri: 'http://192.168.1.102:3000/graphql',
cache: cache,
resolvers: {},
});
这是查询组件和查询:
const PROFILES_QUERY = gql`
query getFilteredProfiles($type: String, $parameters: ProfileParameters) {
profileParameters @client @export(as: "parameters")
profiles(type: $type, parameters: $parameters) {
id
name
termOfEntry
sport
sportPosition
gpa
avatarUrl
imageUrl
nationality
countryOfResidence
dateOfBirth
annualBudget
career
satScore
toeflScore
graduationDate
}
}
`;
<Query
query={PROFILES_QUERY}
variables={{
type: this.PROFILE_TYPE,
}}>
...
</Query>
在变量对象中传递了一个 type 变量。这来自于类中的局部变量。
此外,屏幕B还不存在,但是缓存正在初始化,我想读取其中的内容,以便将这些过滤器发送到服务器。