Skip to content

Instantly share code, notes, and snippets.

@willviles
Last active June 3, 2019 09:16
Show Gist options
  • Select an option

  • Save willviles/d4cc3fe6b3c8fccb99d9aa0374d75740 to your computer and use it in GitHub Desktop.

Select an option

Save willviles/d4cc3fe6b3c8fccb99d9aa0374d75740 to your computer and use it in GitHub Desktop.
JSON:API relationships vs nested attributes. Only return records as relationships to the frontend if we need to do CRUD operations on them, otherwise we'll have too many unnecessary models in memory!
{
"data": [{
"type": "properties",
"id": "1",
"attributes": {
"geolocation": "xx,xx",
"propertyInfos": [{
"id": "123",
"value": "Shitface bastards",
"locale": "en-gb",
"info": {
"id": "description-of-owners",
"name": "Description of Owners",
"localization": "Description of Owners"
}
}]
}
}]
}
{
"data": [{
"type": "properties",
"id": "1",
"attributes": {
"geolocation": "xx,xx"
},
"relationships": {
"propertyInfo": {
"data": [{"id": "123", "type": "propertyInfos"}]
}
}
}],
"included": [
{
"type": "propertyInfos",
"id": "123",
"attributes": {
"value": "Shitface bastards",
"locale": "en-gb"
},
"relationships": {
"property": {
"data": {"id": "1", "type": "properties"}
},
"info": {
"data": {"id": "description-of-owners", "type": "infos"}
}
}
},
{
"type": "infos",
"id": "description-of-owners",
"attributes": {
"name": "Description of Owners",
"localization": "Description of Owners"
}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment