Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LocalStorageWrapper broken on 0.10.0 #426

Closed
blaenk opened this issue Jul 10, 2021 · 7 comments · Fixed by #436
Closed

LocalStorageWrapper broken on 0.10.0 #426

blaenk opened this issue Jul 10, 2021 · 7 comments · Fixed by #436

Comments

@blaenk
Copy link

blaenk commented Jul 10, 2021

Using the same code from the example:

const cache = new InMemoryCache();
let newPersistor = new CachePersistor({
cache,
storage: new LocalStorageWrapper(window.localStorage),
debug: true,
trigger: 'write',
});
await newPersistor.restore();

And from the readme, upgrading to 0.10.0 without any code changes breaks the use of LocalStorageWrapper:

TS2322: Type 'LocalStorageWrapper' is not assignable to type 'PersistentStorage<PersistedData<NormalizedCacheObject>>'.
Types of property 'setItem' are incompatible.
Type '(key: string, value: string) => void | Promise<void>' is not assignable to type '(key: string, value: PersistedData<NormalizedCacheObject>) => void | Promise<void> | PersistedData<NormalizedCacheObject> | Promise<...>'.
Types of parameters 'value' and 'value' are incompatible.
Type 'PersistedData<NormalizedCacheObject>' is not assignable to type 'string'.
Type 'null' is not assignable to type 'string'.

The types don't seem to line up. I think the problem is that the type parameter for CachePersistor is in this case NormalizedCacheObject so the options are ApolloPersistOptions<NormalizedCacheObject> which would then expect storage: PersistentStorage<PersistedData<NormalizedCacheObject>>, whereas LocalStorageWrapper would provide PersistentStorage<string>.

By the way, it might be helpful to build the examples as part of CI to serve as a canary for when these things happen.

@wtrocki
Copy link
Collaborator

wtrocki commented Jul 10, 2021

On holidays for 2 weeks with limited access to workstation. Would using any for interface work as workaround until we fix this?
Good hint to build examples. Thank you

@blaenk
Copy link
Author

blaenk commented Jul 10, 2021

No rush at all! I simply stuck with the previous version for now, just thought I'd make you all aware of this.

Thank you and enjoy your holiday!

@Micloroc
Copy link

Just to add some info, same happens for IonicStorageWrapper. I think all wrappers are broken in 0.10.0.

Anyway, awesome library, thanks for your work and enjoy holidays!

@blaenk
Copy link
Author

blaenk commented Jul 11, 2021

cc @PMCorbett regarding what I think may be the relevant commit 8a15634 via #420

No pressure, just adding information.

@wtrocki
Copy link
Collaborator

wtrocki commented Jul 23, 2021

I could not figure out how to fix it apart from adding any as option to every storage abstraction which defeats the purpose of having generics at all.

@maximeg
Copy link

maximeg commented Aug 1, 2021

Same here since 0.10, 0.11 didn't solve it:

node_modules/apollo3-cache-persist/lib/storageWrappers/LocalForageWrapper.d.ts:7:5 - error TS2416: Property 'setItem' in type 'LocalForageWrapper' is not assignable to the same property in base type 'PersistentStorage<string | object>'.
  Type '(key: string, value: string) => void | Promise<void>' is not assignable to type '(key: string, value: string | object) => string | void | object | Promise<void> | Promise<string | object>'.
    Types of parameters 'value' and 'value' are incompatible.
      Type 'string | object' is not assignable to type 'string'.
        Type 'object' is not assignable to type 'string'.

7     setItem(key: string, value: string): void | Promise<void>;
      ~~~~~~~

src/graphql/client.ts:79:5 - error TS2322: Type 'LocalStorageWrapper' is not assignable to type 'PersistentStorage<PersistedData<NormalizedCacheObject>>'.
  Types of property 'setItem' are incompatible.
    Type '(key: string, value: string) => void | Promise<void>' is not assignable to type '(key: string, value: PersistedData<NormalizedCacheObject>) => void | Promise<void> | PersistedData<NormalizedCacheObject> | Promise<...>'.
      Types of parameters 'value' and 'value' are incompatible.
        Type 'PersistedData<NormalizedCacheObject>' is not assignable to type 'string'.
          Type 'null' is not assignable to type 'string'.

79     storage,
       ~~~~~~~

  node_modules/apollo3-cache-persist/lib/types/index.d.ts:15:5
    15     storage: PersistentStorage<PersistedData<TSerialized>>;
           ~~~~~~~
    The expected type comes from property 'storage' which is declared here on type 'ApolloPersistOptions<NormalizedCacheObject>'

@wtrocki
Copy link
Collaborator

wtrocki commented Aug 1, 2021

We need to add | any to every storage interface we have. Volunteers?

wodCZ added a commit to wodCZ/apollo-cache-persist that referenced this issue Aug 1, 2021
wodCZ added a commit to wodCZ/apollo-cache-persist that referenced this issue Aug 1, 2021
wodCZ added a commit to wodCZ/apollo-cache-persist that referenced this issue Aug 2, 2021
fix apollographql#426, fix apollographql#431 by adding support for new synchronous removeItem
wodCZ added a commit to wodCZ/apollo-cache-persist that referenced this issue Aug 2, 2021
fix apollographql#426, fix apollographql#431 by adding support for new synchronous removeItem
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants