[英]How to retrieve value from Apollo graphql client state
Looking at Apollo link state I can see how to write/update a value to local store/cache by using a mutation or directly by using cache.writeData({ data })
. 查看Apollo链接状态,我可以看到如何通过使用突变或直接使用
cache.writeData({ data })
将值写入/更新到本地存储/缓存中。
But how do i retrieve that cached value directly to use as a variable for a mutation? 但是,我如何直接检索该缓存的值以用作突变的变量? Or do I need chain 2 queries using the @client on first one?
还是我需要在第一个查询中使用@client进行链2查询?
Note: if I use the ApolloProvider I get access to cache but it looks like this: {cache.cache.data.$ROOT.data.myVariable}
注意:如果我使用ApolloProvider,则可以访问缓存,但它看起来像这样:
{cache.cache.data.$ROOT.data.myVariable}
You could nest the Mutation component within a Query component. 您可以将Mutation组件嵌套在Query组件中。 Like below.
像下面。 If you do it a lot then you could create your own wrapper component.
如果您经常这样做,则可以创建自己的包装器组件。
Or you could do a client.readQuery
as seen in the docs here: https://www.apollographql.com/docs/react/advanced/caching.html#readquery . 或者,您可以执行一个
client.readQuery
,如此处的文档所示: https : client.readQuery
。
const ADD_TODO = gql`
mutation addTodo($type: String!) {
addTodo(type: $type) {
id
type
}
}
`;
<Query query={gql`
{
myVariable @client
}
`>
{({ data: { myVariable }}) => {
return (
<Mutation mutation={ADD_TODO}>
{(addTodo, { data }) => (
<div>
<form
onSubmit={e => {
e.preventDefault();
addTodo({ variables: { type: myVariable } });
input.value = "";
}}
>
<input
ref={node => {
input = node;
}}
/>
<button type="submit">Add Todo</button>
</form>
</div>
)}
</Mutation>
</Query>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.