laravel-json-api icon indicating copy to clipboard operation
laravel-json-api copied to clipboard

Is there support for field selection parameters?

Open igorsantos07 opened this issue 6 years ago • 8 comments

I'm researching API packages for Laravel and was wondering if the missing doc page called "sparse fieldsets" is related to the ?fields parameter specified on the JSON-API, that allows a request to only receive a number of specified fields on the response.

On the same topic, is sorting supported or a planned feature?

Bottom-line: what's "to be done", the docs or the feature itself? :)

igorsantos07 avatar Jun 12 '18 07:06 igorsantos07

The @todos in the docs indicate where I need to write docs, but the features do work. If a feature doesn't work there'll be an issue linked to a milestone.

The fields parameter is fully support so if you send it with a request it will work. You may need to set the allowedFieldSetsType on your validators class, as per this doc block: https://github.com/cloudcreativity/laravel-json-api/blob/master/src/Http/Query/ChecksQueryParameters.php#L64-L82

lindyhopchris avatar Jun 13 '18 10:06 lindyhopchris

First, I really appreciate this project. It's a must have for every laravel devs who try to create and maintain standardized Json API. I was wondering how the felds parameter actually works ? I guess I can list the fields of the resource that I want to be included in the response. But can I also choose the fields of the related resources ? I mean, If I fetch a single post with all it's related comments, can I choose the comments fields that I want ?

mathieufrh avatar Apr 08 '19 21:04 mathieufrh

Yes the implementation complies with the spec: https://jsonapi.org/format/#fetching-sparse-fieldsets

Those docs give this example:

GET /articles?include=author&fields[articles]=title,body&fields[people]=name HTTP/1.1
Accept: application/vnd.api+json

In that the fields[articles] sets the fields for the articles resource, the fields[people] sets the fields for the people resource.

So yes, you can ask for the fields of the primary resource that is being requested plus the included resources.

lindyhopchris avatar Apr 09 '19 08:04 lindyhopchris

Thank you very much ! This is awesome !

mathieufrh avatar Apr 09 '19 21:04 mathieufrh

Hi! I believe that you should consider updating this part of docs, because @todos in these docs is somehow confusing. You can at least write that this feature is implemented. Thanks!

myfbone avatar Dec 19 '19 11:12 myfbone

I would like to know (as it does not seem to work) if it's possible to do get a specific field on a relationship link.

For example:

http://127.0.0.1:8000/api/v1/assets/27/histories?fields[assets]=id By doing this, I still have all the attributes and I wanted to get just a specific field.

lironesamoun avatar Apr 28 '21 13:04 lironesamoun

@lilouch I remember something like using a dot for that. If you have a relationship assets with a field id on each asset, I think you should try assets.id.

ben221199 avatar Apr 28 '21 16:04 ben221199

@lilouch it's because you're only requesting the id field. If you only want the resource identifiers of a relationship, you should use this endpoint:

GET http://127.0.0.1:8000/api/v1/assets/27/relationships/histories HTTP/1.1
Accept: application/vnd.api+json

The fields will work on the endpoint that you're using (/api/v1/assets/27/histories) but the fields should be attributes and/or relationships. That will return sparse fieldsets.

lindyhopchris avatar Apr 28 '21 16:04 lindyhopchris