Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Relationship self Link #42

Merged
merged 5 commits into from Feb 12, 2017
Merged

Add Relationship self Link #42

merged 5 commits into from Feb 12, 2017

Conversation

rgant
Copy link
Contributor

@rgant rgant commented Feb 7, 2017

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
Copy link
Contributor Author

rgant commented Feb 7, 2017

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
Copy link
Owner

@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
Copy link
Contributor Author

rgant commented Feb 12, 2017

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
Copy link
Owner

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
Copy link
Contributor Author

rgant commented Feb 12, 2017

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

@rgant rgant deleted the relationship-self-link branch February 17, 2017 19:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants