-
Notifications
You must be signed in to change notification settings - Fork 109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Attach top-level meta in index
response
#225
Comments
Hi! Thanks for creating this issue. There is a way to include meta in a response at the moment, but it definitely could be a lot better supported. Your summary of the three different ways to add meta is a great list, and I definitely think it would be good to support each of those ways. At the moment if you wanted to add meta you would have to use the controller hooks that are described in this chapter: Basically if you return a response from any hook it will use that. Most of the methods on the JSON API responses factory take meta as their argument. You can see those methods here: As an example, when reading a resource, the controller does this: return $this->reply()->content($post); You could change this by adding a protected function reading($post)
{
// ... work out $meta
return $this->reply()->content($post, [], $meta);
} I'd like to do this in a much easier way but that's how you'd need to implement it at the moment. |
Also, it's difficult to add top-level, query-independant meta data to |
@dapperdanman1400 yes that's definitely a fair comment. It would be worth me adding a |
@lindyhopchris A My current quick-and-dirty workaround is using the public function searching(ValidatedRequest $request)
{
$store = app(\CloudCreativity\LaravelJsonApi\Contracts\Store\StoreInterface::class);
$result = $store->queryRecords($request->getResourceType(), $request->getParameters());
$data = [];
try {
$data = get_object_vars($result->getData()); // need to do this to e.g. not loose pagination meta?
} catch {
// ignore
}
return $this->reply()
->content(
$result,
[], // links
array_merge($data, [
'globalMeta' => 'Hello from searching hook',
])
);
} edit: Just realized that the |
Searched hook has been added on |
FYI controller takes care of calling |
i have a very similar question in mind, i like to add the amount of results in the very top meta field and did not found any solution for this so far. |
Yes, that's correct I need to add in support for meta in different places. |
@lindyhopchris Are there any concrete plans on tackling this in the foreseeable future? I'm currently struggling using the e.g. I currently have some response with top-level pagination meta "magically" (to my current knowledge) added: {
"meta": {
"page": {
"current-page": 1,
"per-page": 100,
"from": 1,
"to": 100,
"total": 21781,
"last-page": 218
}
},
"links": {},
"data": {}
} Without finding the time yet to delve into the code, it seems that data in a Now my problem is, that I didn't find an obvious way to get my hands on that pagination meta data within the My current implementation looks something like this and loses the public function searched($paginatedData)
{
$meta = ['some' => 'data'];
return $this->reply()
->content(
$paginatedData,
[], // links
$meta // <-- how can I add my own meta without overriding the pagination meta?
);
} What I would like to have is something like this (note the {
"meta": {
"some": "data",
"page": {
"current-page": 1,
"per-page": 100,
"from": 1,
"to": 100,
"total": 21781,
"last-page": 218
}
},
"links": {},
"data": {}
} |
@tsterker this looks like a bug... if you're passing $meta into the I'll create an issue for that separately because the bug is not the same as the feature that this issue is requesting (and which probably won't land until 2.0 as it'll be breaking to add in top-level meta support in a nice way!). |
It feels like I'm missing something obvious, but I was not able to find a straight-forward way for attaching custom top-level meta to the response (just like the
page
meta for pagination). I also didn't find any tickets that seemed related to this.I have different contexts in mind, for which one might want to attach top-level metadata, in case it could inform any design decisions:
e.g.
requested_at
e.g.
sortable_fields
differs for users and postse.g.
facet_fields
counts depend on the current query and are only available with the query resulte.g.
Apologies if there already is a way of achieving this in a clean way. If not, are there any plans of adding support for this?
The text was updated successfully, but these errors were encountered: