olosegres/jsona

Incompatible with react-query

Closed this issue · 4 comments

Description

It seems like it causes circular dependency and thus causes incompatibility with react query. Discussion here.

Replication

link

Hi, actually Jsona is designed to use circular relationships.

You can look at original implementation here

setRelationships(model: TJsonaModel, relationships: TJsonaRelationships) {

You have few choices:

  • move deserialization somewhere to hook, after raw data is processed by react-query
  • modify process of properties mapping via extending JsonPropertiesMapper and use some special implementation of setRelationships method or defineRelationGetter (jsona is extanable const dataFormatter = new Jsona({ jsonPropertiesMapper: MyJsonPropertiesMapper });)

@olosegres thank you for the reply. can you provide an example of how to do this?

@olosegres thank you for the reply. can you provide an example of how to do this?

To do what? extend jsona? It is briefly described in the readme https://github.com/olosegres/jsona#customize

But I highly recommend you to use my first suggestion - move deserialization from the interceptor to custom hook. Something like useJsonApiFetch where you will get data from api and convert it to deserialized version.

For future references:

I was able to solve this problem by changing this line model[propName] = relationships[propName] in setRelationships method. changed it as:

model[propName] = structuredClone(relationships[propName])

Make a separate myCustomMapper.ts where everything is same as link, except for the line mentioned above.

Then just use the custom mapper as:

import { JsonPropertiesMapper } from 'utils/myCustomMapper

const dataFormatter = new Jsona({
  jsonPropertiesMapper: new JsonPropertiesMapper()
})