BlairAllegroTech / js-data-jsonapi

JsonApi Adapter for js-data
MIT License
15 stars 5 forks source link

Add Relationship self Link #42

Closed rgant closed 7 years ago

rgant commented 7 years ago

This patch will add self links to the relationships in the metadata. It also checks that the URLs are defined in the JSONAPI payload before adding to the metadata.

This will result in things like:

[{
    "title": "JSON API paints my bikeshed!",
    "id": "1",
    "$_JSONAPIMETA_": {
        "selfType": "article",
        "selfLink": "http://example.com/article/1",
        "isJsonApiReference": false,
        "relationships": {
            "authors": {
                "related": {
                    "type": "article_author",
                    "url": "http://example.com/article/1/authors"
                },
                "self": {
                    "type": "article_author",
                    "url": "http://example.com/article/1/relationships/authors"
                }
            }
        },
        "links": {
            "self": {
                "type": "self",
                "url": "http://example.com/article/1",
                "meta": {}
            }
        },
        "referenceCount": 3
    }
}]
rgant commented 7 years ago

Curses, I got the comments mixed up.

"authors relationship should have plural URLs." belongs to 365480e

"Update for tests. relationship links shouldn't exist if the jsonapi payload doesn't have a link." belongs to cb96719

BlairAllegroTech commented 7 years ago

@rgant Could you please just add a bit of a description here of the problem/use case that this solves?

I assume that what was missing was a way of knowing where the actual resource/object came from on the Json-Api side. this is useful so that we can post back to it etc... without having to assume anything about where it came from. Therefore GETS POSTS, PATCHS and DELETES, can be directed there.

Thanks for your help, appreciated as always!!

rgant commented 7 years ago

JSONAPI supports managing the relationships between objects directly: http://jsonapi.org/format/#crud-updating-relationships

This exposes that URL to the JavaScript code so we can use the URL for making updates:

var relationship = vm.item.$_JSONAPIMETA_.relationships.services.self;

There doesn't seem to be an obvious way to setup a "relationship" resource with js-data and js-data-jsonapi so I am still working on an optimal solution for that. But I know I will need the self relation URL to accomplish it.

BlairAllegroTech commented 7 years ago

Ok, so the effect of updating, adding or removing a foreign key value in js-data, for example should result in a POST, PATCH or DELETE to the relationship's self link would be the end goal

rgant commented 7 years ago

Sometimes yes. It depends on if we are just modifying the list, or adding new resources.