Skip to content
This repository has been archived by the owner on Mar 29, 2020. It is now read-only.

Default resolver for connections

Pre-release
Pre-release
Compare
Choose a tag to compare
@chrissm79 chrissm79 released this 23 Nov 21:33
· 109 commits to master since this release

If no resolve method is set on a connection, the following function will be used:

function ($collection, array $args, ResolveInfo $info) use ($name) {
    $items = [];

    if ($collection instanceof Model) {
        $items = $collection->getAttribute($name);
    } else if (is_object($collection) && method_exists($collection, 'get')) {
        $items = $collection->get($name);
    } else if (is_array($collection) && isset($collection[$name])) {
        $items = new Collection($collection[$name]);
    }

    if (isset($args['first'])) {
        $total = $items->count();
        $first = $args['first'];
        $after = $this->decodeCursor($args);
        $currentPage = $first && $after ? floor(($first + $after) / $first) : 1;

        return new Paginator(
            $items->slice($after)->take($first),
            $total,
            $first,
            $currentPage
        );
    }

    return new Paginator(
        $items,
        count($items),
        count($items)
    );
}