reyesoft / ngx-jsonapi

JSON API client library for Angular 5+ 👌 :: Production Ready 🚀
https://ngx-jsonapi.reyesoft.com/
MIT License
101 stars 52 forks source link

Relationships only included when corresponding service is constructed #338

Open TimmayNL opened 2 years ago

TimmayNL commented 2 years ago

When I use a Service to get a Resource and use include to also fetch data for relationships, I need to construct the Service for the included Resource(s) for it to work.

Taking the demo as example:

   public constructor(private booksService: BooksService) {
        booksService
            .all({
                include: ['author']
            }).subscribe(books => {
            console.log(books);
        })
    }

will return a DocumentCollection with all books, where every Book has an author relationship. But when you look at the data in this author Resource, it is empty. It does have the correct id, but attributes is a completly empty object.

Only when I also inject the AuthorsService, the attributes for the included author are loaded.

   public constructor(private booksService: BooksService, private authorService: AuthorsService) {
        booksService
            .all({
                include: ['author']
            }).subscribe(books => {
            console.log(books);
        })
    }

I am not using the AuthorService anywhere, just injecting it and thus constructing it is enough.

arrgson commented 1 year ago

I have a similiar issue ... included resource's attributes are sometimes available and sometimes missing. (If I refresh the page 10 times about 2-3 times the attributes are missing. The ID and the type is there, but the attribues are empty.)