Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 544
JSONAPI::Resources Querystring Examples
Edipo Vinicius da Silva edited this page Aug 23, 2017
·
11 revisions
The purpose of this article is to correlate querystring parameter usage in jsonapi-resources with the jsonapi specification
Given the example resource:
classUserResource < JSONAPI::Resourceattribute:first_nameattribute:last_nameattribute:emailattribute:created_atattribute:updated_atpaginator:offset#can also operate in 'paged' mode: see READMEendand the following route (in routes.rb):
jsonapi_resources :users
we can do the following:
In the case where you don't need all the attributes to be returned, you can request specific attributes be returned.
Limiting Returned Resource Attributes:
/users?fields[{resource-type}]={attribute-names}{resource-type}= the pluralized type of model whose attributes you wish to limit (must be dasherized){attribute-names}= a comma separated list of the attributes you wish to be included (must be dasherized)- e.g.
/users?fields[users]=first-name,last-namewill returnuserresources including only thefirst_nameandlast_nameattributes
Ascending By A Property:
/users?sort={attribute-name}{attribute-name}= the attribute you wish to sort by (must be dasherized)- e.g.
/users?sort=last-namewill sort the collection ascending by last name (A-Z)
Descending By A Property:
/users?sort=-{attribute-name}- the sort parameter is prepended by a
- {attribute-name}= the attribute you wish to sort by (must be dasherized)- e.g.
/users?sort=-last-namewill sort the collection descending by last name (Z-A)
Provided pagination has been enabled for your application or resource (see README):
Limiting Returned Resources:
/users?page[limit]={resource_count}{resource_count}= number of resources you want returned- e.g.
/users?page[limit]=5will return the first 5userresources
Offsetting Returned Resources:
/users?page[offset]={resource_offset}{resource_offset}= the number of records to offset by prior to returning resources- e.g.
/users?page[offset]=10will skip the first 10userresources in the collection
Combining Limiting/Offsetting:
/users?page[limit]={resource_count}&page[offset]={resource_offset}- e.g.
/users?page[limit]=5&page[offset]=10will skipuserresources 0-10 and return resources 11-15
Paging Returned Resources:
/users?page[number]={page_number}{page_number}= the page number of the resources to be returned (this is "one-based", i.e. the first page is1, not0)- e.g.
/users?page[number]=7will skip the first 7 pages ofuserresources in the collection, and return the 8th page
Setting Page Size:
/users?page[size]={page_size}{page_size}= the number of resources to be returned per page- e.g.
/users?page[size]=25will return 25userresources on a single page
Combining Paging/Page Sizing:
/users?page[size]={page_size}&page[number]={page_number}- e.g.
/users?page[size]=25&page[number]=5will skipuserresources 0-100 and return resources 101-125