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

Getter with polymorphic type #25

Closed
mhluska opened this issue Mar 11, 2019 · 2 comments
Closed

Getter with polymorphic type #25

mhluska opened this issue Mar 11, 2019 · 2 comments
Labels
bug Something isn't working question Further information is requested

Comments

@mhluska
Copy link
Contributor

mhluska commented Mar 11, 2019

I have a need to look up an item by id which has a polymorphic type. For example:

this.$store.getters['jv/get']({ _jv: { type: 'card' } })[this.itemId]

But this will fail because the item types returned by my JSON API are actually credit-card and debit-card. As an ugly hack, I can call this function repeatedly for all the possible types until one resolves with the data.

Would it make sense to have a getter that does not require specifying a type since IDs should be unique across types in the database?

@mrichar1
Copy link
Owner

I think the right way to go about handling records from the store with globally unique IDs is to use the jsonpath functionality provided in the get getter. Bear in mind that not all APIs have globally unique ids, and that ids muct only be unique for each type:

Within a given API, each resource object’s type and id pair MUST identify a single, unique resource.

https://jsonapi.org/format/#document-resource-object-identification

You should be able to specify no type or id, which would normally return the whole store, and then filter by id - so to get all resources with id = 10 you'd do:

this.$store.getters['jv/get']('', '$.*.10')

As it turned out, this wasn't previously possible, since the code path for returning the whole store missed the jsonpath filtering code, but I've fixed this in 0.1.1

@mhluska
Copy link
Contributor Author

mhluska commented Mar 11, 2019

Aah perfect. This explains a lot. Thanks.

@mhluska mhluska closed this as completed Mar 11, 2019
@mrichar1 mrichar1 added bug Something isn't working question Further information is requested labels Mar 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants