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

Include relationship in http response even without "include" in URL #238

Open
Tonio-lavanda opened this issue Aug 21, 2019 · 3 comments
Open

Comments

@Tonio-lavanda
Copy link

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 #103 ?

@mh453Uol
Copy link

mh453Uol commented Sep 2, 2019

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"        
       }
      }
    }
  }
}

@Tonio-lavanda
Copy link
Author

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

@mh453Uol
Copy link

@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

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