angular2-jsonapi
angular2-jsonapi copied to clipboard
Include relationship in http response even without "include" in URL
Maybe It is something that I don't understand, apology if that's the case.
I would like to have the option of getting the relationship data from the model even if I don't "include" relationship in my GET request.
Let's say we have a Model like this:
@JsonApiModelConfig({
type: 'room',
})
export class Room extends JsonApiModel {
@Attribute()
title: string;
@BelongsTo()
property: Property;
}
I want to get a specific room, I want to know which propertyID it belongs to but I don't want to request the propertyObject because it's really big, Only when the user click on a button will I then fetch the property from the Back End.
If I do a GET query https://example.com/rooms/1
, the server will reply something like this
{
"data": {
"id": "1",
"type": "room",
"attributes": {
"title": "Kitchen"
},
"relationships": {
"property": {
"data": {
"id": "2",
"type": "property"
}
}
}
}
}
So in the response from the backend, we have the ID of the corresponding property.
But I'm not able to get this data from my GET query, the only way I was able to retrieve the property ID is when I add include=property
Is there a way to retrieve all relationship IDs without retrieving the relationship Data ?
Is it the exact same thing than what people are asking in https://github.com/ghidoz/angular2-jsonapi/issues/103 ?
You can use sparse fieldset to get certain properties of the child object - https://jsonapi.org/format/#fetching-sparse-fieldsets i.e https://example.com/rooms/1?include=property&fields[property]=title,description,rent
it will return
{
"data": {
"id": "1",
"type": "room",
"attributes": {
"title": "Kitchen"
},
"relationships": {
"property": {
"data": {
"id": "2",
"type": "property",
"title": "3 bed in SF"
"description": "Flat"
"rent": "£200 per month"
}
}
}
}
}
From the JSON API point of view that's perfectly valid but trying to understand the code of angular2-jsonapi, I believe that if the "included" field is not present, the data won't be available in the JsonApiModel
@Tonio-lavanda yes you are right you have to "include" the object i.e https://example.com/rooms/1?include=property&fields[property]=title,description,rent
however you can use the the sparse field and just get the id and nothing else so https://example.com/rooms/1?include=property&fields[property]=id