Skip to content
This repository has been archived by the owner on Oct 7, 2021. It is now read-only.

Fetching from a link isn't working (in Angular?) #22

Open
Smolli opened this issue Nov 24, 2017 · 7 comments
Open

Fetching from a link isn't working (in Angular?) #22

Smolli opened this issue Nov 24, 2017 · 7 comments

Comments

@Smolli
Copy link

Smolli commented Nov 24, 2017

There is this setup:

export class CustomerResource extends HalResource {
    @HalProperty()
    id: number
    @HalProperty()
    name: string
    // @HalProperty(StoreResource)
    // stores: Array<Store>
}

@Injectable()
export class CustomerService {

    getCustomers(): Promise<CustomerResource[]> {
        const uri = this.createUrl('customer')
        return this.client.fetchArray(uri, CustomerResource)
    }

    getStores(customer: CustomerResource): Promise<StoreResource[]> {
        const resource = customer.link('stores')
        const link = resource.fetch()
        return (<Promise<any>>link)
    }    
}

When I call customerService.getStores(customer) I get the error Error: object is not hal resource [<array of stores>]. It doesn't change a thing if I comment out the stores field in the CustomerResource type or not. However, fetching the customer in the first place works fine. And I don't see any difference to your example code (besides the await statements) in the wiki.

There is a alternate approach that actually works, but I guess it wasn't meant to be (looks a bit ugly to me ;)

const link = customer.link('stores').uri.uri
return this.client.fetchArray(link, StoreResource)

As you may already have guessed by the @Injectable() decorator, I'm using Angular 5 and calling resource.fetch() returns a ZoneAwarePromise. Maybe that's a clue.

@deblockt
Copy link
Owner

can you provide a json representation of /customerand /stores?

I will try to reproduce this issue

@Smolli
Copy link
Author

Smolli commented Nov 24, 2017

Sure. All resources are 0..n. For the sake of readability, I just provided one object per resource.


/api/v1/customer

[
    {
        "id": 22,
        "name": "Customer 22",
        "_links": {
            "self": {
                "href": "/api/v1/customer/22"
            },
            "stores": {
                "href": "/api/v1/customer/22/store"
            }
        }
    }
]

/api/v1/customer/22/store

[
    {
        "id": 144102,
        "customer_id": 22,
        "name": "City Store",
        "_links": {
            "self": {
                "href": "/api/v1/store/144102_22_KU"
            },
            "customer": {
                "href": "/api/v1/customer/22"
            },
            "sector": {
                "href": "/api/v1/sector/KU_22"
            }
        }
    }
]

/api/v1/store

[
    {
        "id": 0,
        "customer_id": 22,
        "_links": {
            "self": {
                "href": "/api/v1/store/0_22_D"
            },
            "customer": {
                "href": "/api/v1/customer/22"
            }
        }
    }
]

@deblockt
Copy link
Owner

deblockt commented Nov 24, 2017

Thank's, this doesn' work because your stores link is not an HalResource.
It's juste an array of HalResource.

each link must be an HalResource. If you want that's works, you must have a json like this :

{
    "_links": {
        "self": { "href": "/stores" },
     },
    "_embedded": {
        "stores": [
            {
                "id": 144102,
                "customer_id": 22,
                "name": "City Store",
                "_links": {
                    "self": {
                        "href": "/api/v1/store/144102_22_KU"
                    },
                    "customer": {
                        "href": "/api/v1/customer/22"
                    },
                    "sector": {
                        "href": "/api/v1/sector/KU_22"
                    }
                }
            }
        ]
    }
}

@Smolli
Copy link
Author

Smolli commented Nov 27, 2017

Ok, got it. So I guess there is no other way as to do it with the .uri.uri thingi and fetch them all by hand.

Thanks!

@Smolli Smolli closed this as completed Nov 27, 2017
@deblockt
Copy link
Owner

deblockt commented Nov 27, 2017

Sorry, but at the moment, I think link can not be an Array.
I will perform some tests. I think i will manage this case. But that can take some time.

meanwhile, you umust use uri.uri, I will update fetch method to take URI on parameter, It'll be less ugly.

@deblockt deblockt reopened this Nov 27, 2017
@Smolli
Copy link
Author

Smolli commented Nov 27, 2017

Sorry, but I think link can not be an Array.

Why not? I don't see why a link should be exclusively pointing to a single resource. As far as I understood HATEOAS, it can be any action or resource. But if I'm wrong, please tell me. I'm not that far into the topic (yet). 😁

@deblockt
Copy link
Owner

I have edited my previous comment.

I think this is the case now, but this feature will be implemented soon.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants