Skip to content

Commit 8d01849

Browse files
authored
[Feature] Add default include paths to query request classes (#75)
Allows the developer to specify default include paths that should be used if the client provides none.
1 parent 8d87764 commit 8d01849

File tree

9 files changed

+17
-12
lines changed

9 files changed

+17
-12
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"require": {
2626
"php": "^7.4|^8.0",
2727
"ext-json": "*",
28-
"laravel-json-api/core": "^1.0.0-beta.2",
28+
"laravel-json-api/core": "^1.0.0-beta.3",
2929
"laravel-json-api/eloquent": "^1.0.0-beta.3",
3030
"laravel-json-api/encoder-neomerx": "^1.0.0-beta.1",
3131
"laravel-json-api/exceptions": "^1.0.0-beta.2",

src/Http/Controllers/Actions/FetchMany.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,6 @@ public function index(Route $route, StoreContract $store)
6161
$response = $this->searched($data, $request);
6262
}
6363

64-
return $response ?: new DataResponse($data);
64+
return $response ?: DataResponse::make($data)->withQueryParameters($request);
6565
}
6666
}

src/Http/Controllers/Actions/FetchOne.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,6 @@ public function show(Route $route, StoreContract $store)
6161
$response = $this->read($model, $request);
6262
}
6363

64-
return $response ?: new DataResponse($model);
64+
return $response ?: DataResponse::make($model)->withQueryParameters($request);
6565
}
6666
}

src/Http/Controllers/Actions/FetchRelated.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ public function showRelated(Route $route, StoreContract $store)
7676
$response = $this->{$hook}($model, $data, $request);
7777
}
7878

79-
return $response ?: new RelatedResponse(
79+
return $response ?: RelatedResponse::make(
8080
$model,
8181
$relation->name(),
8282
$data,
83-
);
83+
)->withQueryParameters($request);
8484
}
8585
}

src/Http/Controllers/Actions/FetchRelationship.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ public function showRelationship(Route $route, StoreContract $store)
7676
$response = $this->{$hook}($model, $data, $request);
7777
}
7878

79-
return $response ?: new RelationshipResponse(
79+
return $response ?: RelationshipResponse::make(
8080
$model,
8181
$relation->name(),
8282
$data
83-
);
83+
)->withQueryParameters($request);
8484
}
8585
}

src/Http/Controllers/Actions/Store.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,6 @@ public function store(Route $route, StoreContract $store)
7171
$response = $this->saved($model, $request, $query);
7272
}
7373

74-
return $response ?: new DataResponse($model);
74+
return $response ?: DataResponse::make($model)->withQueryParameters($query);
7575
}
7676
}

src/Http/Controllers/Actions/Update.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,6 @@ public function update(Route $route, StoreContract $store)
7373
$response = $this->saved($model, $request, $query);
7474
}
7575

76-
return $response ?: new DataResponse($model);
76+
return $response ?: DataResponse::make($model)->withQueryParameters($query);
7777
}
7878
}

src/Http/Controllers/Actions/UpdateRelationship.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ public function updateRelationship(Route $route, StoreContract $store)
8181
$response = $this->{$hook}($model, $result, $request, $query);
8282
}
8383

84-
return $response ?: new RelationshipResponse(
84+
return $response ?: RelationshipResponse::make(
8585
$model,
8686
$fieldName,
8787
$result
88-
);
88+
)->withQueryParameters($query);
8989
}
9090
}

src/Http/Requests/ResourceQuery.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ class ResourceQuery extends FormRequest implements QueryParameters
5252
self::JSON_API_MEDIA_TYPE,
5353
];
5454

55+
/**
56+
* @var string[]|null
57+
*/
58+
protected ?array $defaultIncludePaths = null;
59+
5560
/**
5661
* Specify the callback to use to guess the request class for querying many resources.
5762
*
@@ -149,7 +154,7 @@ public function includePaths(): ?IncludePaths
149154
return IncludePaths::fromString($data['include'] ?: '');
150155
}
151156

152-
return null;
157+
return IncludePaths::nullable($this->defaultIncludePaths);
153158
}
154159

155160
/**

0 commit comments

Comments
 (0)