Skip to content

Commit 66b040b

Browse files
committed
Create PageFileInterfaceType for file based fields.
1 parent 8e99396 commit 66b040b

File tree

3 files changed

+138
-96
lines changed

3 files changed

+138
-96
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
3+
namespace ProcessWire\GraphQL\Type\InterfaceType;
4+
5+
use Youshido\GraphQL\Type\InterfaceType\AbstractInterfaceType;
6+
use Youshido\GraphQL\Type\Scalar\StringType;
7+
use Youshido\GraphQL\Type\Scalar\IntType;
8+
use ProcessWire\Pageimage;
9+
use ProcessWire\GraphQL\Type\Object\PageFileType;
10+
use ProcessWire\GraphQL\Type\Object\PageImageType;
11+
12+
class PageFileInterfaceType extends AbstractInterfaceType {
13+
14+
public function getName()
15+
{
16+
return 'PageFileInterface';
17+
}
18+
19+
public function getDescription()
20+
{
21+
return 'ProcessWire PageFile interface.';
22+
}
23+
24+
public function build($config)
25+
{
26+
$config->addField('url', [
27+
'type' => new StringType(),
28+
'description' => 'URL to the file on the server.',
29+
'resolve' => function ($value) {
30+
return (string) $value->url;
31+
}
32+
]);
33+
34+
$config->addField('httpUrl', [
35+
'type' => new StringType(),
36+
'description' => 'The web accessible URL (with scheme and hostname) to this Pagefile.',
37+
'resolve' => function ($value) {
38+
return (string) $value->httpUrl();
39+
}
40+
]);
41+
42+
$config->addField('URL', [
43+
'type' => new StringType(),
44+
'description' => "Same as 'url' property but with browser cache busting query
45+
string appended that represents the file's modification time.",
46+
'resolve' => function ($value) {
47+
return (string) $value->URL;
48+
}
49+
]);
50+
51+
$config->addField('basename', [
52+
'type' => new StringType(),
53+
'description' => 'The filename without the path.',
54+
'resolve' => function ($value) {
55+
return (string) $value->basename;
56+
}
57+
]);
58+
59+
$config->addField('description', [
60+
'type' => new StringType(),
61+
'description' => 'The description of the file.',
62+
'resolve' => function ($value) {
63+
return (string) $value->description;
64+
}
65+
]);
66+
67+
$config->addField('ext', [
68+
'type' => new StringType(),
69+
'description' => "File’s extension.",
70+
'resolve' => function ($value) {
71+
return (string) $value->ext;
72+
}
73+
]);
74+
75+
$config->addField('filesize', [
76+
'type' => new IntType(),
77+
'description' => 'File size (number of bytes).',
78+
'resolve' => function ($value) {
79+
return (integer) $value->filesize;
80+
}
81+
]);
82+
83+
$config->addField('modified', [
84+
'type' => new IntType(),
85+
'description' => 'Unix timestamp of when Pagefile (file, description or tags) was last modified.',
86+
'resolve' => function ($value) {
87+
return (integer) $value->modified;
88+
}
89+
]);
90+
91+
$config->addField('mtime', [
92+
'type' => new IntType(),
93+
'description' => 'Unix timestamp of when file (only) was last modified.',
94+
'resolve' => function ($value) {
95+
return (integer) $value->mtime;
96+
}
97+
]);
98+
99+
$config->addField('created', [
100+
'type' => new IntType(),
101+
'description' => 'Unix timestamp of when file was created.',
102+
'resolve' => function ($value) {
103+
return (integer) $value->created;
104+
}
105+
]);
106+
107+
$config->addField('filesizeStr', [
108+
'type' => new StringType(),
109+
'description' => 'File size as a formatted string, i.e. “123 Kb”.',
110+
'resolve' => function ($value) {
111+
return (string) $value->filesizeStr;
112+
}
113+
]);
114+
}
115+
116+
public function resolveType($opject)
117+
{
118+
if ($object instanceof Pageimage) return new PageImageType();
119+
return new PageFileType();
120+
}
121+
122+
}

src/Type/Object/PageFileType.php

Lines changed: 7 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
namespace ProcessWire\GraphQL\Type\Object;
44

55
use Youshido\GraphQL\Type\Object\AbstractObjectType;
6-
use Youshido\GraphQL\Type\Scalar\StringType;
7-
use Youshido\GraphQL\Type\Scalar\IntType;
8-
use Youshido\GraphQL\Type\ListType\ListType;
6+
use ProcessWire\GraphQL\Type\InterfaceType\PageFileInterfaceType;
97

