RestPack / restpack_serializer

Model serialization, paging, side-loading and filtering
MIT License
175 stars 43 forks source link

Represent relationships in "links" format #11

Closed GavinJoyce closed 11 years ago

GavinJoyce commented 11 years ago

As per http://jsonapi.org/, represent relationships in links format:

{
  "links": {
    "posts.author": {
      "href": "http://example.com/people/{posts.author}",
      "type": "people"
    },
    "posts.comments": {
      "href": "http://example.com/comments/{posts.comments}",
      "type": "comments"
    }
  }
  "posts": [{
    "id": "1",
    "title": "Rails is Omakase",
    "links": {
      "author": 9,
      "comments": [ 1, 2, 3 ]
   }, {
    "id": "2",
    "title": "The Parley Letter",
    "links": {
      "author": 9,
      "comments": [ 4, 5 ]
   }, {
    "id": "1",
    "title": "Dependency Injection is Not a Virtue",
    "links": {
      "author": 9,
      "comments": [ 6 ]
    }
  }],
  "people": [{
    "id": "9",
    "name": "@d2h"
  }],
  "comments": [{
    "id": "1",
    "body": "Mmmmmakase"
  }, {
    "id": "2",
    "body": "I prefer unagi"
  }, {
    "id": "3",
    "body": "What's Omakase?"
  }, {
    "id": "4",
    "body": "Parley is a discussion, especially one between enemies"
  }, {
    "id": "5",
    "body": "The parsley letter"
  }, {
    "id": "6",
    "body": "Dependency Injection is Not a Vice"
  }]
}
GavinJoyce commented 11 years ago

This is what we currently render:

http://restpack-serializer-sample.herokuapp.com/albums.json?includes=artists

{
    "albums": [
        {
            "id": 1,
            "title": "Kid A",
            "year": 2000,
            "artist_id": 1,
            "href": "/albums/1.json"
        },
        {
            "id": 2,
            "title": "Amnesiac",
            "year": 2001,
            "artist_id": 1,
            "href": "/albums/2.json"
        },
        {
            "id": 3,
            "title": "Murder Ballads",
            "year": 1996,
            "artist_id": 2,
            "href": "/albums/3.json"
        },
        {
            "id": 4,
            "title": "Curtains",
            "year": 2005,
            "artist_id": 3,
            "href": "/albums/4.json"
        }
    ],
    "meta": {
        "albums": {
            "page": 1,
            "page_size": 10,
            "count": 4,
            "includes": [
                "artists"
            ],
            "page_count": 1,
            "previous_page": null,
            "next_page": null
        }
    },
    "artists": [
        {
            "id": 1,
            "name": "Radiohead",
            "website": "http://radiohead.com/",
            "href": "/artists/1.json"
        },
        {
            "id": 2,
            "name": "Nick Cave & The Bad Seeds",
            "website": "http://www.nickcave.com/",
            "href": "/artists/2.json"
        },
        {
            "id": 3,
            "name": "John Frusciante",
            "website": "http://johnfrusciante.com/",
            "href": "/artists/3.json"
        }
    ]
}
GavinJoyce commented 11 years ago

This is what we would like to render:

http://restpack-serializer-sample.herokuapp.com/albums.json?includes=artists

{
    "links": {
        "albums.artist": {
            "href": "/artists/{albums.artist}.json",
            "type": "artists"
        },
        "albums.songs": {
            "href": "/songs.json?album_id={id}",
            "type": "songs"
        },
        "artists.albums": {
            "href": "/albums.json?artist_id={id}",
            "type": "albums"
        },
        "artists.songs": {
            "href": "/songs.json?artist_id={id}",
            "type": "songs"
        }
    },
    "albums": [
        {
            "id": 1,
            "title": "Kid A",
            "year": 2000,
            "links": {
                "artist": 1
            },
            "href": "/albums/1.json"
        },
        {
            "id": 2,
            "title": "Amnesiac",
            "year": 2001,
            "links": {
                "artist": 1
            },
            "href": "/albums/2.json"
        },
        {
            "id": 3,
            "title": "Murder Ballads",
            "year": 1996,
            "links": {
                "artist": 2
            },
            "href": "/albums/3.json"
        },
        {
            "id": 4,
            "title": "Curtains",
            "year": 2005,
            "links": {
                "artist": 3
            },
            "href": "/albums/4.json"
        }
    ],
    "meta": {
        "albums": {
            "page": 1,
            "page_size": 10,
            "count": 4,
            "includes": [
                "artists"
            ],
            "page_count": 1,
            "previous_page": null,
            "next_page": null
        }
    },
    "artists": [
        {
            "id": 1,
            "name": "Radiohead",
            "website": "http://radiohead.com/",
            "href": "/artists/1.json"
        },
        {
            "id": 2,
            "name": "Nick Cave & The Bad Seeds",
            "website": "http://www.nickcave.com/",
            "href": "/artists/2.json"
        },
        {
            "id": 3,
            "name": "John Frusciante",
            "website": "http://johnfrusciante.com/",
            "href": "/artists/3.json"
        }
    ]
}