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

Nested include breaks CacheStore#setCollection #128

Closed xaun closed 5 years ago

xaun commented 6 years ago

Is the nested include supported?

.all({ include: 'comments.author' })

On version 1.1.1 when trying to query with the above it breaks with stack trace:

marketplace.component.ts:27 TypeError: Cannot read property 'data' of undefined
    at eval (ngx-jsonapi.es5.js:1956)
    at Array.forEach (<anonymous>)
    at eval (ngx-jsonapi.es5.js:1955)
    at eval (ngx-jsonapi.es5.js:351)
    at Array.forEach (<anonymous>)
    at Function.Base.forEach (ngx-jsonapi.es5.js:350)
    at CacheStore.setCollection (ngx-jsonapi.es5.js:1952)
    at eval (ngx-jsonapi.es5.js:2355)
    at ZoneDelegate.invoke (zone.js:388)
    at Object.onInvoke (core.js:4760)

The resource_type_alias is "comments.author", so resource.relationships[resource_type_alias].data can never work.

jonasgrundtvig commented 6 years ago

The exact same problem occurs in version 2.0.0.-rc.10.

maxi7587 commented 6 years ago

@pablorsk Do you want me to take a look?

pablorsk commented 6 years ago

Yeah, you're right. (https://jsonapi.org/format/#fetching-includes)

http://jsonapiplayground.reyesoft.com/ dont support yet compound includes, I can't test.

stefandb commented 5 years ago

I made a PR for this issue to fix this. https://github.com/reyesoft/ngx-jsonapi/pull/140

pablorsk commented 5 years ago

PR #140 and PR #169 try to fix this problem. @maxi7587 take this issue on FE-4 (internal issue code of @reyesoft/reyesoft-team)

maxi7587 commented 5 years ago

I'm on it!

xaun commented 5 years ago

@maxi7587 @pablorsk Has this been done? I can see #175 was merged, but I'm trying to test now on v2.0.2 and while it's not throwing errors, the nested include relationship resource has an empty id and empty attributes (in the network request, the relationships are included).

Something else I have noticed is that the while the DocumentCollection response of my all request is_loaded is true, is_loaded value for all nested relationships is false.

In the screenshot below you can see the nested relationships which lead to the empty "fields" resource.

Screen Shot 2019-05-14 at 13 11 00

My services are registered and injected (no register warnings).

My models are defined correctly I believe

export class Product extends Resource {
  public relationships = {
    values: new DocumentCollection<ProductValue>()
  }
}

export class ProductValue extends Resource {
  public relationships = {
    field: new DocumentResource<ProductValueField>()
  }
}

export class ProductValueField extends Resource {
  attributes: {
    created_at: string
    description: string
    field_type: string
    max_length: number | null
    min_length: number | null
    multiple: boolean
    name: string
    nid: string
    position: number
    required: boolean
    section: string
    updated_at: string
    visible: boolean
  }
}

@Injectable({
  providedIn: 'root'
})
@Autoregister()
export class ProductService extends Service<Product> {
  public resource = Product
  public type = 'products'

  constructor(
    private productValueService: ProductValueService,
    private productValueFieldService: ProductValueFieldService) {
    super()

    this.all({ include: ['values.field'] }).pipe(filter(res => !res.is_loaded)).subscribe(res => {
      console.log(res) // this is the screenshotted output
    })
  }
}

@Injectable({
  providedIn: 'root'
})
@Autoregister()
export class ProductValueService extends Service<ProductValue> {
  public resource = ProductValue
  public type = 'values'

  constructor() {
    super()
  }
}

@Injectable({
  providedIn: 'root'
})
@Autoregister()
export class ProductValueFieldService extends Service<ProductValueField> {
  public resource = ProductValueField
  public type = 'fields'

  constructor() {
    super()
  }
}

Am I missing something? Thanks

karwank commented 5 years ago

@xaun please look what I did in #180, maybe it will help you.

pablorsk commented 5 years ago

Solved on v2.1.9

noahott commented 4 years ago

I'm having issues with this still in v2.1.10. Attributes in nest relationships are not always loaded:

Screen Shot 2019-12-30 at 2 49 21 PM Screen Shot 2019-12-30 at 2 51 33 PM