You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -60,9 +60,9 @@ The schema definition is extremely powerful and lets you easily apply [permissio
60
60
### Handling Requests
61
61
62
62
```php
63
-
use Tobyz\JsonApiServer\Api;
63
+
use Tobyz\JsonApiServer\JsonApi;
64
64
65
-
$api = new Api('http://example.com/api');
65
+
$api = new JsonApi('http://example.com/api');
66
66
67
67
try {
68
68
$response = $api->handle($request);
@@ -71,26 +71,26 @@ try {
71
71
}
72
72
```
73
73
74
-
`Tobyz\JsonApiServer\Api` is a [PSR-15 Request Handler](https://www.php-fig.org/psr/psr-15/). Instantiate it with your API's base URL. Convert your framework's request object into a [PSR-7 Request](https://www.php-fig.org/psr/psr-7/#321-psrhttpmessageserverrequestinterface) implementation, then let the `Api` handler take it from there. Catch any exceptions and give them back to `Api` if you want a JSON:API error response.
74
+
`Tobyz\JsonApiServer\JsonApi` is a [PSR-15 Request Handler](https://www.php-fig.org/psr/psr-15/). Instantiate it with your API's base URL. Convert your framework's request object into a [PSR-7 Request](https://www.php-fig.org/psr/psr-7/#321-psrhttpmessageserverrequestinterface) implementation, then let the `JsonApi` handler take it from there. Catch any exceptions and give them back to `JsonApi` if you want a JSON:API error response.
75
75
76
76
### Defining Resources
77
77
78
78
Define your API's resources using the `resource` method. The first argument is the [resource type](https://jsonapi.org/format/#document-resource-object-identification). The second is an instance of `Tobyz\JsonApiServer\Adapter\AdapterInterface` which will allow the handler to interact with your models. The third is a closure in which you'll build the schema for your resource.
79
79
80
80
```php
81
-
use Tobyz\JsonApiServer\Schema\Builder;
81
+
use Tobyz\JsonApiServer\Schema\Type;
82
82
83
-
$api->resource('comments', $adapter, function (Builder $schema) {
83
+
$api->resource('comments', $adapter, function (Schema $schema) {
84
84
// define your schema
85
85
});
86
86
```
87
87
88
-
We provide an `EloquentAdapter` to hook your resources up with Laravel [Eloquent](https://laravel.com/docs/5.8/eloquent) models. Set it up with an instance of the model that your resource represents. You can [implement your own adapter](https://github.com/tobyz/json-api-server/blob/master/src/Adapter/AdapterInterface.php) if you use a different ORM.
88
+
We provide an `EloquentAdapter` to hook your resources up with Laravel [Eloquent](https://laravel.com/docs/5.8/eloquent) models. Set it up with the name of the model that your resource represents. You can [implement your own adapter](https://github.com/tobyz/json-api-server/blob/master/src/Adapter/AdapterInterface.php) if you use a different ORM.
89
89
90
90
```php
91
91
use Tobyz\JsonApiServer\Adapter\EloquentAdapter;
92
92
93
-
$adapter = new EloquentAdapter(new User);
93
+
$adapter = new EloquentAdapter(User::class);
94
94
```
95
95
96
96
### Attributes
@@ -116,10 +116,10 @@ $schema->hasOne('user');
116
116
$schema->hasMany('comments');
117
117
```
118
118
119
-
By default the [resource type](https://jsonapi.org/format/#document-resource-object-identification) that the relationship corresponds to will be derived from the relationship name. In the example above, the `user` relationship would correspond to the `users` resource type, while `comments` would correspond to `comments`. If you'd like to use a different resource type, provide it as a second argument:
119
+
By default the [resource type](https://jsonapi.org/format/#document-resource-object-identification) that the relationship corresponds to will be derived from the relationship name. In the example above, the `user` relationship would correspond to the `users` resource type, while `comments` would correspond to `comments`. If you'd like to use a different resource type, call the `type` method:
120
120
121
121
```php
122
-
$schema->hasOne('author', 'people');
122
+
$schema->hasOne('author')->type('people');
123
123
```
124
124
125
125
Like attributes, the relationship will automatically read and write to the relation on your model with the same name. If you'd like it to correspond to a different relation, provide it as a third argument.
@@ -173,14 +173,14 @@ $schema->hasOne('user')
173
173
174
174
#### Polymorphic Relationships
175
175
176
-
Define polymorphic relationships on your resource using the `morphOne` and `morphMany` methods:
176
+
Define a relationship as polymorphic using the `polymorphic` method:
177
177
178
178
```php
179
-
$schema->morphOne('commentable');
180
-
$schema->morphMany('taggable');
179
+
$schema->hasOne('commentable')->polymorphic();
180
+
$schema->hasMany('taggable')->polymorphic();
181
181
```
182
182
183
-
Polymorphic relationships do not accept a second argument for the resource type, because it will be automatically derived from each related resource. Nested includes cannot be requested on these relationships.
183
+
This will mean that the resource type associated with the relationship will be derived from the model of each related resource. Consequently, nested includes cannot be requested on these relationships.
184
184
185
185
### Getters
186
186
@@ -257,7 +257,6 @@ $schema->attribute('email')
257
257
258
258
You can provide a default value for a field to be used when creating a new resource if there is no value provided by the consumer. Pass a value or a closure to the `default` method:
259
259
260
-
261
260
```php
262
261
$schema->attribute('joinedAt')
263
262
->default(new DateTime);
@@ -299,7 +298,7 @@ $schema->hasMany('groups')
299
298
You can easily use Laravel's [Validation](https://laravel.com/docs/5.8/validation) component for field validation with the `rules` function:
300
299
301
300
```php
302
-
use Tobyz\JsonApi\Server\Laravel\rules;
301
+
use Tobyz\JsonApiServer\Laravel\rules;
303
302
304
303
$schema->attribute('username')
305
304
->validate(rules('required', 'min:3', 'max:30'));
@@ -312,7 +311,7 @@ Use the `set` method to define custom mutation logic for your field, instead of
312
311
```php
313
312
$schema->attribute('firstName')
314
313
->set(function ($model, $value, $request) {
315
-
return $model->first_name = strtolower($value);
314
+
$model->first_name = strtolower($value);
316
315
});
317
316
```
318
317
@@ -396,6 +395,14 @@ You can set a default sort string to be used when the consumer has not supplied
396
395
$schema->defaultSort('-updatedAt,-createdAt');
397
396
```
398
397
398
+
To define sortable criteria that does not correspond to an attribute, use the `sort` method:
399
+
400
+
```php
401
+
$schema->sort('relevance', function ($query, $direction, $request) {
402
+
$query->orderBy('relevance', $direction);
403
+
});
404
+
```
405
+
399
406
### Pagination
400
407
401
408
By default, resource listings are automatically [paginated](https://jsonapi.org/format/#fetching-pagination) with 20 records per page. You can change this limit using the `paginate` method on the schema builder, or you can remove it by passing `null`:
@@ -434,17 +441,19 @@ $schema->meta('requestTime', function ($request) {
434
441
435
442
### Creating Resources
436
443
437
-
By default, resources are not [creatable](https://jsonapi.org/format/#crud-creating) (i.e. `POST` requests will return `403 Forbidden`). You can allow them to be created using the `creatable` and `notCreatable` methods on the schema builder:
444
+
By default, resources are not [creatable](https://jsonapi.org/format/#crud-creating) (i.e. `POST` requests will return `403 Forbidden`). You can allow them to be created using the `creatable` and `notCreatable` methods on the schema builder. Pass a closure that returns `true` if the resource should be creatable, or no value to have it always creatable.
438
445
439
446
```php
447
+
$schema->creatable();
448
+
440
449
$schema->creatable(function ($request) {
441
450
return $request->getAttribute('isAdmin');
442
451
});
443
452
```
444
453
445
454
#### Customizing the Model
446
455
447
-
When creating a resource, an empty model is supplied by the adapter. You may wish to provide a custom model in special circumstances. You can do so using the `create` method:
456
+
When creating a resource, an empty model is supplied by the adapter. You may wish to override this and provide a custom model in special circumstances. You can do so using the `create` method:
By default, resources are not [updatable](https://jsonapi.org/format/#crud-updating) (i.e. `PATCH` requests will return `403 Forbidden`). You can allow them to be updated using the `updatable` and `notUpdatable` methods on the schema builder:
By default, resources are not [deletable](https://jsonapi.org/format/#crud-deleting) (i.e. `DELETE` requests will return `403 Forbidden`). You can allow them to be deleted using the `deletable` and `notDeletable` methods on the schema builder:
0 commit comments