beauby / jsonapi-datastore

JavaScript client-side JSON API data handling made easy.
MIT License
195 stars 40 forks source link

Circular references #27

Closed andyjbas closed 8 years ago

andyjbas commented 8 years ago

Any thoughts on supporting the stringification of circular references?

By that I mean:

const a = {id: 'a'}
const b = {id: 'b'}

let graph = {};
graph['a'] = a;
graph['b'] = b;

graph['a'].b = b;
graph['b'].a = a;

JSON.stringify(graph.a) // blow up

In my case, I was receiving the following two payloads:

{
   "data":[
      {
         "type":"updates",
         "id":"3",
         "attributes":{},
         "relationships":{
            "project":{
               "data":{
                  "type":"projects",
                  "id":"1"
               }
            }
         }
      }
   ]
}

{
   "data":[
      {
         "type":"projects",
         "id":"1",
         "attributes":{},
         "relationships":{
            "updates":{
               "data":[
                  {
                     "type":"updates",
                     "id":"3"
                  },
               ]
            }
         }
      }
   ],
   "included":[
      {
         "type":"updates",
         "id":"3",
         "attributes":{}
      }
   ]
}

... and with angular you'll get json stringification errors.

I didn't spot any obviously easy ways to re-arrange how models reference relationships without sacrificing the functionality of sync, and so I opted to use smart-circular. I haven't done any performance benchmarks, but otherwise this solves my issue without any other downsides (assuming someone isn't actually trying to reach down below the first level of a relationship).

beauby commented 8 years ago

Hi @andyjbas – could you be more specific? This issue does not seem related to this library.

beauby commented 8 years ago

Closing this as it does not seem to be related to jsonapi-datastore. Feel free to reopen if needed.