108
class PageFileType extends AbstractObjectType {
119

@@ -16,101 +14,17 @@ public function getName()
1614

1715
public function getDescription()
1816
{
19-
return 'ProcessWire PageFiel.';
17+
return 'ProcessWire PageFile.';
2018
}
2119

2220
public function build($config)
2321
{
24-
25-
$config->addField('url', [
26-
'type' => new StringType(),
27-
'description' => 'URL to the file on the server.',
28-
'resolve' => function ($value) {
29-
return (string) $value->url;
30-
}
31-
]);
32-
33-
$config->addField('httpUrl', [
34-
'type' => new StringType(),
35-
'description' => 'The web accessible URL (with scheme and hostname) to this Pagefile.',
36-
'resolve' => function ($value) {
37-
return (string) $value->httpUrl();
38-
}
39-
]);
40-
41-
$config->addField('URL', [
42-
'type' => new StringType(),
43-
'description' => "Same as 'url' property but with browser cache busting query
44-
string appended that represents the file's modification time.",
45-
'resolve' => function ($value) {
46-
return (string) $value->URL;
47-
}
48-
]);
49-
50-
$config->addField('basename', [
51-
'type' => new StringType(),
52-
'description' => 'The filename without the path.',
53-
'resolve' => function ($value) {
54-
return (string) $value->basename;
55-
}
56-
]);
57-
58-
$config->addField('description', [
59-
'type' => new StringType(),
60-
'description' => 'The description of the file.',
61-
'resolve' => function ($value) {
62-
return (string) $value->description;
63-
}
64-
]);
65-
66-
$config->addField('ext', [
67-
'type' => new StringType(),
68-
'description' => "File’s extension.",
69-
'resolve' => function ($value) {
70-
return (string) $value->ext;
71-
}
72-
]);
73-
74-
$config->addField('filesize', [
75-
'type' => new IntType(),
76-
'description' => 'File size (number of bytes).',
77-
'resolve' => function ($value) {
78-
return (integer) $value->filesize;
79-
}
80-
]);
81-
82-
$config->addField('modified', [
83-
'type' => new IntType(),
84-
'description' => 'Unix timestamp of when Pagefile (file, description or tags) was last modified.',
85-
'resolve' => function ($value) {
86-
return (integer) $value->modified;
87-
}
88-
]);
89-
90-
$config->addField('mtime', [
91-
'type' => new IntType(),
92-
'description' => 'Unix timestamp of when file (only) was last modified.',
93-
'resolve' => function ($value) {
94-
return (integer) $value->mtime;
95-
}
96-
]);
97-
98-
$config->addField('created', [
99-
'type' => new IntType(),
100-
'description' => 'Unix timestamp of when file was created.',
101-
'resolve' => function ($value) {
102-
return (integer) $value->created;
103-
}
104-
]);
22+
$config->applyInterface(new PageFileInterfaceType());
23+
}
10524

106-
$config->addField('filesizeStr', [
107-
'type' => new StringType(),
108-
'description' => 'File size as a formatted string, i.e. “123 Kb”.',
109-
'resolve' => function ($value) {
110-
return (string) $value->filesizeStr;
111-
}
112-
]);
113-
25+
public function getInterfaces()
26+
{
27+
return [ new PageFileInterfaceType() ];
11428
}
11529

11630
}

src/Type/Object/PageImageType.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
namespace ProcessWire\GraphQL\Type\Object;
44

5+
use Youshido\GraphQL\Type\Object\AbstractObjectType;
56
use Youshido\GraphQL\Type\Scalar\StringType;
67
use Youshido\GraphQL\Type\Scalar\IntType;
7-
use ProcessWire\GraphQL\Type\Object\PageFileType;
8+
use ProcessWire\GraphQL\Type\InterfaceType\PageFileInterfaceType;
89

9-
class PageImageType extends PageFileType {
10+
class PageImageType extends AbstractObjectType {
1011

1112
public function getName()
1213
{
@@ -21,7 +22,7 @@ public function getDescription()
2122
public function build($config)
2223
{
2324

24-
parent::build($config);
25+
$config->applyInterface(new PageFileInterfaceType());
2526

2627
$config->addfield('width', [
2728
'type' => new IntType(),
@@ -38,4 +39,9 @@ public function build($config)
3839
]);
3940
}
4041

42+
public function getInterfaces()
43+
{
44+
return [ new PageFileInterfaceType() ];
45+
}
46+
4147
}

0 commit comments

Comments
 (0)