Excluding relationships?

First off, this isn’t best topic name, so my apologies ahead of time! Here’s the scenario:

I’m using Ember on the front-end and trying to take advantage of being able to asynchronously load resources when needed. In order to do so, I need to return the relationships that are associated with the resource (not a compound document). Because I need those relationships and their IDs, I essentially have to hit the database and retrieve all of the relationships whether I want to only return the relationship or I want to include the relationship data as part of a compound document.

It appears that by default, the spec suggests that all relationships are returned as part of the response. If you wish to include specific resources as part of a compound document, then you may then include them.

The issue I see is that this can get very inefficient if all relations are loaded by default even if not returned as part of a compound document.

That said, is there a compliant way of excluding specific relationships WITHOUT include-ing them? For example, I have a customer model and in addition to a number of other relationships, it has a transactions relation, which could be thousands of records. I obviously do not want to load that until necessary. How would I load x - 1 relationships?

Has anyone run into a similar issue and if so, how did you solve it?

Thanks!

1 Like

Why do you need the IDs? Why aren’t the relationship URLs enough?

Why wouldn’t you model that something like this:

{“data”:
“type”: “customer”,
“id”: “1234”,
“attributes”: {
“name”: “Jane Doe”
},
“relationships”: {
“transactions”: {
“links”: {
“self”: “/customers/1234/relationships/transactions”,
“related”: “/customers/1234/transactions”,
}
}
}
}

Then you GET customers/1234/transactions later when you need that data.

Because from what I can tell, Ember Data cannot yet take advantage of links.

@jlangley your reply actually helped me quite a bit.

The issue was that none of my relations were being returned with ONLY the links. Therefore, Ember Data was ignoring the links and not using them. Thanks a TON for your help!

1 Like

As @jlangley said, returning only links for relationships that you don’t want to load is the best practice. Beyond that, there’s also the ?fields parameter.