JSON:API
With Laravel JSON:API installed, its resources are documented as JSON:API resource objects in
application/vnd.api+json, with their attributes, relationships, links, meta and included resources. Nothing to
configure: the installed version is detected, and php artisan about shows it.
ark4ne/laravel-json-apiWhat is documented
/**
* @extends JsonApiResource<Article>
*/
final class ArticleResource extends JsonApiResource
{
public function toAttributes(Request $request): iterable
{
return [
'title' => $this->string(),
'published_at' => $this->date(),
];
}
protected function toRelationships(Request $request): iterable
{
return ['author' => $this->one(AuthorResource::class)];
}
}
{
"type": "object",
"required": ["id", "type"],
"properties": {
"id": { "type": "string" },
"type": { "type": "string", "const": "article" },
"attributes": {
"type": "object",
"properties": {
"title": { "type": ["string", "null"] },
"published_at": { "type": ["string", "null"], "format": "date-time" }
}
},
"relationships": {
"type": "object",
"properties": {
"author": {
"type": "object",
"properties": {
"data": {
"type": ["object", "null"],
"required": ["type", "id"],
"properties": {
"type": { "type": "string", "const": "author" },
"id": { "type": "string" }
}
}
}
}
}
}
}
}
- The descriptors give the type of each value:
$this->string(),$this->date(),$this->struct()… - The relationships give the resource identifiers, and the resources a response may include (
included). - A resource returned by an action is wrapped in a document:
{"data": Article, "included": [Author]}; a collection gives{"data": [Article]}, withlinksandmetawhen paginated.
Include and sparse fieldsets
The Includes and Fields rules of a form request give the include and fields[type] query parameters, with the
relationships and the fields they accept:
public function rules(): array
{
return [
'include' => [new Includes(ArticleResource::class)],
'fields' => [new Fields(ArticleResource::class)],
];
}
[
{
"name": "include",
"in": "query",
"schema": { "type": "string", "example": "author", "description": "Relationships to include, separated by commas: `author`." }
},
{
"name": "fields[article]",
"in": "query",
"schema": { "type": "string", "description": "Fields of `article`, separated by commas: `title`, `published_at`." }
},
{
"name": "fields[author]",
"in": "query",
"schema": { "type": "string", "description": "Fields of `author`, separated by commas: `name`." }
}
]
The rules are Ark4ne\JsonApi\Requests\Rules\… in 1.x, Ark4ne\JsonApi\Http\Rules\… from 2.x.
Errors
If the application renders its errors as JSON:API error documents (JsonApi::renderErrors() from 2.x), say so:
'integrations' => [
'json_api' => ['errors' => true],
],
The 401, 403, 404, 422 and other error responses are then JSON:API error documents, in application/vnd.api+json.
Versions
ark4ne/laravel-json-api ^1.9. The generator runs the hooks of the resources (toAttributes(), toRelationships(),
toLinks(), toResourceMeta()) on the package's fake model and reads the descriptors they return.
ark4ne/laravel-json-api ^2.0. The generator reads the definition the package compiles
(ArticleResource::definition()): the type of each value, the related resources of each relationship (polymorphic
ones, Laravel's native JSON:API resources), their links and meta. id is a string and the documents carry the
jsonapi member (the JsonApi component), as the package renders them. A value without descriptor (a closure) is
typed by rendering the resource on a sample of its model. The error documents are those of JsonApi::renderErrors().
ark4ne/laravel-json-api ^3.0, on ark4ne/laravel-api-revisions. The values of a
definition are the shapes of the base: each field gives its type and its constraints (min(), max(), format(),
pattern()), its description, its first example and deprecated. An attribute rendered only when the sparse fieldset
names it (whenInFields()) says so; the links of a resource object are a URL or a link object by name. A value of
unknown type (mixed()) and a custom type are typed by rendering the resource on a sample of its model.
Each revision of the API can be its own document: see API revisions.
The findings of the compilation of a definition (2.x and 3.x) are relayed as diagnostics of the document, once each.