Skip to content

Commit 28b5a7d

Browse files
committed
Add support for "first" & "last" fields for PageArray types.
1 parent 84147ea commit 28b5a7d

File tree

8 files changed

+182
-1
lines changed

8 files changed

+182
-1
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace ProcessWire\GraphQL\Field\PageArray;
4+
5+
use Youshido\GraphQL\Field\AbstractField;
6+
use Youshido\GraphQL\Execution\ResolveInfo;
7+
use ProcessWire\GraphQL\Type\InterfaceType\PageInterfaceType;
8+
9+
class PageArrayFirstField extends AbstractField {
10+
11+
public function getName()
12+
{
13+
return 'first';
14+
}
15+
16+
public function getType()
17+
{
18+
return new PageInterfaceType();
19+
}
20+
21+
public function resolve($value, array $args, ResolveInfo $info)
22+
{
23+
return $value->first();
24+
}
25+
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace ProcessWire\GraphQL\Field\PageArray;
4+
5+
use Youshido\GraphQL\Field\AbstractField;
6+
use Youshido\GraphQL\Execution\ResolveInfo;
7+
use ProcessWire\GraphQL\Type\InterfaceType\PageInterfaceType;
8+
9+
class PageArrayLastField extends AbstractField {
10+
11+
public function getName()
12+
{
13+
return 'last';
14+
}
15+
16+
public function getType()
17+
{
18+
return new PageInterfaceType();
19+
}
20+
21+
public function resolve($value, array $args, ResolveInfo $info)
22+
{
23+
return $value->last();
24+
}
25+
26+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace ProcessWire\GraphQL\Field\TemplatedPageArray;
4+
5+
use Youshido\GraphQL\Type\ListType\ListType;
6+
use ProcessWire\Template;
7+
use ProcessWire\GraphQL\Field\PageArray\PageArrayFirstField;
8+
use ProcessWire\GraphQL\Type\Object\TemplatedPageType;
9+
10+
class TemplatedPageArrayFirstField extends PageArrayFirstField {
11+
12+
protected $template;
13+
14+
public function __construct(Template $template)
15+
{
16+
$this->template = $template;
17+
parent::__construct([]);
18+
}
19+
20+
public function getType()
21+
{
22+
return new TemplatedPageType($this->template);
23+
}
24+
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace ProcessWire\GraphQL\Field\TemplatedPageArray;
4+
5+
use Youshido\GraphQL\Type\ListType\ListType;
6+
use ProcessWire\Template;
7+
use ProcessWire\GraphQL\Field\PageArray\PageArrayLastField;
8+
use ProcessWire\GraphQL\Type\Object\TemplatedPageType;
9+
10+
class TemplatedPageArrayLastField extends PageArrayLastField {
11+
12+
protected $template;
13+
14+
public function __construct(Template $template)
15+
{
16+
$this->template = $template;
17+
parent::__construct([]);
18+
}
19+
20+
public function getType()
21+
{
22+
return new TemplatedPageType($this->template);
23+
}
24+
25+
}

src/Type/Object/PageArrayType.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
use Youshido\GraphQL\Type\Object\AbstractObjectType;
66
use ProcessWire\GraphQL\Type\Object\WireArrayType;
77
use ProcessWire\GraphQL\Field\PageArray\PageArrayListField;
8+
use ProcessWire\GraphQL\Field\PageArray\PageArrayFirstField;
9+
use ProcessWire\GraphQL\Field\PageArray\PageArrayLastField;
810
use ProcessWire\GraphQL\Field\PageArray\PageArrayFindField;
9-
use ProcessWire\GraphQL\Field\PageArray\PageArrayCountField;
1011
use ProcessWire\GraphQL\Type\InterfaceType\PaginatedArrayInterfaceType;
1112

1213
class PageArrayType extends AbstractObjectType {
@@ -26,6 +27,8 @@ public function build($config)
2627
$config->applyInterface(new PaginatedArrayInterfaceType());
2728
$config->addField(new PageArrayListField());
2829
$config->addField(new PageArrayFindField());
30+
$config->addField(new PageArrayFirstField());
31+
$config->addField(new PageArrayLastField());
2932
}
3033

3134
public function getInterfaces()

src/Type/Object/TemplatedPageArrayType.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use ProcessWire\GraphQL\Type\Object\PageArrayType;
77
use ProcessWire\GraphQL\Field\TemplatedPageArray\TemplatedPageArrayFindField;
88
use ProcessWire\GraphQL\Field\TemplatedPageArray\TemplatedPageArrayListField;
9+
use ProcessWire\GraphQL\Field\TemplatedPageArray\TemplatedPageArrayFirstField;
10+
use ProcessWire\GraphQL\Field\TemplatedPageArray\TemplatedPageArrayLastField;
911

1012
class TemplatedPageArrayType extends PageArrayType {
1113

@@ -39,6 +41,8 @@ public function build($config)
3941
parent::build($config);
4042
$config->addField(new TemplatedPageArrayFindField($this->template));
4143
$config->addField(new TemplatedPageArrayListField($this->template));
44+
$config->addField(new TemplatedPageArrayFirstField($this->template));
45+
$config->addField(new TemplatedPageArrayLastField($this->template));
4246
}
4347

4448
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace ProcessWire\GraphQL\Test\Field\Page\Fieldtype;
4+
5+
use \ProcessWire\GraphQL\Utils;
6+
use \ProcessWire\GraphQL\Test\GraphQLTestCase;
7+
use \ProcessWire\GraphQL\Test\Field\Page\Traits\AccessTrait;
8+
9+
class PageArrayFirstFieldTest extends GraphQLTestCase {
10+
11+
const accessRules = [
12+
'legalTemplates' => ['city'],
13+
'legalFields' => ['title'],
14+
];
15+
16+
use AccessTrait;
17+
18+
public function testValue()
19+
{
20+
$firstCity = Utils::pages()->find("template=city")->first();
21+
$query = "{
22+
city {
23+
first {
24+
name
25+
id
26+
title
27+
}
28+
}
29+
}";
30+
$res = $this->execute($query);
31+
$this->assertEquals($firstCity->name, $res->data->city->first->name, 'Retrieves correct name of the first page.');
32+
$this->assertEquals($firstCity->id, $res->data->city->first->id, 'Retrieves correct id of the first page.');
33+
$this->assertEquals($firstCity->title, $res->data->city->first->title, 'Retrieves correct title of the first page.');
34+
}
35+
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace ProcessWire\GraphQL\Test\Field\Page\Fieldtype;
4+
5+
use \ProcessWire\GraphQL\Utils;
6+
use \ProcessWire\GraphQL\Test\GraphQLTestCase;
7+
use \ProcessWire\GraphQL\Test\Field\Page\Traits\AccessTrait;
8+
9+
class PageArrayLastFieldTest extends GraphQLTestCase {
10+
11+
const accessRules = [
12+
'legalTemplates' => ['city'],
13+
'legalFields' => ['title'],
14+
];
15+
16+
use AccessTrait;
17+
18+
public function testValue()
19+
{
20+
$lastCity = Utils::pages()->find("template=city, limit=50")->last();
21+
$query = "{
22+
city {
23+
last {
24+
name
25+
id
26+
title
27+
}
28+
}
29+
}";
30+
$res = $this->execute($query);
31+
$this->assertEquals($lastCity->name, $res->data->city->last->name, 'Retrieves correct name of the last page.');
32+
$this->assertEquals($lastCity->id, $res->data->city->last->id, 'Retrieves correct id of the last page.');
33+
$this->assertEquals($lastCity->title, $res->data->city->last->title, 'Retrieves correct title of the last page.');
34+
}
35+
36+
}

0 commit comments

Comments
 (0)