【问题标题】:LocalState initialisation issue: Migrating from @apollo/client (3..0.0-beta.29) to @apollo/client (3.0.2) cache not initialising data as expectedLocalState 初始化问题:从 @apollo/client (3..0.0-beta.29) 迁移到 @apollo/client (3.0.2) 缓存未按预期初始化数据
【发布时间】:2020-07-23 22:54:11
【问题描述】:

因此,对于 beta 版本,我将按如下方式指定我的缓存:

withData.js

  const cache = new InMemoryCache().restore(initialState || {});

  const data = {
    cartOpen: false,
    items: [
      {
        id: "ck0e27onl00av071035uzliqt",
        color: {
          id: "cjrumlijl003708947wpq15kp",
          label: "Military Green",
          name: "militarygreen",
          __typename: "Color"
        },
        description: "Push Up Low Waist Padded Swimwear.",
        mainDescription: "Bikinis Women Swimsuit 2017 Summer Beach Wear Bikini Set Push Up Swimwear Bandage Bathing Suit.",
        image: "/static/white-black-01.jpg",
        largeImage: "/static/white-black-01.jpg",
        itemvariants: [{
          id: "1234",
          __typename: 'ItemVariants',
        }],
        price: 3500,
        quantity: 0,
        size: {
          id: "cjrumlijl003708947wpq15kp",
          label: "Medium",
          name: "M",
          __typename: "Size"
        },
        title: "Bandage Black & White Swimsuit.",
        user: {
          id: "cjo7q048b000408312h9c2fo0",
          __typename: "User"
        },
        __typename: "Item"
      }
    ]
  };

  cache.writeData({ data });

  client_.onResetStore(() => cache.writeData({ data }));

然后调用localState如下:

items.js

const LOCAL_STATE_QUERY = gql`
  query {
    items @client {
      id
      color {
        id
        label
        name
      }
      description
      mainDescription
      image
      largeImage
      itemvariants {
        id
      }
      price
      quantity
      size {
        id
        label
        name
      }
      title
      user {
        id
      }
    }
  }
`;

  // LocalState Query
  const { data: cachedItem, error: errorQuery, loading: loadingQuery } = useQuery(
    LOCAL_STATE_QUERY,
  );

但既然我变了:

  cache.writeData({ data });
  client_.onResetStore(() => cache.writeData({ data }));

  cache.modify({ data });
  client_.onResetStore(async () => cache.modify({ data }));

localState 初始化未按预期工作。我在这里俯瞰什么?

【问题讨论】:

    标签: apollo apollo-client


    【解决方案1】:

    通过执行以下操作解决了这个问题:

    cache.writeQuery({ query: LOCAL_STATE_QUERY, data })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-18
      • 2011-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-12
      • 2015-01-23
      • 2021-04-18
      相关资源
      最近更新 更多