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

JSON:API Inclusion of related resources #3454

Closed
Finhas opened this issue Jun 26, 2015 · 6 comments
Closed

JSON:API Inclusion of related resources #3454

Finhas opened this issue Jun 26, 2015 · 6 comments

Comments

@Finhas
Copy link

Finhas commented Jun 26, 2015

We have an api built following the JSON::API 1.0 specs; all the data seems to be loading correctly through ember-data and the JSONAPI Serilaizer and Adaptor (polymorphic, async) except that related resources are sending unique requests to our api. It seems as though ember-data is expecting related data to always be returned instead of having it as a api/user option.

An endpoint MAY return resources related to the primary data by default.

http://jsonapi.org/format/#fetching-includes

Basically, can ember-data handle requesting and loading the included data:
http://localhost:3000/api/apps/:id/responsibilities**?include=party**

Instead of requesting:
localhost:3000/api/apps/:id/responsibilities
and
[localhost:3000/api/party/:ids_from_responsibilities]

Thanks

@wecc
Copy link
Contributor

wecc commented Jun 26, 2015

Does you API return both data and links { related } for your relationships?

Currently links have precedence over data so if both are present data will be fetched by links instead of local data. We're considering this a bug and it's tracked in #3380.

@Finhas
Copy link
Author

Finhas commented Jun 26, 2015

I was reading that issue and I believe this is related but not the same. It seems at though @wwwdata api is returning the included information from his primary data by default. The issue he is having is true for us too; data is being retrieved by links instead of local data but that isn't our current issue.

According to JSON:API specs, using 'included' to return information from primary data is optional. Basically, 'http://example.com/posts/1' could return this (ember-data uses the links to retrieve the relationships which is expected behavior):

{
    "data": [
        {
            "type": "posts",
            "id": "1",
            "attributes": {
                "title": "JSON API paints my bikeshed!"
            },
            "relationships": {
                "author": {
                    "links": {
                        "self": "http://example.com/posts/1/relationships/author",
                        "related": "http://example.com/posts/1/author"
                    },
                    "data": {
                        "type": "people",
                        "id": "9"
                    }
                }
            },
            "links": {
                "self": "http://example.com/posts/1"
            }
        }
    ]
}

But what if I wanted ember-data to do this instead http://example.com/posts/1?included=author which would return (and lead us to #3380):

{
    "data": [
        {
            "type": "posts",
            "id": "1",
            "attributes": {
                "title": "JSON API paints my bikeshed!"
            },
            "relationships": {
                "author": {
                    "links": {
                        "self": "http://example.com/posts/1/relationships/author",
                        "related": "http://example.com/posts/1/author"
                    },
                    "data": {
                        "type": "people",
                        "id": "9"
                    }
                }
            },
            "links": {
                "self": "http://example.com/posts/1"
            }
        }
    ],
    "included": [
        {
            "type": "people",
            "id": "9",
            "attributes": {
                "first-name": "Dan",
                "last-name": "Gebhardt",
                "twitter": "dgeb"
            },
            "links": {
                "self": "http://example.com/people/9"
            }
        }
    ]
}

@wwwdata
Copy link

wwwdata commented Jun 27, 2015

Hi,

I think you are exactly describing the same problem that I have. My backend does not send the included object per default. I use the query parameter that you described and add it via store.query('post', {include: 'comments'});

But it doesn't matter if you have a query parameter or not, ember-data does not handle both scenarios correctly.

@Finhas
Copy link
Author

Finhas commented Jun 29, 2015

Ok, so it looks like this has already been a point of discussion #1576 but it doesn't seem like it has been solved yet #3281.

Is the plan to have this work:
return this.store.findRecord('post', params.post_id, { adapterOptions: { query: { 'includes': 'comments' } } });

In the meantime, I am trying to implement findOneQuery but am having some issues with JSONAPISerializer.extract

@wecc
Copy link
Contributor

wecc commented Jun 29, 2015

@Finhas

In the meantime, I am trying to implement findOneQuery but am having some issues with JSONAPISerializer.extract

The JSONAPISerializer only supports the new Serializer API so if you use normalizeResponse instead of extract it should work out.

@Finhas
Copy link
Author

Finhas commented Jun 29, 2015

Thanks @wecc

Also had to change it to findRecord and remove the typeClass from the push since it's deprecated.

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

No branches or pull requests

3 participants