The Fields Pattern or Field Selection Pattern for REST API design is a useful approach whenever the representations returned from the API become quite large and the API can be used by many clients in different contexts. For example, a native app client may only require a select few fields from the usual resource representation.
Suppose we have a resource employees, with employee representations containing a large number of fields. With the fields pattern, a client could make a request of the form:
The API will respond to this request with only these fields in addition to id:
"name": "John Smith",
A different client can request a different selection of fields:
"streetName": "REST Avenue",
"postalCode": "J6V 9M2",
It is also possible to omit the id field, though arguably a resource representation should always have its primary identifier present, even when selecting fields.
If the fields parameter is omitted, the API responds with all fields of the employee resource available.
What if we want to be able to select fields of sub-objects, such as the address object? There are several options, for example:
However, none of these are fully satisfying and venture into inventing syntax. Almost certainly, no two API designs will select the same syntax, and so none of these are ideal for a natural, simple design.
Selection of fields on arbitrarily complex objects is one of the many elegant, standard solutions provided by GraphQL, so a GraphQL API design may be preferable in cases where complex multi-level field selection is desired.