laravel-json-api
laravel-json-api copied to clipboard
Is there support for field selection parameters?
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? :)
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
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 ?
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.
Thank you very much ! This is awesome !
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!
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.
@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
.
@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.