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

Accessing related object not in the store #18

Closed
JakeDluhy opened this issue Jul 13, 2017 · 6 comments
Closed

Accessing related object not in the store #18

JakeDluhy opened this issue Jul 13, 2017 · 6 comments

Comments

@JakeDluhy
Copy link
Contributor

If I have users that have many books, and I make a request for a user object without sideloading the related books, then I might have the following structure in my store:

{
    users: {
        '1': {
            id: '1',
            type: 'users',
            attributes: {},
            relationships: {
                books: {
                    data: {
                        [{
                            id: '1',
                            type: 'books'
                        }, {
                            id: '2',
                            type: 'books'
                        }]
                    }
                }
            }
        }
    },
    books: {}
}

If I run const user = build(state.entities, 'users', '1') then I get the user back. However if I then run user.books I get [null, null]. I would expect that I should get

[{
    id: '1',
    type: 'books'
}, {
    id: '2',
    type: 'books'
}]

I have experienced many use cases where I don't need the full related object, just the id (to provide a link to that object, for instance) and so I won't include the related resources.

I'd be happy to make the changes if you would accept a PR!

@yury-dymov
Copy link
Owner

Hey @JakeDluhy,

it is supposed to normalize data first before saving in store. Have a look at https://github.com/yury-dymov/json-api-normalizer

@JakeDluhy
Copy link
Contributor Author

JakeDluhy commented Jul 13, 2017

Right, I'm using the json-api-normalizer. I misrepresented what my store looks like. I suppose it's more like

{
    users: {
        '1': {
            id: '1',
            attributes: {},
            relationships: {
                books: {
                    data: {
                        [{
                            id: '1',
                            type: 'books'
                        }, {
                            id: '2',
                            type: 'books'
                        }]
                    }
                }
            }
        }
    },
    books: {}
}

My point is that if I've just made a request like GET /users/1, not GET /users/1?include=books then I won't have any books loaded into the store. Which means that when I do user.books it's going to return [null, null] (because it's trying to find the related resource and is unable to do so). I think that if it can't find the related resource, it should fall back to providing the relationship object, i.e.

console.log(user.books);
// output
[{
    id: '1',
    type: 'books'
}, {
    id: '2',
    type: 'books'
}]

@JakeDluhy
Copy link
Contributor Author

JakeDluhy commented Jul 13, 2017

Here's the diff of my changes: JakeDluhy@aa76ec0, to help you understand what I'm talking about

@yury-dymov
Copy link
Owner

oh, now I see. Yeah, feel free to submit PR, I am happy to merge it

@JakeDluhy
Copy link
Contributor Author

Thanks! Btw, I'm loving this library (and json-api-normalizer). It's really cleaning up our implementation of JSON API!

@yury-dymov
Copy link
Owner

Thank you for your contribution and kind words :)

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