来自缓存的Apollo查询对象值

时间:2020-03-06 10:52:21

标签: javascript graphql apollo react-apollo apollo-client

我有一个快速的问题,关于从阿波罗中的客户端缓存中查询对象。

我的初始缓存状态如下:

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,我只会收到没有任何值的查询响应

这是我在组件道具中收到的回复:

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"

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。您需要在展位参数后面输入 @client

const QUERY2 = gql`
  query data {
    value2 @client {
      test @client
    }
  }
`;