React-apollo: Immutable JS with react-apollo and apollo-client V2

Created on 5 Jan 2018  路  1Comment  路  Source: apollographql/react-apollo

Hi, there is a related issue on Apollo-client repository, which we ask for help regarding using an Immutable structure with apollo-client.

I created my custom ImmutableCache as following:

import { Map, fromJS } from 'immutable';

export class ImmutableCache {
  constructor(data = Map()) {
    if (!(data instanceof Map)) {
      this.data = fromJS(data);
    } else {
      this.data = data;
    }
  }

  toObject() {
    return this.data.toJS();
  }

  get(dataId: string) {
    return this.data.get(dataId);
  }

  set(dataId, value) {
    this.data = this.data.set(dataId, fromJS(value));
  }

  delete(dataId): void {
    this.data = this.data.set(dataId, undefined);
  }

  clear(): void {
    this.data = Map();
  }

  replace(newData) {
    this.data = newData || Map();
  }
}

export function immutableCache(seed) {
  return new ImmutableCache(seed);
}

Then use it like that:

const cache = new InMemoryCache({
    storeFactory: immutableCache,
});

However, the props are not populated in the component with the graphql response.
Could you please advice on how can i achieve this with react-apollo?

Thanks

>All comments

Please ask questions on the slack channel or on stack overflow.

Was this page helpful?
0 / 5 - 0 ratings