question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Load relationships without going through includes

See original GitHub issue

https://twitter.com/mwpastore/status/969315305863417856

when I do foo.load(‘bar’), why does it hit /api/foo/1?include=bar instead of /api/foo/1/relationships/bar? The latter would allow me to e.g. filter foo’s bar collection. Maybe storefront needs an additional interface?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:18 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
ryantocommented, Apr 3, 2018

I’ve had a few conversations with folks and I think I have some API changes that will solve a number of these cases.

Goal

To provide an API for explicitly loading relationship data. This allows for code that is easier to reason about. Calls to model.get('relationship') will return whatever data is in the store, while calls to model.load('relationship') will fetch data from the backend.

Current problem

load('relationship') uses a JSON:API include query to sideload a models relationship. While this works for most JSON:API backends, it does not work if the API layer wants the client to use relationship.links to load data.

Proposed solution

Add model.sideload('relationship'). This will work exactly how load works today, it will fetch the model and slideload it’s relationship. It will return a cached result if the relationship has already been loaded. Since it is using JSON:API’s include parameter to fetch data it has the added benefit of fetching multiple relationships in one go (example: post.sideload('comments.author,favorites')).

Change model.load('relationship') to use relationship.links. This will allow the client to fetch data from URLs provided by the backend without using the include pattern.

These new APIs will be backwards compatible, if you are using load() today it will continue to work.

Options

Each of these methods will take an options hash as its last parameter. This will allow for force reloading the relationships data.

model.sideload('relationship', { reload: true });
model.load('relationship', { reload: true });

It will also allow developers to implement their own query param logic by overloading the load method on a model-by-model basis. Since sending query param data while loading relationships is not supported by ember-data the user will have to write this logic themselves.

Other

Instead of naming the method sideload, we could call it include.

2reactions
ryantocommented, Sep 13, 2018

Ok, this feature has been released in 0.14.0. 🎉

Two changes to load

  1. It now uses Ember Data to load the relationship, meaning it will respect your payloads relationship links.
  2. It also returns a promise that resolves with the relationship.
Read more comments on GitHub >

github_iconTop Results From Across the Web

laravel eloquent - Use without on nested eager loaded relations
The Eloquent\Model has a newQueryWithoutRelationships . I think you could do the following: (new A())->newQueryWithoutRelationships()->with( ...
Read more >
Eloquent: Relationships - The PHP Framework For Web Artisans
Dynamic properties are "lazy loading", meaning they will only load their relationship data when you actually access them. Because of this, developers often...
Read more >
Relationship Loading Techniques
The loading of relationships falls into three categories; lazy loading, eager loading, and no loading. Lazy loading refers to objects are ...
Read more >
Mental Load: Examples, How to Talk About It, & More
The mental load can be a big burden when one person is left to carry it. ... Emotional labor also shows up in...
Read more >
How to check if an relationship has been eager loaded or not
How can I check if a relationship has been loaded or not? I.e.: Copy Code $user->posts. I used to do: Copy Code if(!isset($user->posts))...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found