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

Access to relationships.*.data.id #400

Open
ef4 opened this issue Mar 7, 2022 · 3 comments
Open

Access to relationships.*.data.id #400

ef4 opened this issue Mar 7, 2022 · 3 comments

Comments

@ef4
Copy link
Contributor

ef4 commented Mar 7, 2022

Given a server that includes data references for unloaded relationships:

{
  relationships: {
    organization: {
      data: { type: "organizations", id: "2" },
      links: { related: "https://example.com/org/2" }
    }
  }
}

Is there public API for accessing the data.type and data.id from an ember-orbit model?

I was able to work around by creating a getter on my model:

get organizationId() {
  let { type, id } = this.$getData().relationships.organization.data;
  return this.$store.keyMap.idToKey(type, 'remoteId', id);
}
@dgeb
Copy link
Member

dgeb commented Mar 8, 2022

If you have a key defined on your Organization model (@key() remoteId;) and relationship data is included with the primary model's data, then I believe that a minimalist record (with just type, id, and remoteId) of type organization should exist in your cache after processing that payload. If that's true, then you should be able to access it through your model via this.organization.remoteId.

I may be missing some context here. Is this.organization not present? Was it explicitly deleted? Do you have any inverse relationships present?

@ef4
Copy link
Contributor Author

ef4 commented Mar 8, 2022

Oh, interesting! I was just assuming this.organization would be undefined. I will try that.

ef4 added a commit to ef4/ember-orbit that referenced this issue Mar 14, 2022
In orbitjs#400 @dgeb suggested that even unloaded records should have a type, id, and (if configured) remoteId key when accessed via a relationship that loaded with a `data` property.

These tests demonstrate that that works when the related record is already loaded, but fails when it is not.
@ef4
Copy link
Contributor Author

ef4 commented Mar 14, 2022

I got a chance to try this, and it's unreliable.

It works if the related record happened to already load via other means. But if the record is not loaded, remoteId is not present. Only type and id are present.

#403 has a passing test to demonstrate the loaded case and a failing test for the unloaded case.

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

2 participants