![](/img/trans.png)
[英]I am migrating from apollo-link-state to @apollo/client, how do I write default data to InMemoryCache of @apollo/client without using cache.writeData?
[英]apollo-link-state: How to write Query resolvers?
我使用默认值创建我的状态链接,如下所示:
const stateLink = withClientState({
cache,
resolvers,
defaults: {
quote: {
__typename: 'Quote',
name: '',
phoneNumber: '',
email: '',
items: []
}
}
})
所以我的缓存不应该是空的。 现在我的resolvers
映射如下所示:
resolvers = {
Mutation: { ... },
Query: {
quote: (parent, args, { cache }) => {
const query = gql`query getQuote {
quote @client {
name phoneNumber email items
}
}`
const { quote } = cache.readQuery({ query, variables: {} })
return ({ ...quote })
}
}
}
我的resolvers
的datasource
是缓存吗? 所以我必须以某种方式查询缓存。 但这不起作用,我想这是因为我正在尝试回复quote
查询,为此我正在进行另一个quote
查询。
我想我应该在不查询quote
情况下获取quote
数据,但是如何?
我收到此错误:
Can't find field **quote** on object (ROOT_QUERY) undefined
请帮忙
只是想发布同样的问题 - 幸运的是刚刚想通了。 readQuery-Methode 只允许你从 root 查询。 因此,您应该使用 readFragment,因为它允许您访问缓存中的任何规范化字段,只要您获得它的 id(类似这样的内容:GraphQlTypeName:0 通常由字段:id 和 __typename 构成)。 您的查询解析器应该如下所示:
protected resolvers = {
Query: {
getProdConfig: (parent, args, { cache, getCacheKey }) => {
const id = getCacheKey({ __typename: 'ProdConfig', id: args.id });
const fragment = gql`fragment prodConfig on ProdConfig {
id,
apiKey,
backupUrl,
serverUrl,
cache,
valid
}`;
const data = cache.readFragment({ fragment, id })
return ({ ...data });
}
}
来自阿波罗的电话如下:
let query = this.$apollo.query(`
query prodConfig($id: Int!) {
getProdConfig(id: $id) @client {
apiKey,
backupUrl,
serverUrl,
cache,
valid
}
}`,
{ id: 0 }
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.