Apollo 从缓存中查询对象值
Apollo query object values from cache
我有一个关于从 apollo 中的客户端缓存查询对象的快速问题。
我有一个这样的初始缓存状态:
cache.writeData({
data: { value1: true, value2: { test: "123" } }
});
我将查询数据附加到组件的容器:
import EditModalComponent from "..";
import gql from "graphql-tag";
import { graphql } from "@apollo/react-hoc";
import { compose, pure } from "recompose";
const QUERY2 = gql`
query data {
value2 @client {
test
}
}
`;
const query2 = graphql(QUERY2, {
name: "query2"
});
const EditModalWithData = compose(query2, pure)(EditModalComponent);
export default EditModalWithData;
如果我尝试查询 value1,一切正常,但如果我尝试使用上面的查询查询 value2,我只会收到没有任何值的查询响应
这是我在组件 props 中收到的响应:
variables: {}
refetch: ƒ (variables)
fetchMore: ƒ (fetchMoreOptions)
updateQuery: ƒ (mapFn)
startPolling: ƒ (pollInterval)
stopPolling: ƒ ()
subscribeToMore: ƒ (options)
loading: false
networkStatus: 7
error: undefined
called: true
XXXXXX <- here should be something like: test: "123"
感谢您的帮助!
我找到了解决方案。你需要在 booth 参数后面写 @client:
const QUERY2 = gql`
query data {
value2 @client {
test @client
}
}
`;
我有一个关于从 apollo 中的客户端缓存查询对象的快速问题。
我有一个这样的初始缓存状态:
cache.writeData({
data: { value1: true, value2: { test: "123" } }
});
我将查询数据附加到组件的容器:
import EditModalComponent from "..";
import gql from "graphql-tag";
import { graphql } from "@apollo/react-hoc";
import { compose, pure } from "recompose";
const QUERY2 = gql`
query data {
value2 @client {
test
}
}
`;
const query2 = graphql(QUERY2, {
name: "query2"
});
const EditModalWithData = compose(query2, pure)(EditModalComponent);
export default EditModalWithData;
如果我尝试查询 value1,一切正常,但如果我尝试使用上面的查询查询 value2,我只会收到没有任何值的查询响应
这是我在组件 props 中收到的响应:
variables: {}
refetch: ƒ (variables)
fetchMore: ƒ (fetchMoreOptions)
updateQuery: ƒ (mapFn)
startPolling: ƒ (pollInterval)
stopPolling: ƒ ()
subscribeToMore: ƒ (options)
loading: false
networkStatus: 7
error: undefined
called: true
XXXXXX <- here should be something like: test: "123"
感谢您的帮助!
我找到了解决方案。你需要在 booth 参数后面写 @client:
const QUERY2 = gql`
query data {
value2 @client {
test @client
}
}
`;