Querying multiple collections is giving - Collection not found

I am trying to query multiple collections as shown -

/api/collections/get/(authors|posts)

But i keep getting the error like -

{
    "error": "Collection not found"
}

Can anyone help me where i am going wrong? The individual APIs work correctly

This is not possible by default and it would only make sense if they have similar fields. Otherwise the filters wouldn’t work correctly.

If you need to query multiple collections with on api request, you have to write your own api endpoint.

What’s your actual goal? Maybe I can point you in the right direction.

Hi,

I am creating a landing page which draws information from multiple collections like carousel, comments, and posts. One way how i got it to work in a single query is by using CockpitQL and calling a GraphQL API with graphql-request. Is there any other way to get the same output?

The documentation tells querying multiple is possible - https://getcockpit.com/documentation/api so only got confused

Ah, I see. This is regex style is only to give permission to fetch multiple collections with single api keys. You can’t fetch them this way.

You can create a custom api endpoint.

sample:

<?php
$authors = cockpit('collections')->find('authors');
$posts   = cockpit('collections')->find('posts');
return compact('authors', 'posts');
1 Like

Possible solution:

create a file config/api/public/multirequest.php

<?php


/**

    <requests> parameter should look like:

    {
        "posts": {
            "endpoint": "/api/collections/get/posts",
            "params": {"lang": "de"}
        },
        "footer": {
            "endpoint": "/api/singletons/get/footer",
            "params": {"lang": "de"}
        }
    }

 */


$requests = $this->param('requests');

if (!$requests) {
    $this->stop(['error' => 'requests parameter is missing'], 412);
}

$ret = [];
$cockpit = cockpit();

foreach ($requests as $k => $req) {

    if (!isset($req['endpoint'])) continue;

    $request = new Lime\Request([
        'request' => array_merge([], $req['query'] ?? [], $req['post'] ?? [], $req['params'] ?? []),
        'post' => $req['post'] ?? [],
        'cookies' => $req['cookies'] ?? [],
        'query' => $req['query'] ?? [],
        'files' => $request->files ?? [],
        'server' => $req['server'] ?? [],
        'headers' => $req['headers'] ?? [],
    ]);

    try {

        $r = $cockpit->run($req['endpoint'], $request,  false);

        $body = $r->body;
        $mime = $r->mime;

        if (\is_array($body) || \is_object($body)) {
            $mime = 'json';
        }

        $ret[$k] = [
            'status' => $r->status,
            'mime' => $mime,
            'body' => $body,
        ];

    } catch(\Throwable $e) {
        
        $ret[$k] = [
            'error' => $e->getMessage(),
            'file' => $e->getFile(),
            'line' => $e->getLine(),
        ];
    }
}

return $ret;

now you can query multiple api endpoints by requesting /api/public/multirequest

3 Likes