aerogear / offix

GraphQL Offline Client and Server
https://offix.dev
Apache License 2.0
758 stars 45 forks source link

createOptimisticResponse CacheOperation.ADD overwrites id prop #430

Closed MitchellMonaghan closed 4 years ago

MitchellMonaghan commented 4 years ago

createOptimisticResponse with CacheOperation.ADD creates its own id using generateClientId. If they id prop already has a value can we not blow the existing value away? I already generated a id for this new element and I want to keep it in the format I provided. I know I can work around this using the default apollo update function, this would be a nice thing to have.

Thanks Mitch

wtrocki commented 4 years ago

Really great idea and quite an easy fix. If the operation has already id present we can skip a generation.

https://github.com/aerogear/offix/blob/master/packages/offix-cache/src/createOptimisticResponse.ts#L66

Could be if (operationType === CacheOperation.ADD && !optimisticResponse[operation][idField])

We will release it in the patch release. As a current workaround, you can simply copy this method with modification on every mutation

wtrocki commented 4 years ago

CC @darahayes

wtrocki commented 4 years ago

CC @darahayes

darahayes commented 4 years ago

@MitchellMonaghan thanks for reporting this and apologies it took such a long time for us to respond. This is absolutely a bug and it can be fixed with what @wtrocki is suggesting. I'll let you know as soon as a fix is released. In the meantime, if you're really stuck, you can directly copy the createOptimisticResponse function that @wtrocki linked into your application and make the modification you need until we fix it. Or better yet, if you can do that and then make a pull request back to the project we'd be delighted and we would make sure it gets merged as soon as possible.

darahayes commented 4 years ago

@MitchellMonaghan we've just published 0.15.1 of all the offix packages. Can you try that new release and let me know if it fixes the issue for you? Thanks.